Simple Linear Regression

Calories Consumed

Assignment 12

mydata <- read.csv("C:\\Users\\RISHI RAHUL\\Desktop\\DS\\2 SLR\\Assignment\\calories_consumed.csv")

attach(mydata)

# 1st, 2nd, 3rd, 4th Moment Business Decision
summary(mydata)
##  Weight.gained..grams. Calories.Consumed
##  Min.   :  62.0        Min.   :1400     
##  1st Qu.: 114.5        1st Qu.:1728     
##  Median : 200.0        Median :2250     
##  Mean   : 357.7        Mean   :2341     
##  3rd Qu.: 537.5        3rd Qu.:2775     
##  Max.   :1100.0        Max.   :3900
var(Weight.gained..grams.)
## [1] 111350.7
sd(Calories.Consumed)
## [1] 752.1095
library(e1071)
skewness(Weight.gained..grams.)
## [1] 0.9994639
kurtosis(Calories.Consumed)
## [1] -0.9277095
# Plotting
boxplot(Weight.gained..grams.)

boxplot(Calories.Consumed, horizontal = TRUE)

hist(Calories.Consumed)

qqnorm(Calories.Consumed)
qqline(Calories.Consumed)

plot(mydata)

cor(mydata)
##                       Weight.gained..grams. Calories.Consumed
## Weight.gained..grams.              1.000000          0.946991
## Calories.Consumed                  0.946991          1.000000
# Model
model <- lm(Weight.gained..grams. ~ Calories.Consumed)
summary(model) # R Squared Value : 0.8968
## 
## Call:
## lm(formula = Weight.gained..grams. ~ Calories.Consumed)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -158.67 -107.56   36.70   81.68  165.53 
## 
## Coefficients:
##                     Estimate Std. Error t value Pr(>|t|)    
## (Intercept)       -625.75236  100.82293  -6.206 4.54e-05 ***
## Calories.Consumed    0.42016    0.04115  10.211 2.86e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 111.6 on 12 degrees of freedom
## Multiple R-squared:  0.8968, Adjusted R-squared:  0.8882 
## F-statistic: 104.3 on 1 and 12 DF,  p-value: 2.856e-07
model_log <- lm((Weight.gained..grams.) ~ log(Calories.Consumed))
summary(model_log) # R Squared Value : 0.8077
## 
## Call:
## lm(formula = (Weight.gained..grams.) ~ log(Calories.Consumed))
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -187.44 -142.96   23.13  113.20  213.82 
## 
## Coefficients:
##                        Estimate Std. Error t value Pr(>|t|)    
## (Intercept)             -6955.7     1030.9  -6.747 2.05e-05 ***
## log(Calories.Consumed)    948.4      133.6   7.100 1.25e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 152.3 on 12 degrees of freedom
## Multiple R-squared:  0.8077, Adjusted R-squared:  0.7917 
## F-statistic:  50.4 on 1 and 12 DF,  p-value: 1.248e-05
model_exp <- lm(log(Weight.gained..grams.) ~ Calories.Consumed)
summary(model_exp) # R Squared Value : 0.8776
## 
## Call:
## lm(formula = log(Weight.gained..grams.) ~ Calories.Consumed)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.86537 -0.10532  0.02462  0.13467  0.42632 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)    
## (Intercept)       2.8386724  0.2994581   9.479 6.36e-07 ***
## Calories.Consumed 0.0011336  0.0001222   9.276 8.02e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3314 on 12 degrees of freedom
## Multiple R-squared:  0.8776, Adjusted R-squared:  0.8674 
## F-statistic: 86.04 on 1 and 12 DF,  p-value: 8.018e-07
confint(model,level=0.95)
##                          2.5 %       97.5 %
## (Intercept)       -845.4266546 -406.0780569
## Calories.Consumed    0.3305064    0.5098069
predict(model,interval="predict")
## Warning in predict.lm(model, interval = "predict"): predictions on current data refer to _future_ responses
##            fit        lwr       upr
## 1     4.482599 -258.20569  267.1709
## 2   340.607908   88.93791  592.2779
## 3   802.780209  533.81393 1071.7465
## 4   298.592245   46.63271  550.5518
## 5   424.639236  172.59086  676.6876
## 6    46.498263 -213.75953  306.7561
## 7   -37.533065 -302.93258  227.8664
## 8   172.545254  -82.18110  427.2716
## 9   550.686227  295.69632  805.6761
## 10 1012.858527  724.99432 1300.7227
## 11   75.909227 -182.81852  334.6370
## 12  172.545254  -82.18110  427.2716
## 13  508.670563  254.97398  762.3671
## 14  634.717554  376.22600  893.2091
# Simple Model has higher R Squared Value
model_final <- predict(model)
model_final
##           1           2           3           4           5           6 
##    4.482599  340.607908  802.780209  298.592245  424.639236   46.498263 
##           7           8           9          10          11          12 
##  -37.533065  172.545254  550.686227 1012.858527   75.909227  172.545254 
##          13          14 
##  508.670563  634.717554
rmse <- sqrt(mean((model_final-Weight.gained..grams.)^2))
rmse
## [1] 103.3025
plot(model)

hist(residuals(model))