Está en la página 1de 5

Trabajo

data("iris")
attach(iris)
head(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
tail(iris)

## 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
Quitando la variable Setosa de la Data “IRIS” y renombrandola “iris1”
iris1=iris[,c(1,2,3,4)]
head(iris1)

## 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
tail(iris1)

## Sepal.Length Sepal.Width Petal.Length Petal.Width


## 145 6.7 3.3 5.7 2.5
## 146 6.7 3.0 5.2 2.3
## 147 6.3 2.5 5.0 1.9
## 148 6.5 3.0 5.2 2.0
## 149 6.2 3.4 5.4 2.3
## 150 5.9 3.0 5.1 1.8
Para observar el diagrama de dispersión de todas las variables utilizamos el siguiente código
pairs(iris[,1:4],pch=as.numeric(iris$Species),col=iris$Species)

1
2.0 3.0 4.0 0.5 1.5 2.5

7.5
Sepal.Length

6.0
4.5
4.0

Sepal.Width
3.0
2.0

7
5
Petal.Length

3
1
2.5
1.5

Petal.Width
0.5

4.5 5.5 6.5 7.5 1 2 3 4 5 6 7

regresion <- lm(Petal.Width~Petal.Length, data = iris)


regresion

##
## Call:
## lm(formula = Petal.Width ~ Petal.Length, data = iris)
##
## Coefficients:
## (Intercept) Petal.Length
## -0.3631 0.4158
residuos <- rstandard(regresion)
shapiro.test(residuos)

##
## Shapiro-Wilk normality test
##
## data: residuos
## W = 0.98379, p-value = 0.07524
summary(regresion)

##
## Call:
## lm(formula = Petal.Width ~ Petal.Length, data = iris)
##
## Residuals:
## Min 1Q Median 3Q Max

2
## -0.56515 -0.12358 -0.01898 0.13288 0.64272
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.363076 0.039762 -9.131 4.7e-16 ***
## Petal.Length 0.415755 0.009582 43.387 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.2065 on 148 degrees of freedom
## Multiple R-squared: 0.9271, Adjusted R-squared: 0.9266
## F-statistic: 1882 on 1 and 148 DF, p-value: < 2.2e-16
anova(regresion)

## Analysis of Variance Table


##
## Response: Petal.Width
## Df Sum Sq Mean Sq F value Pr(>F)
## Petal.Length 1 80.26 80.260 1882.5 < 2.2e-16 ***
## Residuals 148 6.31 0.043
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
plot(Petal.Width~Petal.Length,xlab = "Petal.Length", ylab = "Petal.Width",pch=as.numeric(iris$Species)
legend("topleft",c("Setosa","Versicolor","Virginica"),pch=c(1,2,3),
col=c("black","red","green"),cex=0.5,bty="n")
abline(regresion)

3
3.0

Setosa
Versicolor
Virginica
2.5
2.0
Petal.Width

1.5
1.0
0.5
0.0

0 2 4 6 8

Petal.Length

valores.ajustados <- fitted(regresion)


plot(valores.ajustados, residuos,pch=as.numeric(iris$Species),col=iris$Species)
abline(h=0)
legend("topleft",c("Setosa","Versicolor","Virginica"),pch=c(1,2,3),
col=c("black","red","green"),cex=0.5,bty="n")

4
Setosa
3

Versicolor
Virginica
2
1
residuos

0
−1
−2

0.0 0.5 1.0 1.5 2.0 2.5

valores.ajustados

También podría gustarte