# # File name: lec02.out # # # data example: advertising # advert = data.frame(c(3.2, 1.4, 2.6, 6.9, 3.6, 1.7, 5.0), c(4.9, 2.4, 4.4, 9.6, 4.8, 2.1, 7.3)) names(advert) = c("ads","sale") advert # ads sale # 1 3.2 4.9 # 2 1.4 2.4 # 3 2.6 4.4 # 4 6.9 9.6 # 5 3.6 4.8 # 6 1.7 2.1 # 7 5.0 7.3 advert.lm = lm(sale~ads, data=advert) summary(advert.lm) #output is: #Call: #lm(formula = sale ~ ads, data = advert) # #Residuals: # 1 2 3 4 5 6 7 # 0.2159 0.1564 0.5294 -0.1005 -0.4264 -0.5504 0.1755 # #Coefficients: # Estimate Std. Error t value Pr(>|t|) #(Intercept) 0.34552 0.34688 0.996 0.365 #ads 1.35579 0.08855 15.311 2.16e-05 *** # #Residual standard error: 0.4188 on 5 degrees of freedom #Multiple R-squared: 0.9791, Adjusted R-squared: 0.9749 #F-statistic: 234.4 on 1 and 5 DF, p-value: 2.156e-05 pdf("fig.advert.pdf") plot(advert$ads, advert$sale, xlab="advertising expenditure ($1000)", ylab="sale ($10000)") lines(advert$ads, fitted(advert.lm)) dev.off() #Note: an easier way to get pdf plots is simply to make the plot, #then make sure that the desired graphics window is raised (i.e., #on top), then do File-->Save as --> PDF attributes(advert.lm) # $names # [1] "coefficients" "residuals" "effects" "rank" # [5] "fitted.values" "assign" "qr" "df.residual" # [9] "xlevels" "call" "terms" "model" # $class # [1] "lm" advert.lm$coefficients # (Intercept) ads # 0.3455231 1.3557926 coef(advert.lm) resid(advert.lm) fitted(advert.lm) formula(advert.lm)