data(iris)
head(iris);tail(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 145 6.7 3.3 5.7 2.5 virginica
## 146 6.7 3.0 5.2 2.3 virginica
## 147 6.3 2.5 5.0 1.9 virginica
## 148 6.5 3.0 5.2 2.0 virginica
## 149 6.2 3.4 5.4 2.3 virginica
## 150 5.9 3.0 5.1 1.8 virginica
names(iris)
## [1] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" "Species"
names(iris)<-tolower(names(iris))
names(iris)
## [1] "sepal.length" "sepal.width" "petal.length" "petal.width" "species"
iris<-iris[1:4]
head(iris)
## sepal.length sepal.width petal.length petal.width
## 1 5.1 3.5 1.4 0.2
## 2 4.9 3.0 1.4 0.2
## 3 4.7 3.2 1.3 0.2
## 4 4.6 3.1 1.5 0.2
## 5 5.0 3.6 1.4 0.2
## 6 5.4 3.9 1.7 0.4
names(iris)<-c('sl','sw','pl','pw')
head(iris)
## sl sw pl pw
## 1 5.1 3.5 1.4 0.2
## 2 4.9 3.0 1.4 0.2
## 3 4.7 3.2 1.3 0.2
## 4 4.6 3.1 1.5 0.2
## 5 5.0 3.6 1.4 0.2
## 6 5.4 3.9 1.7 0.4
m2<-lm(sl~.,data=iris)
summary(m2)
##
## 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
step(m2,direction = "both")
## Start: AIC=-343.04
## sl ~ sw + pl + pw
##
## Df Sum of Sq RSS AIC
## <none> 14.445 -343.04
## - pw 1 1.8834 16.329 -326.66
## - sw 1 9.4353 23.881 -269.63
## - pl 1 15.4657 29.911 -235.86
##
## Call:
## lm(formula = sl ~ sw + pl + pw, data = iris)
##
## Coefficients:
## (Intercept) sw pl pw
## 1.8560 0.6508 0.7091 -0.5565
step(m2,direction = "forward")
## Start: AIC=-343.04
## sl ~ sw + pl + pw
##
## Call:
## lm(formula = sl ~ sw + pl + pw, data = iris)
##
## Coefficients:
## (Intercept) sw pl pw
## 1.8560 0.6508 0.7091 -0.5565
step(m2,direction = "backward")
## Start: AIC=-343.04
## sl ~ sw + pl + pw
##
## Df Sum of Sq RSS AIC
## <none> 14.445 -343.04
## - pw 1 1.8834 16.329 -326.66
## - sw 1 9.4353 23.881 -269.63
## - pl 1 15.4657 29.911 -235.86
##
## Call:
## lm(formula = sl ~ sw + pl + pw, data = iris)
##
## Coefficients:
## (Intercept) sw pl pw
## 1.8560 0.6508 0.7091 -0.5565