For each question, show to the R source code and the R output and graphs.
> setwd("c:/it223") > getwd( ) [1] "d:/it223" > laundry_df <- read.csv("laundry-detergent.txt", header=T) > print(laundary_df) 1 61 17 2 55 30 3 50 9 4 46 13 5 35 8 6 32 5 7 59 22 8 52 23 9 48 16 10 46 13 11 34 12 12 29 14 13 56 22 14 51 11 15 48 15 16 45 17 17 33 7 18 26 11 19 55 16 20 50 15 21 48 18 22 36 8 23 32 6 24 26 13
> print(laundry_df$Rating) [1] 61 55 50 46 35 32 59 52 48 46 34 29 [13] 56 51 48 45 33 26 55 50 48 36 32 26 > print(laundry_df$Price) [1] 17 30 9 13 8 5 22 23 16 13 12 14 [13] 22 11 15 17 7 11 16 15 18 8 6 13
> cor(laundry_df$Rating, laundry_df$Price) [1] 0.6707535
> model <- lm(Price ~ Rating, data=laundry_df) > model Call: lm(formula = Price ~ Rating, data = laundry_df) Coefficients: (Intercept) Rating -2.1443 0.3727The regression equation is y = 0.3727x - 2.1443.
> summary(model) Call: lm(formula = Price ~ Rating, data = laundry_df) Residuals: Min 1Q Median 3Q Max -7.491 -3.185 -1.119 2.597 11.645 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -2.14435 3.96484 -0.541 0.594051 Rating 0.37271 0.08786 4.242 0.000334 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 4.539 on 22 degrees of freedom Multiple R-squared: 0.4499, Adjusted R-squared: 0.4249 F-statistic: 17.99 on 1 and 22 DF, p-value: 0.0003342
> res <- predict(model) > res 1 2 3 4 -3.5910035 11.6452605 -7.4911862 -2.0003435 5 6 7 8 -2.9005262 -4.7823942 2.1544178 5.7633925 9 10 11 12 0.2542352 -2.0003435 1.4721845 5.3357378 13 14 15 16 3.2725498 -5.8638968 -0.7457648 2.3723672 17 18 19 20 -3.1551048 3.4538698 -2.3547395 -1.4911862 21 22 23 24 2.2542352 -3.2732368 -3.7823942 5.4538698
> pred <- predict(model) > pred 1 2 3 4 20.591003 18.354739 16.491186 15.000343 5 6 7 8 10.900526 9.782394 19.845582 17.236607 9 10 11 12 15.745765 15.000343 10.527816 8.664262 13 14 15 16 18.727450 16.863897 15.745765 14.627633 17 18 19 20 10.155105 7.546130 18.354739 16.491186 21 22 23 24 15.745765 11.273237 9.782394 7.546130
> plot(pred, res, xlab="Predicted Values", ylab="Residuals", + main="Normal Plot of Residuals")
> plot(resid, main="Normal Plot of Residuals")