#Analisis de regresion lineal
#datos de pacientes
# peso, edad, grasa
grasas = read.table("http://verso.mat.uam.es/~joser.berrendero/datos/EdadPesoGrasas.txt", header=TRUE)

#para conocer el nombre de las variables
names(grasas)
## [1] "peso"   "edad"   "grasas"
#regresion lineal simple para h0
pairs(grasas)

#tabla de correlacion 
cor(grasas)
##             peso      edad    grasas
## peso   1.0000000 0.2400133 0.2652935
## edad   0.2400133 1.0000000 0.8373534
## grasas 0.2652935 0.8373534 1.0000000
#recta de minimos 
#cuadrados 
regresion = lm(grasas ~ edad, data=grasas)
summary(regresion)
## 
## Call:
## lm(formula = grasas ~ edad, data = grasas)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -63.478 -26.816  -3.854  28.315  90.881 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 102.5751    29.6376   3.461  0.00212 ** 
## edad          5.3207     0.7243   7.346 1.79e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 43.46 on 23 degrees of freedom
## Multiple R-squared:  0.7012, Adjusted R-squared:  0.6882 
## F-statistic: 53.96 on 1 and 23 DF,  p-value: 1.794e-07
#Grafico del ajuste lineal
plot (grasas$edad, grasas$grasas, xlab="Edad", ylab = "Grasas")
abline(regresion)

#calculo de predicciones
nuevas.edades = data.frame(edad=seq(30,50))
predict(regresion, nuevas.edades)
##        1        2        3        4        5        6        7        8 
## 262.1954 267.5161 272.8368 278.1575 283.4781 288.7988 294.1195 299.4402 
##        9       10       11       12       13       14       15       16 
## 304.7608 310.0815 315.4022 320.7229 326.0435 331.3642 336.6849 342.0056 
##       17       18       19       20       21 
## 347.3263 352.6469 357.9676 363.2883 368.6090
#intervalo de confianza
confint(regresion)
##                 2.5 %     97.5 %
## (Intercept) 41.265155 163.885130
## edad         3.822367   6.818986