# Simple linear regression

#EXAMPLE 1
datacar=mtcars
attach(datacar)
plot(wt,mpg, main = "Scatter Plot")
###################################
cor(wt,mpg)
## [1] -0.8676594
#######################
modelcar= lm(mpg ~wt)   # note: alway lm(y~x)
summary(modelcar)
## 
## Call:
## lm(formula = mpg ~ wt)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.5432 -2.3647 -0.1252  1.4096  6.8727 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  37.2851     1.8776  19.858  < 2e-16 ***
## wt           -5.3445     0.5591  -9.559 1.29e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.046 on 30 degrees of freedom
## Multiple R-squared:  0.7528, Adjusted R-squared:  0.7446 
## F-statistic: 91.38 on 1 and 30 DF,  p-value: 1.294e-10
######################
attributes(modelcar)
## $names
##  [1] "coefficients"  "residuals"     "effects"       "rank"         
##  [5] "fitted.values" "assign"        "qr"            "df.residual"  
##  [9] "xlevels"       "call"          "terms"         "model"        
## 
## $class
## [1] "lm"
modelcar$coefficients
## (Intercept)          wt 
##   37.285126   -5.344472
# or 
coef(modelcar)
## (Intercept)          wt 
##   37.285126   -5.344472
#####################
#Fit the regression line
plot(wt,mpg, main = "Scatter Plot")
abline(modelcar, col="red")

####################
#Confidence interval for parameters of slope and intercept
confint(modelcar)
##                 2.5 %    97.5 %
## (Intercept) 33.450500 41.119753
## wt          -6.486308 -4.202635
confint(modelcar, level = .99)
##                 0.5 %    99.5 %
## (Intercept) 32.121659 42.448593
## wt          -6.881997 -3.806946
###############################
# We might be interested in the ANOVA
anova(modelcar)
## Analysis of Variance Table
## 
## Response: mpg
##           Df Sum Sq Mean Sq F value    Pr(>F)    
## wt         1 847.73  847.73  91.375 1.294e-10 ***
## Residuals 30 278.32    9.28                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
################################
#Checking the assumptions of linear model
# R has a set of buiin diagnostic plots for 4 of the assumptions:
# 1-Errors are normal #2-Mean of errors=1 #3-variance of errors=Constant
#Underlying relationship between X and Y is linear

#5 The errors are independent. (Note that this can not be checked by 
#the following built-in plots)

plot(modelcar) # Hit it 4 times to see each time one of the diagnostic plot

#we can see all the 4 plots in one screen
par(mfrow=c(2,2))
plot(modelcar)

#Agian we can reset it by 
par(mfrow=c(1,1))

##############################################################
# Multiple Linear Regression
datacar2 <- lm(mpg~wt+disp)
summary(datacar2)
## 
## Call:
## lm(formula = mpg ~ wt + disp)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.4087 -2.3243 -0.7683  1.7721  6.3484 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 34.96055    2.16454  16.151 4.91e-16 ***
## wt          -3.35082    1.16413  -2.878  0.00743 ** 
## disp        -0.01773    0.00919  -1.929  0.06362 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.917 on 29 degrees of freedom
## Multiple R-squared:  0.7809, Adjusted R-squared:  0.7658 
## F-statistic: 51.69 on 2 and 29 DF,  p-value: 2.744e-10