This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.

Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.

plot(cars)

Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.

When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Ctrl+Shift+K to preview the HTML file).

The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.

grasas <- read.table('http://verso.mat.uam.es/~joser.berrendero/datos/EdadPesoGrasas.txt', header = TRUE)
names(grasas)
## [1] "peso"   "edad"   "grasas"
pairs(grasas)

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
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
plot(grasas$edad, grasas$grasas, xlab='Edad', ylab='Grasas')
abline(regresion)

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
confint(regresion)
##                 2.5 %     97.5 %
## (Intercept) 41.265155 163.885130
## edad         3.822367   6.818986
confint(regresion, level = 0.90)
##                   5 %       95 %
## (Intercept) 51.780153 153.370132
## edad         4.079335   6.562018
nuevas.edades <- data.frame(edad = seq(20, 60))

Grafico de dispersion y recta

plot(grasas$edad, grasas$grasas, xlab='Edad', ylab='Grasas')
abline(regresion)

Intervalos de confianza de la respuesta media:

ic es una matriz con tres columnas: la primera es la prediccion, las otras dos son los extremos del intervalo

# modelo y newdata (pueden estar en un chunk previo)
regresion <- lm(grasas ~ edad, data = grasas)
nuevas.edades <- data.frame(
  edad = seq(min(grasas$edad, na.rm = TRUE),
             max(grasas$edad, na.rm = TRUE),
             length.out = 100)
)
ic <- predict(regresion, newdata = nuevas.edades, interval = "confidence")

# --- TODO en el MISMO chunk ---
plot(grasas$edad, grasas$grasas,
     xlab = "Edad", ylab = "Grasas", pch = 19)

lines(nuevas.edades$edad, ic[, "fit"])
lines(nuevas.edades$edad, ic[, "lwr"], lty = 2)
lines(nuevas.edades$edad, ic[, "upr"], lty = 2)

# Tabla ANOVA (solo texto, no figura)
anova(regresion)
# ===== FIGURA 2 =====
op <- par(mfrow = c(1, 2))   # 1 fila, 2 columnas

residuos <- rstandard(regresion)
ajustados <- fitted(regresion)

plot(ajustados, residuos,
     xlab = "Valores ajustados", ylab = "Residuos estandarizados",
     pch = 19)
abline(h = 0, lty = 2)

qqnorm(residuos, pch = 19)
qqline(residuos)

par(op)  # restaura parámetros gráficos

Intervalos de prediccion

# Figura 1: dispersión + ajuste + bandas
regresion <- lm(grasas ~ edad, data = grasas)

nuevas.edades <- data.frame(
  edad = seq(min(grasas$edad, na.rm = TRUE),
             max(grasas$edad, na.rm = TRUE),
             length.out = 100)
)

ic <- predict(regresion, newdata = nuevas.edades, interval = "prediction")

plot(grasas$edad, grasas$grasas,
     xlab = "Edad", ylab = "Grasas", pch = 19)

lines(nuevas.edades$edad, ic[, "fit"])
lines(nuevas.edades$edad, ic[, "lwr"], lty = 2, col = 'red')  # banda inferior
lines(nuevas.edades$edad, ic[, "upr"], lty = 2, col = 'red')  # banda superior

# Tabla ANOVA (solo texto, no figura)
anova(regresion)
# ===== FIGURA 2 =====
op <- par(mfrow = c(1, 2))   # 1 fila, 2 columnas

residuos <- rstandard(regresion)
ajustados <- fitted(regresion)

plot(ajustados, residuos,
     xlab = "Valores ajustados", ylab = "Residuos estandarizados",
     pch = 19)
abline(h = 0, lty = 2)

qqnorm(residuos, pch = 19)
qqline(residuos)

par(op)  # restaura parámetros gráficos

Variable regresora (dieño fijo) y parámetros

x = seq(1,10)
beta0 <- 0
beta1 <- 1
sigma <- 0.3

Genera la variable respuesta

y <- beta0 + beta1*x + rnorm(length(x), sd=sigma)

Ajusta el modelo

reg <- lm(y~x)

Extrae el valor de la pendiente estimada

coefficients(reg)[2]
##        x 
## 1.031097

Resume el ajuste

summary(reg)
## 
## Call:
## lm(formula = y ~ x)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.42449 -0.05907  0.01479  0.11455  0.31333 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -0.01552    0.16657  -0.093    0.928    
## x            1.03110    0.02684  38.410 2.32e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.2438 on 8 degrees of freedom
## Multiple R-squared:  0.9946, Adjusted R-squared:  0.9939 
## F-statistic:  1475 on 1 and 8 DF,  p-value: 2.319e-10