data(cars)
head(cars);tail(cars)
## speed dist
## 1 4 2
## 2 4 10
## 3 7 4
## 4 7 22
## 5 8 16
## 6 9 10
## speed dist
## 45 23 54
## 46 24 70
## 47 24 92
## 48 24 93
## 49 24 120
## 50 25 85
m<-lm(dist~speed,data=cars)
coef(m)
## (Intercept) speed
## -17.579095 3.932409
fitted(m)[1:4]
## 1 2 3 4
## -1.849460 -1.849460 9.947766 9.947766
residuals(m)[1:4]
## 1 2 3 4
## 3.849460 11.849460 -5.947766 12.052234
fitted(m)[1:4]+residuals(m)[1:4]
## 1 2 3 4
## 2 10 4 22
confint(m)
## 2.5 % 97.5 %
## (Intercept) -31.167850 -3.990340
## speed 3.096964 4.767853
deviance(m)
## [1] 11353.52
predict(m,newdata=data.frame(speed=3))
## 1
## -5.781869
summary(m)
##
## Call:
## lm(formula = dist ~ speed, data = cars)
##
## Residuals:
## Min 1Q Median 3Q Max
## -29.069 -9.525 -2.272 9.215 43.201
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -17.5791 6.7584 -2.601 0.0123 *
## speed 3.9324 0.4155 9.464 1.49e-12 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 15.38 on 48 degrees of freedom
## Multiple R-squared: 0.6511, Adjusted R-squared: 0.6438
## F-statistic: 89.57 on 1 and 48 DF, p-value: 1.49e-12
anova(m)
## Analysis of Variance Table
##
## Response: dist
## Df Sum Sq Mean Sq F value Pr(>F)
## speed 1 21186 21185.5 89.567 1.49e-12 ***
## Residuals 48 11354 236.5
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
full<-lm(dist~speed,data=cars)
reduced<-lm(dist~1,data=cars)
anova(reduced,full)
## Analysis of Variance Table
##
## Model 1: dist ~ 1
## Model 2: dist ~ speed
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 49 32539
## 2 48 11354 1 21186 89.567 1.49e-12 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
plot(m)




plot(m,which=c(4,6))


dim(cars)
## [1] 50 2
cars[c(23,39,40),]
## speed dist
## 23 14 80
## 39 20 32
## 40 20 48
data(iris)
names(iris)<-tolower(names(iris))
iris<-iris[1:4]
names(iris)<-c('sl','sw','pl','pw')
names(iris)
## [1] "sl" "sw" "pl" "pw"
m1<-lm(sl~.,data=iris)
summary(m1)
##
## Call:
## lm(formula = sl ~ ., data = iris)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.82816 -0.21989 0.01875 0.19709 0.84570
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.85600 0.25078 7.401 9.85e-12 ***
## sw 0.65084 0.06665 9.765 < 2e-16 ***
## pl 0.70913 0.05672 12.502 < 2e-16 ***
## pw -0.55648 0.12755 -4.363 2.41e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.3145 on 146 degrees of freedom
## Multiple R-squared: 0.8586, Adjusted R-squared: 0.8557
## F-statistic: 295.5 on 3 and 146 DF, p-value: < 2.2e-16
options(scipen = 999)
data("Orange")
with(Orange,plot(Tree,circumference,xlab="tree",ylab="circumference"))

with(Orange,interaction.plot(age,Tree,circumference))

Orange$Tree<-factor(Orange$Tree)
m<-lm(circumference~Tree*age,data=Orange)
anova(m)
## Analysis of Variance Table
##
## Response: circumference
## Df Sum Sq Mean Sq F value Pr(>F)
## Tree 4 11841 2960 27.2983 0.000000008428 ***
## age 1 93772 93772 864.7348 < 0.00000000000000022 ***
## Tree:age 4 4043 1011 9.3206 0.000094016610 ***
## Residuals 25 2711 108
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1