# R code for least squares estimation of # a simple linear regression model # Consider the example on pg. 15 of the book # response variable Y is "attempts" # predictor variable X is "age" age <- c(20, 55, 30) attempts <- c(5, 12, 10) # The above example is a reasonable way to enter # SMALL data sets into R. # See Toluca data example on course web page for an # example of how to read in large data sets in R. ####################################################### # fitting the regression model: task.reg <- lm(attempts ~ age) # Seeing the estimates (b_0 and b_1) of the regression coefficients: task.reg # Note the fitted regression equation is: # # Y-hat = 2.8077 + 0.1769X # # getting the summary regression output: summary(task.reg) # getting the ANOVA table: anova(task.reg) # Note MSE = 5.6538, and sqrt(MSE) = 2.378. # (Can you find these in the R output?) ######### # Plotting the data and the fitted regression line: # Note that the plot function in R requires the X variable to be listed first # (this is different than the plots in SAS): plot(age, attempts) # overlaying the regression line on this scatter plot: abline(task.reg)