install.packages("Stat2Data")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.5'
## (as 'lib' is unspecified)
library(Stat2Data)
data(Cereal)
attach(Cereal)
## The following object is masked _by_ .GlobalEnv:
## 
##     Cereal
cereal.lm = lm(Calories~Sugar)

Sugar Plot

plot(Calories~Sugar, main="Calories v. Sugar", xlab= "Sugar",ylab ="Calories")
abline(cereal.lm)

For every 1 gram of sugar, there is an increase in 2.4808 predicted calories, on average.

For 10 gram cereal, is it predicted to be approximately 112.285 calories

plot of residuals

plot(cereal.lm$fitted.values,cereal.lm$residuals,main="Residual Plot",ylab="Residuals",xlab="Predicted Calories")
abline(0,0)

The residual plot shows a random scatter, so the linear model appropriately fits the data.

Typical size of error for residuals is 19.27

The residual for 1 gram of sugar having 110 calories with predicted calories is approximately 20. Compared to the typical error, this is very likely.

histogram of residuals

hist(cereal.lm$residuals)

### normal plot

qqnorm(cereal.lm$residuals)
qqline(cereal.lm$residuals)

Looking at the histogram and normal plot of residuals, the linear regression model is a good summary for the relationship between sugar and calories in cereals.

Summary

summary(cereal.lm)
## 
## Call:
## lm(formula = Calories ~ Sugar)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -37.428  -9.832   0.245   8.909  40.322 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  87.4277     5.1627  16.935   <2e-16 ***
## Sugar         2.4808     0.7074   3.507   0.0013 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 19.27 on 34 degrees of freedom
## Multiple R-squared:  0.2656, Adjusted R-squared:  0.244 
## F-statistic:  12.3 on 1 and 34 DF,  p-value: 0.001296

About 24.4% of the variation in calories is explained by the Sugar content.

since p-val<0.01, the sugar content is a significant indicator for predicting calories in cereal.