# This sample program illustrate simple linear regression and # polynomial regression of second order. # Refer to # http://lib.stat.cmu.edu/DASL/Stories/tvads.html # for background of the data # 1. Initiate an R section # 2. Change the working dir to the one where the data is located. # Reading data and assigned it as the working data set tvad <- read.table("tvad.txt", header=T) attach(tvad) # Prelim inspection tvad # Initiate graphic devices par(mfrow=c(2,2)); hist(spend); boxplot(spend); qqnorm(spend); plot(density(spend)); stem(spend) ; summary(spend); hist(yields); boxplot(yields); qqnorm(yields); plot(density(yields)); stem(yields) ; summary(yields); plot(yields~spend) fm0 <- lm(yields~1) # Summarization by mean only. summary(fm0) # Yields = b_0 + epsilon # Plot the fitted line overlayed over the y vs. x scatter plot plot(spend,yields,xlab="spend", ylab="yields", main="Summary by mean"); abline(coef(fm0),0) # Plots for diagnosis par(mfrow=c(2,2)); plot(fm0) fm1 <- lm(yields~spend) # Regress yields on spend. summary(fm1) # Yields = b_0 + b_1 (spend) + epsilon names(fm1) # List variables from the regression output # Plot the fitted line overlayed over the y vs. x scatter plot plot(spend,yields,xlab="spend", ylab="yields", main="Simple Linear Regression"); abline(coef(fm1)) # Plots for diagnosis par(mfrow=c(2,2)); plot(fm1) fm2 <- lm(yields~spend+I(spend^2)) summary(fm2) par(mfrow=c(2,2)); plot(fm2) plot(spend,yields,xlab="spend", ylab="yields", main="Polynomial Regression"); hat<-predict(fm2) points(approx(spend, hat), col = 2, pch = "*") detach(tvad) q() # Exit (You don't need to save workspace image)