A continuación, se utiliza el conjunto de datos “ChickWeight” y se grafican las variable del peso de los pollos (Weight) y los días de vida al momento de la medición (Time).
ChickWeight <- ChickWeight[, -c(3, 4)]
plot(weight~Time,data=ChickWeight)
Luego, se utiliza la función lm para ajustar el modelo lineal.
lm.fit=lm(weight~Time,data=ChickWeight)
lm.fit
##
## Call:
## lm(formula = weight ~ Time, data = ChickWeight)
##
## Coefficients:
## (Intercept) Time
## 27.467 8.803
summary(lm.fit)
##
## Call:
## lm(formula = weight ~ Time, data = ChickWeight)
##
## Residuals:
## Min 1Q Median 3Q Max
## -138.331 -14.536 0.926 13.533 160.669
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 27.4674 3.0365 9.046 <2e-16 ***
## Time 8.8030 0.2397 36.725 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 38.91 on 576 degrees of freedom
## Multiple R-squared: 0.7007, Adjusted R-squared: 0.7002
## F-statistic: 1349 on 1 and 576 DF, p-value: < 2.2e-16
plot(weight~Time,data=ChickWeight)
abline(lm.fit,col='red')
pw<-c(3,5,7,9,11)
predict.lm(lm.fit,data.frame(Time=pw))
## 1 2 3 4 5
## 53.87654 71.48262 89.08870 106.69478 124.30086
Ahora se agregan los nuevos registros al conjunto de datos y se calcula nuevamente la regresión lineal.
ChickWeight2 = rbind(ChickWeight, c(53.87654, 3), c(71.48262, 5), c(89.08870, 7), c(106.69478, 9),c(124.30086, 11))
lm.fit_2=lm(weight~Time,data=ChickWeight2)
plot(weight~Time,data=ChickWeight2)
abline(lm.fit_2,col='red')
summary(lm.fit_2)
##
## Call:
## lm(formula = weight ~ Time, data = ChickWeight2)
##
## Residuals:
## Min 1Q Median 3Q Max
## -138.331 -13.998 0.684 13.533 160.669
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 27.4674 3.0088 9.129 <2e-16 ***
## Time 8.8030 0.2382 36.960 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 38.75 on 581 degrees of freedom
## Multiple R-squared: 0.7016, Adjusted R-squared: 0.7011
## F-statistic: 1366 on 1 and 581 DF, p-value: < 2.2e-16
Se puede observar que no existe un cambio en los coeficientes.
# Valor inicial
ChickWeight2[100,]
## weight Time
## 100 85 8
# Al multiplicarlo por 100
ChickWeight2[100,] = ChickWeight2[100,]*100
ChickWeight2[100,]
## weight Time
## 100 8500 800
Calculando nuevamente la regresión lineal
lm.fit_3=lm(weight~Time,data=ChickWeight2)
plot(weight~Time,data=ChickWeight2)
abline(lm.fit_3,col='red')
summary(lm.fit_3)
##
## Call:
## lm(formula = weight ~ Time, data = ChickWeight2)
##
## Residuals:
## Min 1Q Median 3Q Max
## -156.369 -18.502 6.335 20.376 142.631
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 9.01107 1.78571 5.046 6.04e-07 ***
## Time 10.54087 0.05036 209.303 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 40.55 on 581 degrees of freedom
## Multiple R-squared: 0.9869, Adjusted R-squared: 0.9869
## F-statistic: 4.381e+04 on 1 and 581 DF, p-value: < 2.2e-16
En este caso, se observa un cambio en los coeficientes.