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 | |
Re: How to graph trendline
by RichardK (Parson) on Oct 02, 2012 at 09:16 UTC | |
Re: How to graph trendline
by pvaldes (Chaplain) on Oct 02, 2012 at 20:36 UTC |
Back to
Seekers of Perl Wisdom