 |
Linear regression example. Show predictions and confidence range
Source code name: "stat_regression1.py"
Programming language: Python
Topic: Statistics/Regression
DMelt Version 1. Last modified: 09/29/2015. License: Pro
https://datamelt.org/code/cache/stat_regression1_4845.py
To run this script using the DMelt IDE,
copy the above URL link to the menu [File]→[Read script from URL] of the DMelt IDE.
#
from jhplot import *
from jhplot.stat import LinReg
from java.awt import Color
from java.util import Random
print "\njhplot.regression:"
c1 = HPlot("Canvas")
c1.visible(1)
c1.setGTitle("Linear regression")
c1.setAutoRange()
p1= P1D("data")
rand = Random();
for i in range(500):
x=rand.nextGaussian()
y=rand.nextGaussian()
p1.add(0.2*x, y)
c1.draw(p1);
#d="-0.3222682085734333456+(-0.0103614777304622313*x)"
#f1=F1D("ss",d,20,100,0)
#f1.parse()
r = LinReg(p1)
print "Intercept=",r.getIntercept(), "+/-",r.getInterceptError()
print "Slope=",r.getSlope(),"+/-",r.getSlopeError()
# f1= r.getResult()
#print f1.getName()
#print f1.eval(22)
# get predictions as band
pC=r.getConfidenceBand(Color.red,0.5)
c1.draw(pC)
pP=r.getPredictionBand(Color.red,0.5)
c1.draw(pP)
# draw original data
# c1.draw(p1)
# draw F1D function with the result
c1.draw( r.getResult() )
# draw P1D[2] with predictions
c1.draw( r.getPrediction() )
# draw P1D[2} with confidence
c1.draw( r.getConfidence() )
# p.setErrFillColor(Color.blue,0.5)
# p.setErr(1)
# c1.draw(p)
You see the box below because you did not login.