1.- Cree su propio ejemplo de regresión lineal simple utilizando un conjunto de datos con dos variables y la función lm. Grafique utilizando las funciones plot y abline.
data = data.frame(ChickWeight)
data = subset(data, select = c(Time, weight))
plot(weight~Time,data=data)
lm.fit=lm(weight~Time,data=data)
lm.fit
## 
## Call:
## lm(formula = weight ~ Time, data = data)
## 
## Coefficients:
## (Intercept)         Time  
##      27.467        8.803
summary(lm.fit)
## 
## Call:
## lm(formula = weight ~ Time, data = data)
## 
## 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
abline(27.47,8.8,col='blue')

Predecimos 5 valores con la recta que creamos usando \(lm\).

pw<-c(1,5,9,17,21)
predicts= c(predict.lm(lm.fit,data.frame(Time=pw)))

Creamos un nuevo registro con los 5 datos predecidos y los agregamos al dataset. Luego graficamos y hacemos regresión lineal nuevamente.

nuevo_registro = data.frame(Time = pw, weight = predicts)
data = rbind(data, nuevo_registro)
plot(weight~Time,data=data)
lm.fit=lm(weight~Time,data=data)
lm.fit
## 
## Call:
## lm(formula = weight ~ Time, data = data)
## 
## Coefficients:
## (Intercept)         Time  
##      27.467        8.803
summary(lm.fit)
## 
## Call:
## lm(formula = weight ~ Time, data = data)
## 
## 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.0083   9.131   <2e-16 ***
## Time          8.8030     0.2374  37.076   <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.7029, Adjusted R-squared:  0.7024 
## F-statistic:  1375 on 1 and 581 DF,  p-value: < 2.2e-16
abline(27.47,8.8,col='blue')

Vemos que la recta resultante no ha cambiado respecto a la anterior.