Linear regression example II
Source code name: "stat_regression2.py"
Programming language: Python
Topic: Statistics/Regression
DMelt Version 1. Last modified: 05/09/2015. License: Pro
https://datamelt.org/code/cache/stat_regression2_251.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 java.awt import Color,Font
from java.util import Random
from jhplot  import HPlot, P1D, F1D 
from org.apache.commons.math3.stat.regression import SimpleRegression

print "\napache.commons.math:"
c1 = HPlot("Canvas",600,400,1, 1)
c1.visible(1)
c1.setGTitle("Linear regression")
c1.setAutoRange()


reg=SimpleRegression()
rand = Random()
p1=P1D("Data")
for i in range(500):
      x=10*rand.nextGaussian()
      y=20*x +50*rand.nextGaussian()+5.0
      p1.add(x,y)
      reg.addData(x,y) 


 
a1=reg.getIntercept()
a2=reg.getInterceptStdErr() 
b1=reg.getSlope()
b2=reg.getSlopeStdErr() 

func="P1*x+P0"
f1 = F1D(func, -40.0, 40.0, 0)
f1.setPar("P0",a1)
f1.setPar("P1",b1)
f1.parse()


f1.setColor(Color.blue)
p1.setStyle("p")
p1.setSymbolSize(3)
c1.setNameX("X")
c1.setNameY("Y")
# c1.setLegendFont(Font("SansSerif", Font.BOLD, 16))
c1.draw(p1)
c1.draw(f1)


print "Intercept=",a1,"+/-",a2
print "Slope=",b1,"+/-",b2


You see the box below because you did not login.