Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

How to graph trendline

by djones (Initiate)
on Oct 01, 2012 at 22:08 UTC ( #996757=perlquestion: print w/replies, xml ) Need Help??
djones has asked for the wisdom of the Perl Monks concerning the following question:

I have a set of test scores with dates that I can graph. I would like to be able to include a trendline on the graph. What I wrote is not quite right -- the line just follows the original - it's not straight. I would be extremely grateful for any help.

I calculated the slope thus (some subs not included):

#---------------------------------------------------------- # FOR SLOPE #---------------------------------------------------------- my $mean_score = &average(\@allscores); $mean_score= sprintf("%.0f",$mean_score); my $stdev_score = &stdev(\@allscores); $stdev_score= sprintf("%.3f",$stdev_score); my @list_of_x; my @list_of_y; my @list_of_z; my $num_scores = @allscores; # Get list of x # since each date is just ... yeah... 1 my $dates = 1; foreach ($i=0; $i<$num_scores; $i++){ @list_of_x[$i] = ($dates-$mean_score)/$stdev_score; @list_of_x[$i]= sprintf("%.2f",@list_of_x[$i]); $dates++; } # Get list of y foreach ($i=0; $i<$num_scores; $i++){ @list_of_y[$i] = (@allscores[$i]-$mean_score)/$stdev_score; @list_of_y[$i]= sprintf("%.2f",@list_of_y[$i]); } # Get the list of z-scores for($i=0; $i<$num_scores; $i++){ @list_of_z[$i]= (@list_of_x[$i] * @list_of_y[$i]); @list_of_z[$i]= sprintf("%.2f",@list_of_z[$i]); } # add up the z-scores my $sigma = 0; for($i=0; $i<$num_scores; $i++){ $sigma = ($sigma + @list_of_z[$i]); } # get the correlation my $correl = $sigma/($num_scores-1); # get the slope my $stdev_x = &stdev(\@list_of_x); my $stdev_y = &stdev(\@list_of_y); my $slope = $correl * ($stdev_x / $stdev_y); $slope= sprintf("%.2f",$slope); # add slope to each y ... y is each score my @trendline; for($i=0; $i<$num_scores; $i++){ @trendline[$i] = @allscores[$i]+$slope; @trendline[$i] = sprintf("%.4f",@trendline[$i]); } #create the data for transfer to the graph program $trendlineString = join(',',@trendline);

And the graphing script does this:

my @data = ([@alldates], [@allScores], [@trendline] ); my $mygraph = GD::Graph::lines->new(600, 300); $mygraph->set( x_label => 'Assessments', y_label => 'Score', title => 'Progress Monitoring Report for '.$student, y_max_value => 100, y_min_value => 0, show_values => 1, # Show the grid long_ticks => 1, #line_types => [1, 2, 4], line_types => [1], # Set the thickness of line line_width => 4, # Set colors for datasets #dclrs => ['blue', 'green', 'red'], dclrs => ['blue','red'], ) or warn $mygraph->error; $mygraph->set_legend_font(GD::gdMediumBoldFont); my $myimage = $mygraph->plot(\@data) or die $mygraph->error; print "Content-type: image/png\n\n"; print $myimage->png;

Replies are listed 'Best First'.
Re: How to graph trendline
by frozenwithjoy (Priest) on Oct 02, 2012 at 01:51 UTC
    Are you familiar with the the statistical programming language 'R'? Everything you've done and want to do would be just a few lines in R. If you did use R, you can use Statistics::R to run R commands via Perl.
Re: How to graph trendline
by RichardK (Parson) on Oct 02, 2012 at 09:16 UTC

    I'm not sure exactly what your calculation does, but Statistics::Descriptive can calculate a least squares fit, if that's any use to you?

Re: How to graph trendline
by pvaldes (Chaplain) on Oct 02, 2012 at 20:36 UTC
    use Statistics::R ; my $R = Statistics::R->new() ; $R->startR ; $R->send(q`pdf("/tmp/examplot.pdf")`) ; $R->send(q`adata <- c(10,20,30,25,24,23,12,7,8,9,10,10,28,29,29,34,3 +8,38,11,14,40,42,41)`) ; $R->send(q`bdata <- c(0,3,4,4,1,1,1,7,7,7,1,0,7,4,7,2,1,3,1,0,10,9,1 +1)`) ; $R->send(q`plot(adata,bdata, xlab = 'temp', ylab = 'laziness', main += 'silly experiment')`); $R->send(q`lines(lowess(adata,bdata),f = 4/5, col = 'Red')`) ; my $ret = $R->read ; $R->stopR(); system("xpdf /tmp/examplot.pdf") or die $!;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://996757]
Approved by Athanasius
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2017-08-21 20:33 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (325 votes). Check out past polls.