# ::Free Statistics and Forecasting Software::

v1.2.1

### :: Simulated Regression - Free Statistics Software (Calculator) ::

All rights reserved. The non-commercial (academic) use of this software is free of charge. The only thing that is asked in return is to cite this software when results are used in publications.

This program is one that an instructor could use to introduce simple linear regression. This R module uses a simple simulated data set and allows a sequence of graphs which show what regression is trying to do, how you can use it for prediction, and shows the residual plot. The last part uses a quadratic data set to show what the residual plot looks like in this case.
You can replace the default x and y by changing the underlying R code - for example: x=1:20 and y=1:20+rgamma(20,2,1)

 Send output to: Browser Blue - Charts White Browser Black/White CSV Chart options Width: Height:

 Source code of R module x=0:10 y=x+2+1.5*rnorm(length(x)) bitmap(file="test1.png") print(x) print(y) plot(x,y,xlab="predictor,x",ylab="predicted y",main="data for estimating predictive model",pch=16,cex=1.5,xlim=c(min(x)-.2*sd(x),max(x)+.2*sd(x)),ylim=c(0-.2*sd(y),max(y)+.2*sd(y))) lmout=lm(y~x) lines(x,lmout\$fitted,col="red",lwd=2) x0=mean(x)+sd(x) y0=lmout\$coefficients[]+lmout\$coefficients[]*x0 x1=mean(x)-.5*sd(x) y1=lmout\$coefficients[]+lmout\$coefficients[]*x1 arrows(x0,min(y)-.2*sd(y),x0,y0,col="blue",code=2) arrows(x0,y0,min(x)-.2*sd(x),y0,col="blue",code=2) arrows(x1,min(y)-.2*sd(y),x1,y1,col="blue",code=2) arrows(x1,y1,min(x)-.2*sd(x),y1,col="blue",code=2) for (i in 1:length(x)) { lines(c(x[i],x[i]),c(y[i],lmout\$fitted[i]),col="darkgreen",lwd=1.5) } legend(mean(x),mean(y)-.5*sd(x),"regression line-red") legend(mean(x),mean(y)-1.0*sd(x),"predictions-blue") legend(mean(x),mean(y)-1.5*sd(x),"prediction errors-green") text(mean(x)-.75*sd(x),max(y)+.15*sd(y),"Regression Line Minimizes",col="purple") text(mean(x)-sd(x),max(y)-.10*sd(y),"Root-Mean-Square",col="purple") text(mean(x)+0.4*sd(x),max(y)-.10*sd(y),"Prediction Error",col="darkgreen") dev.off() bitmap(file="myresid.png") plot(x,lmout\$residuals,xlab="predictor,x",ylab="prediction error (residual)",main="Residuals from Regression Fit",xlim=c(min(x)-.2*sd(x),max(x)+.2*sd(x)),pch=16,cex=1.5) lines(c(min(x),max(x)),c(0,0),lwd=2) for (i in 1:length(x)) { lines(c(x[i],x[i]),c(lmout\$residuals[i],0),col="darkgreen",lwd=1.5) } dev.off() z=x+5*(x/sd(x))^2+5*rnorm(length(x)) lmout2=lm(z~x) bitmap(file="mynew.png") plot(x,z,pch=16,cex=1.5,main="New data set - linear? (Not!)") lines(x,lmout2\$fitted,lwd=2,col="red") dev.off() bitmap(file="myresid1.png") plot(x,lmout2\$residuals,pch=16,cex=1.5,main="Residual plot for new data",ylab="residual") lines(c(min(x),max(x)),c(0,0),lwd=2) fit=loess(lmout2\$residuals~x,span=1,degree=2) lines(fit\$x,fit\$fitted,col="red") dev.off()
 Top | Output | Charts | References

 Cite this software as: Weldon Larry, (2017), Simulated Regression (v1.0.3) in Free Statistics Software (v1.2.1), Office for Research Development and Education, URL https://www.wessa.net/rwasp_regr.wasp/ The R code is based on : Weldon Larry, Stat Ed Programs for Demos in R, Simon Fraser University, URL http://www.stat.sfu.ca/~weldon/title.doc Weldon Larry, R Programs for Statistics Education, Simon Fraser University, URL http://www.stat.sfu.ca/~weldon/Programs Weldon Larry, Instructions for using R programs for Statistics Education, Simon Fraser University, URL http://www.stat.sfu.ca/~weldon/instructions.doc
 Top | Output | Charts | References