TP6 - Modelos lineales

En este trabajo vamos a poner en práctica los conceptos aprendidos sobre regresión lineal, regresión múltiple e inferencia.

# install.packages("tidyverse")
library(tidyverse)

1 - Regresión lineal

Utilizando el dataset starbucks del paquete {openintro}, ajuste un modelo lineal para estimar las calorías de un producto calories utilizando como predictor los gramos de grasa fat.

Visualice los resultados y escribir la ecuación del modelo.

Visualice los residuos.

Estime las calorías de un nuevo producto con 19 gramos de grasa.

¿Qué pasa cuando hay 0 gramos de grasa?

¿Cómo varía la pendiente si ajustamos el mismo modelos pero utilizando carb, fiber o protein?

Volvamos al modelo con fat. si vemos la salida de summary(), ¿Qué puede decir del \(R^2\) y del \(p\)?

Agreguede a uno cada uno de estos outliers y estude cómo afectan a la pendiente.

  • Outlier 1: fat=25, calories=150
  • Outlier 2: fat=50, calories=150
  • Outlier 3: fat=50, calories=700
# Ajuste
library(openintro)
model_starbucks <- starbucks %>% 
  lm(calories ~ fat, .)
model_starbucks
#> 
#> Call:
#> lm(formula = calories ~ fat, data = .)
#> 
#> Coefficients:
#> (Intercept)          fat  
#>      183.73        11.27

starbucks %>% ggplot(aes(x = fat,
                         y = calories)) +
  geom_point() +
  geom_smooth(method = lm, se = FALSE)


# Residuos
cbind(starbucks, resid(model_starbucks)) %>%
  rename("residuals" = "resid(model_starbucks)") %>%
  ggplot(aes(x = fat,
             y = residuals)) +
  geom_point() +
  geom_hline(yintercept = 0, color= "#ED6A5A", linetype = "dashed")

         
# Prediccion               
predict.lm(model_starbucks, tibble(fat = 19))                         
#>        1 
#> 397.7975
predict.lm(model_starbucks, tibble(fat = 0))
#>        1 
#> 183.7338

# Modelos alternativos
model_starbucks_carb <- starbucks %>% 
  lm(calories ~ carb, .)
model_starbucks_carb
#> 
#> Call:
#> lm(formula = calories ~ carb, data = .)
#> 
#> Coefficients:
#> (Intercept)         carb  
#>     146.020        4.297

model_starbucks_protein <- starbucks %>% 
  lm(calories ~ protein, .)
model_starbucks_protein
#> 
#> Call:
#> lm(formula = calories ~ protein, data = .)
#> 
#> Coefficients:
#> (Intercept)      protein  
#>     288.090        5.352

model_starbucks_fiber <- starbucks %>% 
  lm(calories ~ fiber, .)
model_starbucks_fiber
#> 
#> Call:
#> lm(formula = calories ~ fiber, data = .)
#> 
#> Coefficients:
#> (Intercept)        fiber  
#>         310           13

# Summary
summary(model_starbucks)
#> 
#> Call:
#> lm(formula = calories ~ fat, data = .)
#> 
#> Residuals:
#>      Min       1Q   Median       3Q      Max 
#> -132.599  -44.130    3.469   54.868  126.134 
#> 
#> Coefficients:
#>             Estimate Std. Error t value Pr(>|t|)    
#> (Intercept)  183.734     17.277   10.63  < 2e-16 ***
#> fat           11.267      1.117   10.09 1.32e-15 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 69.1 on 75 degrees of freedom
#> Multiple R-squared:  0.5756, Adjusted R-squared:  0.5699 
#> F-statistic: 101.7 on 1 and 75 DF,  p-value: 1.32e-15

# Outliers
starbucks_out1 <- starbucks %>%
  dplyr::select(c("calories", "fat")) %>%
  rbind(tibble(fat = 25, calories = 150))

starbucks_out1 %>%
  ggplot(aes(x = fat,
             y = calories)) +
  geom_point() +
  geom_smooth(method = lm, se = FALSE)

  
model_starbucks_out1 <- starbucks_out1 %>% 
  lm(calories ~ fat, .)
model_starbucks_out1
#> 
#> Call:
#> lm(formula = calories ~ fat, data = .)
#> 
#> Coefficients:
#> (Intercept)          fat  
#>      192.00        10.38

starbucks_out2 <- starbucks %>%
  dplyr::select(c("calories", "fat")) %>%
  rbind(tibble(fat = 50, calories = 150))

starbucks_out2 %>%
  ggplot(aes(x = fat,
             y = calories)) +
  geom_point() +
  geom_smooth(method = lm, se = FALSE)

  
model_starbucks_out2 <- starbucks_out2 %>% 
  lm(calories ~ fat, .)
model_starbucks_out2
#> 
#> Call:
#> lm(formula = calories ~ fat, data = .)
#> 
#> Coefficients:
#> (Intercept)          fat  
#>     235.411        7.097

starbucks_out3 <- starbucks %>%
  dplyr::select(c("calories", "fat")) %>%
  rbind(tibble(fat = 50, calories = 700))

starbucks_out3 %>%
  ggplot(aes(x = fat,
             y = calories)) +
  geom_point() +
  geom_smooth(method = lm, se = FALSE)

  
model_starbucks_out3 <- starbucks_out3 %>% 
  lm(calories ~ fat, .)
model_starbucks_out3
#> 
#> Call:
#> lm(formula = calories ~ fat, data = .)
#> 
#> Coefficients:
#> (Intercept)          fat  
#>      187.81        10.94

2 - Regesión multiple

Utilizando el dataset penguinsdel paquete {palmerpenguins}, ajuste un modelo lineal para estimar el peso de los pinguinos (en g) body_mass_g a partir de la variable continua ancho de pico (en mm) bill_depth_mm y la variable discreta especie species,sin interacción.

Escriba la ecuación y estime el peso de un pinguino Chinstrap de ancho de pico 18 mm.

Más allá de los parámetros ¿Qué puede decir del efecto conjunto de la variable discreta species?

Agerguemos la interacción al modelo ¿Es significativamente más “explicativo”? Volvamos a escribir la ecuación y a estimar el peso para un pinguino Chinstrap de ancho de pico 18 mm.

library(palmerpenguins)

# Modelo 
model_penguins <- penguins %>%
  drop_na() %>%
  lm(body_mass_g ~ bill_depth_mm + species, .)

summary(model_penguins)
#> 
#> Call:
#> lm(formula = body_mass_g ~ bill_depth_mm + species, data = .)
#> 
#> Residuals:
#>     Min      1Q  Median      3Q     Max 
#> -846.01 -261.75  -30.43  232.36 1185.55 
#> 
#> Coefficients:
#>                   Estimate Std. Error t value Pr(>|t|)    
#> (Intercept)      -1000.846    324.968  -3.080  0.00225 ** 
#> bill_depth_mm      256.551     17.637  14.546  < 2e-16 ***
#> speciesChinstrap     8.111     52.874   0.153  0.87817    
#> speciesGentoo     2245.878     73.956  30.368  < 2e-16 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 360 on 329 degrees of freedom
#> Multiple R-squared:  0.8019, Adjusted R-squared:  0.8001 
#> F-statistic: 443.9 on 3 and 329 DF,  p-value: < 2.2e-16

predict.lm(model_penguins, tibble(bill_depth_mm = 18, species = "Chinstrap"))
#>        1 
#> 3625.186

# Peso = -1000.846 + 256.551 * bill_depth_mm + 8.111 * speciesChinstrap + 2245.878 * speciesGentoo

library(car)
Anova(model_penguins, type = 3)
#> Anova Table (Type III tests)
#> 
#> Response: body_mass_g
#>                  Sum Sq  Df  F value    Pr(>F)    
#> (Intercept)     1229481   1   9.4853  0.002246 ** 
#> bill_depth_mm  27424833   1 211.5805 < 2.2e-16 ***
#> species       124655460   2 480.8538 < 2.2e-16 ***
#> Residuals      42644614 329                       
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

# Modelo con interacción
model_penguins_interaction <- penguins %>%
  drop_na() %>%
  lm(body_mass_g ~ bill_depth_mm * species, .)

summary(model_penguins_interaction)
#> 
#> Call:
#> lm(formula = body_mass_g ~ bill_depth_mm * species, data = .)
#> 
#> Residuals:
#>     Min      1Q  Median      3Q     Max 
#> -836.31 -255.82  -30.44  229.58 1156.89 
#> 
#> Coefficients:
#>                                Estimate Std. Error t value Pr(>|t|)    
#> (Intercept)                     -297.38     441.69  -0.673 0.501242    
#> bill_depth_mm                    218.21      24.02   9.084  < 2e-16 ***
#> speciesChinstrap                 261.16     828.02   0.315 0.752654    
#> speciesGentoo                   -124.43     663.33  -0.188 0.851314    
#> bill_depth_mm:speciesChinstrap   -13.58      44.91  -0.302 0.762491    
#> bill_depth_mm:speciesGentoo      149.49      40.76   3.668 0.000286 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 352.7 on 327 degrees of freedom
#> Multiple R-squared:  0.811,  Adjusted R-squared:  0.8081 
#> F-statistic: 280.7 on 5 and 327 DF,  p-value: < 2.2e-16

# Comparación de modelos
anova(model_penguins, model_penguins_interaction)
#> Analysis of Variance Table
#> 
#> Model 1: body_mass_g ~ bill_depth_mm + species
#> Model 2: body_mass_g ~ bill_depth_mm * species
#>   Res.Df      RSS Df Sum of Sq      F    Pr(>F)    
#> 1    329 42644614                                  
#> 2    327 40677576  2   1967038 7.9063 0.0004434 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

# Nueva ecuación y predicción
# Peso = -297.38 + 218.21 * bill_depth_mm + 261.16 * speciesChinstrap - 124.43 * speciesGentoo - 13.58 * bill_depth_mm X speciesChinstrap + 149.49 *bill_depth_mm X speciesGentoo

predict.lm(model_penguins_interaction, tibble(bill_depth_mm = 18, species = "Chinstrap"))
#>        1 
#> 3647.025

3 - Intervalos de confianza

Utilizando la siguiente ecuación:

\[ Peso = 500 + 1 \times Dientes \] Genere una muestra de 2000 datos de la siguiente forma:

set.seed(4)
data_dientes <- tibble(Dientes = round(rnorm(2000) * 10+50),
                       Peso = (500 +  1 * Dientes) + rnorm(2000) * 30)

tome 200 muestras aleatorias de 20 individuos y calcule cuántas veces el valor real de la pendiente es contenido por el intervalo de confianza.

¿Qué pasa si en lugar de ser muestras de 20 individuos son de 50?

# Modelo con la muestra
model_dientes <- data_dientes %>%
  lm(Peso ~ Dientes, .)

library(parameters)
model_parameters(model_dientes)
#> Parameter   | Coefficient |   SE |           95% CI | t(1998) |      p
#> ----------------------------------------------------------------------
#> (Intercept) |      499.57 | 3.38 | [492.93, 506.21] |  147.59 | < .001
#> Dientes     |        1.01 | 0.07 | [  0.88,   1.14] |   15.24 | < .001

# 200 realizaciones d eun experimento tomando 20 datos
set.seed(4)
CI_in <- tibble(index = 1:200,
                low = 0,
                high = 0,
                isin = FALSE)
for (i in 1:200) {
  data_sample <- data_dientes %>% sample_n(size = 20)  
  model_sample <- lm(Peso ~ Dientes, data_sample)
  CI_in$low[i] <- model_parameters(model_sample)$CI_low[2]
  CI_in$high[i] <- model_parameters(model_sample)$CI_high[2]
  CI_in$isin[i] <- between(1, model_parameters(model_sample)$CI_low[2], model_parameters(model_sample)$CI_high[2])
  print(paste0(i, " low:", CI_in$low[i], " high:", CI_in$high[i], "IN:", CI_in$isin[i]))
}
#> [1] "1 low:-1.28323910777551 high:2.05968080671899IN:TRUE"
#> [1] "2 low:0.00121108649386614 high:2.55807856111787IN:TRUE"
#> [1] "3 low:0.525492016385851 high:3.2787872311555IN:TRUE"
#> [1] "4 low:-0.622896042884646 high:1.63325356293779IN:TRUE"
#> [1] "5 low:-0.951702886266439 high:1.97047487403205IN:TRUE"
#> [1] "6 low:-0.707283834627705 high:2.16225341482306IN:TRUE"
#> [1] "7 low:-0.991299767055287 high:0.977037965578505IN:FALSE"
#> [1] "8 low:-0.217386152558572 high:2.89942816633604IN:TRUE"
#> [1] "9 low:0.527450342741732 high:4.37820111349492IN:TRUE"
#> [1] "10 low:0.277942247092259 high:2.06880864017461IN:TRUE"
#> [1] "11 low:-1.80508537995557 high:2.2116873684769IN:TRUE"
#> [1] "12 low:-0.919867268611145 high:3.20975363691011IN:TRUE"
#> [1] "13 low:-0.970306252268397 high:2.35514715340217IN:TRUE"
#> [1] "14 low:-0.367959833527602 high:2.84536305371281IN:TRUE"
#> [1] "15 low:0.903284009472214 high:2.98021530420217IN:TRUE"
#> [1] "16 low:-0.174232774223921 high:3.19098907370989IN:TRUE"
#> [1] "17 low:0.349748918405876 high:2.99463658042874IN:TRUE"
#> [1] "18 low:-0.997927421922054 high:2.58798186547246IN:TRUE"
#> [1] "19 low:-0.522825990455697 high:2.73346968933733IN:TRUE"
#> [1] "20 low:-1.06977703372365 high:3.24385400706083IN:TRUE"
#> [1] "21 low:-0.3068098598101 high:2.53111225098238IN:TRUE"
#> [1] "22 low:-1.75113976505249 high:2.52714212652515IN:TRUE"
#> [1] "23 low:-1.67077943314397 high:1.46882045291433IN:TRUE"
#> [1] "24 low:-0.226895530841815 high:1.12108303611647IN:TRUE"
#> [1] "25 low:-0.0430272282392055 high:2.30352415610889IN:TRUE"
#> [1] "26 low:-0.195701263246446 high:1.82714675557115IN:TRUE"
#> [1] "27 low:-0.456565233320516 high:1.69894010895704IN:TRUE"
#> [1] "28 low:-0.297566066008231 high:2.9997582212928IN:TRUE"
#> [1] "29 low:-0.986964945216996 high:2.40823291021373IN:TRUE"
#> [1] "30 low:-1.04520996787802 high:2.8236145726933IN:TRUE"
#> [1] "31 low:-0.446908872357846 high:2.1073972169426IN:TRUE"
#> [1] "32 low:0.1511419643719 high:1.9336253711684IN:TRUE"
#> [1] "33 low:-1.22136866909243 high:1.68635146937675IN:TRUE"
#> [1] "34 low:-1.37345499683686 high:2.9560814463049IN:TRUE"
#> [1] "35 low:-0.886996570464843 high:1.83379443396991IN:TRUE"
#> [1] "36 low:-0.0739821611730354 high:2.12587553520641IN:TRUE"
#> [1] "37 low:-0.192832272784451 high:2.45756389348424IN:TRUE"
#> [1] "38 low:-0.805483762085496 high:2.11006426369111IN:TRUE"
#> [1] "39 low:-0.238485549500451 high:2.390252872513IN:TRUE"
#> [1] "40 low:-1.21182721057894 high:1.42920780961374IN:TRUE"
#> [1] "41 low:-2.20199776908393 high:0.78835481926679IN:FALSE"
#> [1] "42 low:-0.420668865737645 high:2.94783844787055IN:TRUE"
#> [1] "43 low:-0.313936803854389 high:2.82726939362368IN:TRUE"
#> [1] "44 low:-0.0500221131468974 high:3.55999992821683IN:TRUE"
#> [1] "45 low:-0.12619169019569 high:3.18388861430939IN:TRUE"
#> [1] "46 low:0.126388543095267 high:2.79680361074023IN:TRUE"
#> [1] "47 low:1.25534511928936 high:5.1817372412451IN:FALSE"
#> [1] "48 low:-0.267257079504212 high:1.8915264070614IN:TRUE"
#> [1] "49 low:-0.893907411124945 high:3.09008270953445IN:TRUE"
#> [1] "50 low:-1.32457936860321 high:2.17625905317567IN:TRUE"
#> [1] "51 low:-0.322002224741068 high:1.35098598706473IN:TRUE"
#> [1] "52 low:-0.629731377522749 high:2.38826119494431IN:TRUE"
#> [1] "53 low:-0.659705660091786 high:1.68867904769602IN:TRUE"
#> [1] "54 low:0.409165179728522 high:3.39819450835677IN:TRUE"
#> [1] "55 low:0.714308231931449 high:2.59206668193049IN:TRUE"
#> [1] "56 low:-1.16169172749075 high:1.01704239334478IN:TRUE"
#> [1] "57 low:0.111358636539074 high:2.10022138002951IN:TRUE"
#> [1] "58 low:-0.928864600272609 high:3.78202993584296IN:TRUE"
#> [1] "59 low:-0.144666319645605 high:2.88533670406717IN:TRUE"
#> [1] "60 low:-0.540934055866978 high:3.16090898893476IN:TRUE"
#> [1] "61 low:-0.903345173300388 high:1.4268646373812IN:TRUE"
#> [1] "62 low:-1.18520891617811 high:2.06954377846722IN:TRUE"
#> [1] "63 low:-1.4627582745631 high:2.14606431308363IN:TRUE"
#> [1] "64 low:-0.0146615015524393 high:1.98054724378362IN:TRUE"
#> [1] "65 low:-0.0904020423877943 high:4.38135169135744IN:TRUE"
#> [1] "66 low:-0.239889492316522 high:2.6731877780587IN:TRUE"
#> [1] "67 low:0.115043547680075 high:3.85108996441275IN:TRUE"
#> [1] "68 low:-0.814735760696493 high:1.37232369898896IN:TRUE"
#> [1] "69 low:-2.71097502821648 high:2.09071619977885IN:TRUE"
#> [1] "70 low:-0.571929762749702 high:2.9896740754191IN:TRUE"
#> [1] "71 low:-1.06002152619155 high:2.69988266035192IN:TRUE"
#> [1] "72 low:1.00164004768739 high:3.1648563659976IN:FALSE"
#> [1] "73 low:0.223225375664115 high:2.66477987041481IN:TRUE"
#> [1] "74 low:0.827993543018158 high:3.11763468146527IN:TRUE"
#> [1] "75 low:-0.95194779264066 high:1.82579794558454IN:TRUE"
#> [1] "76 low:-0.163897833846263 high:2.96511472682695IN:TRUE"
#> [1] "77 low:0.127825150217428 high:2.00332551848921IN:TRUE"
#> [1] "78 low:0.443679210201847 high:2.9574562443056IN:TRUE"
#> [1] "79 low:-1.26256647468419 high:2.57338382707884IN:TRUE"
#> [1] "80 low:-1.49796830900879 high:2.04488024182384IN:TRUE"
#> [1] "81 low:-0.214178997400807 high:1.97349051869919IN:TRUE"
#> [1] "82 low:-0.454503243817512 high:2.11311319743708IN:TRUE"
#> [1] "83 low:-0.555276305536741 high:2.66044469601548IN:TRUE"
#> [1] "84 low:-0.192983729049395 high:2.11493239902034IN:TRUE"
#> [1] "85 low:0.399141055007344 high:2.78605243523522IN:TRUE"
#> [1] "86 low:0.21416676947718 high:3.13858518280704IN:TRUE"
#> [1] "87 low:-1.18923731311511 high:2.48553115657316IN:TRUE"
#> [1] "88 low:0.571718780138519 high:2.67530891065982IN:TRUE"
#> [1] "89 low:0.386217157518459 high:4.12696487130784IN:TRUE"
#> [1] "90 low:1.02012681077118 high:4.11365638141365IN:FALSE"
#> [1] "91 low:-0.027521424656167 high:2.82483464650766IN:TRUE"
#> [1] "92 low:-0.331916560270549 high:2.89990470821418IN:TRUE"
#> [1] "93 low:-0.16900790091133 high:1.67348592637431IN:TRUE"
#> [1] "94 low:0.168415874071247 high:2.42855502481152IN:TRUE"
#> [1] "95 low:-1.07172923118474 high:2.30391578293047IN:TRUE"
#> [1] "96 low:0.344819574887033 high:3.49597366890641IN:TRUE"
#> [1] "97 low:0.747875347586265 high:3.00629292979281IN:TRUE"
#> [1] "98 low:-1.06099477130469 high:1.79595649978531IN:TRUE"
#> [1] "99 low:-0.701692084042028 high:2.45612689204458IN:TRUE"
#> [1] "100 low:-1.37628068935712 high:3.17354895603559IN:TRUE"
#> [1] "101 low:-0.895466440966625 high:1.29379760742452IN:TRUE"
#> [1] "102 low:-0.0641569676503904 high:2.84552011728125IN:TRUE"
#> [1] "103 low:-0.846119852994073 high:2.01493964388481IN:TRUE"
#> [1] "104 low:-0.193466757402678 high:2.37219216097365IN:TRUE"
#> [1] "105 low:-0.752729171770839 high:1.1146837671082IN:TRUE"
#> [1] "106 low:-0.4687114855228 high:2.19408162782165IN:TRUE"
#> [1] "107 low:-0.203423381792164 high:4.31842529984595IN:TRUE"
#> [1] "108 low:-0.142832339145317 high:2.22724794897345IN:TRUE"
#> [1] "109 low:-1.68003500686954 high:2.70561285762894IN:TRUE"
#> [1] "110 low:0.00978615682132156 high:2.05931317123915IN:TRUE"
#> [1] "111 low:0.00974264321847018 high:2.34717053068392IN:TRUE"
#> [1] "112 low:-1.52866442068582 high:2.47860651246624IN:TRUE"
#> [1] "113 low:-0.825816653109131 high:2.91771109948236IN:TRUE"
#> [1] "114 low:-0.85832801655133 high:2.16814539959386IN:TRUE"
#> [1] "115 low:-0.467546091144022 high:1.52651624205474IN:TRUE"
#> [1] "116 low:-0.170283574270601 high:2.67424715890335IN:TRUE"
#> [1] "117 low:-0.745352809769801 high:2.46227767083435IN:TRUE"
#> [1] "118 low:0.924224381733099 high:4.45881949914167IN:TRUE"
#> [1] "119 low:-1.58799285351826 high:1.85655870186379IN:TRUE"
#> [1] "120 low:0.515095835383377 high:3.88349047711042IN:TRUE"
#> [1] "121 low:-1.00872629384075 high:3.4919685842345IN:TRUE"
#> [1] "122 low:2.13364726666582 high:4.30081684747014IN:FALSE"
#> [1] "123 low:-0.550345751541489 high:2.03075408700199IN:TRUE"
#> [1] "124 low:0.179819909082877 high:2.21764770934568IN:TRUE"
#> [1] "125 low:-2.41045765294439 high:0.712731155573203IN:FALSE"
#> [1] "126 low:0.203264781992615 high:2.88057822823653IN:TRUE"
#> [1] "127 low:-0.650427070640766 high:2.02794638339658IN:TRUE"
#> [1] "128 low:-1.72132726129957 high:1.76741913000484IN:TRUE"
#> [1] "129 low:0.148578348148935 high:2.53670250638417IN:TRUE"
#> [1] "130 low:0.161801559824335 high:3.26217084466224IN:TRUE"
#> [1] "131 low:0.0522933430085355 high:3.2302155115747IN:TRUE"
#> [1] "132 low:-0.16055268400607 high:2.57448899132824IN:TRUE"
#> [1] "133 low:-0.57622920134457 high:2.48437701594343IN:TRUE"
#> [1] "134 low:-1.58560843437242 high:2.77292735194283IN:TRUE"
#> [1] "135 low:0.107321195950554 high:2.66220712384537IN:TRUE"
#> [1] "136 low:0.448947054246079 high:2.83020827264107IN:TRUE"
#> [1] "137 low:-0.233833828496991 high:2.44285498093207IN:TRUE"
#> [1] "138 low:-0.718837078419193 high:3.02021782221532IN:TRUE"
#> [1] "139 low:-0.0561556405429022 high:2.40207858976846IN:TRUE"
#> [1] "140 low:-0.581218056929228 high:2.65818824328938IN:TRUE"
#> [1] "141 low:0.51952931400707 high:4.53871379813401IN:TRUE"
#> [1] "142 low:-1.30168842547231 high:2.30885966566331IN:TRUE"
#> [1] "143 low:-0.553815688669257 high:2.16436577197574IN:TRUE"
#> [1] "144 low:-1.14920699490941 high:1.98688799099402IN:TRUE"
#> [1] "145 low:0.090596563646711 high:2.50035606374901IN:TRUE"
#> [1] "146 low:-0.329027492892702 high:2.84165392272824IN:TRUE"
#> [1] "147 low:-1.95043930759618 high:1.61967955469522IN:TRUE"
#> [1] "148 low:-0.241654778171127 high:1.83418854436731IN:TRUE"
#> [1] "149 low:-0.224928832007362 high:4.6703403913379IN:TRUE"
#> [1] "150 low:-0.609143877925855 high:2.45435529871685IN:TRUE"
#> [1] "151 low:0.0389712077760445 high:3.67204121245612IN:TRUE"
#> [1] "152 low:-0.277060692846816 high:3.64761964670323IN:TRUE"
#> [1] "153 low:-1.46722439670868 high:2.41088729269212IN:TRUE"
#> [1] "154 low:-0.397738605379594 high:4.3672494646532IN:TRUE"
#> [1] "155 low:0.129824051690858 high:1.9344827558959IN:TRUE"
#> [1] "156 low:-1.43192669510395 high:1.60954783559505IN:TRUE"
#> [1] "157 low:-1.39643279948923 high:1.64342428110714IN:TRUE"
#> [1] "158 low:-0.072210797722764 high:2.82899708836385IN:TRUE"
#> [1] "159 low:0.598894648184033 high:3.82384150031651IN:TRUE"
#> [1] "160 low:-0.979379632244419 high:1.55010011220072IN:TRUE"
#> [1] "161 low:-0.0920040989668878 high:2.22772462821348IN:TRUE"
#> [1] "162 low:-0.277565415702554 high:3.01887178654619IN:TRUE"
#> [1] "163 low:-1.52377690029973 high:1.34850665369814IN:TRUE"
#> [1] "164 low:-0.54369478221826 high:2.1121042696854IN:TRUE"
#> [1] "165 low:0.35702433345492 high:3.64981775319926IN:TRUE"
#> [1] "166 low:-0.717227383714185 high:2.05207181129141IN:TRUE"
#> [1] "167 low:0.128580057406573 high:2.10777997696452IN:TRUE"
#> [1] "168 low:-0.104078278663423 high:3.53503109153844IN:TRUE"
#> [1] "169 low:-0.198470686378376 high:3.17914186537804IN:TRUE"
#> [1] "170 low:-0.683834717609935 high:1.6013869578734IN:TRUE"
#> [1] "171 low:-2.46643466282813 high:2.66479324601329IN:TRUE"
#> [1] "172 low:-0.0280561459442759 high:2.51463749538854IN:TRUE"
#> [1] "173 low:0.728915181547325 high:3.6318046006868IN:TRUE"
#> [1] "174 low:-1.17072968762781 high:1.76731442337276IN:TRUE"
#> [1] "175 low:-0.576815039801183 high:1.56988372106925IN:TRUE"
#> [1] "176 low:-1.90239555704758 high:1.24380933857961IN:TRUE"
#> [1] "177 low:0.0439961741221506 high:2.51337440929056IN:TRUE"
#> [1] "178 low:-0.79792221483101 high:2.57404464915896IN:TRUE"
#> [1] "179 low:-0.246482678253538 high:1.31410802717698IN:TRUE"
#> [1] "180 low:1.18397275676133 high:4.38399018929357IN:FALSE"
#> [1] "181 low:0.132655718056496 high:3.2756700331475IN:TRUE"
#> [1] "182 low:-0.345459459193765 high:3.17314031388814IN:TRUE"
#> [1] "183 low:0.577483666306677 high:3.1169990994013IN:TRUE"
#> [1] "184 low:-0.957202902566743 high:1.73106559735054IN:TRUE"
#> [1] "185 low:-0.194637372359381 high:1.84617688038572IN:TRUE"
#> [1] "186 low:0.871960123904534 high:3.00000336314648IN:TRUE"
#> [1] "187 low:-1.27369766190994 high:1.62332005635964IN:TRUE"
#> [1] "188 low:0.604718869482104 high:2.76452597574772IN:TRUE"
#> [1] "189 low:0.106395266415348 high:2.86330515619407IN:TRUE"
#> [1] "190 low:-1.42819867314007 high:1.85639310815725IN:TRUE"
#> [1] "191 low:-0.427668371943012 high:1.79954601462111IN:TRUE"
#> [1] "192 low:-0.920980211367369 high:1.47092492497066IN:TRUE"
#> [1] "193 low:-2.30452595375385 high:0.929417319385889IN:FALSE"
#> [1] "194 low:0.991953225817252 high:3.16147680773337IN:TRUE"
#> [1] "195 low:0.227398319396457 high:3.19232544761469IN:TRUE"
#> [1] "196 low:-0.893377439988648 high:1.43080953151572IN:TRUE"
#> [1] "197 low:-0.0608217799336284 high:2.32994340561012IN:TRUE"
#> [1] "198 low:0.153264708782662 high:3.90806998872155IN:TRUE"
#> [1] "199 low:-1.04987687570109 high:0.930635764268346IN:FALSE"
#> [1] "200 low:-0.189982254952688 high:2.69244007944863IN:TRUE"
# Porcentaje de esos experimentos en los que el CI contiene a 1
sum(CI_in$isin)/200*100
#> [1] 95

# 200 realizaciones d eun experimento tomando 50 datos
set.seed(14)
CI_in_50 <- tibble(index = 1:200,
                low = 0,
                high = 0,
                isin = FALSE)
for (i in 1:200) {
  data_sample <- data_dientes %>% sample_n(size = 50)  
  model_sample <- lm(Peso ~ Dientes, data_sample)
  CI_in_50$low[i] <- model_parameters(model_sample)$CI_low[2]
  CI_in_50$high[i] <- model_parameters(model_sample)$CI_high[2]
  CI_in_50$isin[i] <- between(1, model_parameters(model_sample)$CI_low[2], model_parameters(model_sample)$CI_high[2])
  print(paste0(i, " low:", CI_in_50$low[i], " high:", CI_in_50$high[i], "IN:", CI_in_50$isin[i]))
}
#> [1] "1 low:-0.114659733982348 high:1.74382874727615IN:TRUE"
#> [1] "2 low:0.38169024596116 high:1.95761924939997IN:TRUE"
#> [1] "3 low:-0.114301636818367 high:1.77594715371785IN:TRUE"
#> [1] "4 low:0.260594524519047 high:2.05807706355785IN:TRUE"
#> [1] "5 low:-0.163013511661343 high:2.11094203989838IN:TRUE"
#> [1] "6 low:0.128570076235217 high:1.77000923282342IN:TRUE"
#> [1] "7 low:0.681970001688885 high:2.33716730417448IN:TRUE"
#> [1] "8 low:-0.450488349904784 high:1.49521475097871IN:TRUE"
#> [1] "9 low:0.00404420098188418 high:2.03171991469106IN:TRUE"
#> [1] "10 low:0.255328188763775 high:1.97571194452182IN:TRUE"
#> [1] "11 low:-0.413776359269664 high:1.14513563175184IN:TRUE"
#> [1] "12 low:-0.27667588163143 high:1.49387076131175IN:TRUE"
#> [1] "13 low:0.325038095658522 high:1.82480478815277IN:TRUE"
#> [1] "14 low:0.0807612187006986 high:1.88043759053712IN:TRUE"
#> [1] "15 low:-0.885005738079036 high:1.46630721946445IN:TRUE"
#> [1] "16 low:-0.407174814648637 high:1.37272158857864IN:TRUE"
#> [1] "17 low:0.361561638006125 high:1.96236023793482IN:TRUE"
#> [1] "18 low:1.20338646285795 high:2.68553282529088IN:FALSE"
#> [1] "19 low:-0.640794617475959 high:1.12276930332406IN:TRUE"
#> [1] "20 low:0.18087741238392 high:1.68441189823654IN:TRUE"
#> [1] "21 low:0.682901451064683 high:2.56941739849424IN:TRUE"
#> [1] "22 low:-0.320520561699388 high:1.34131974087409IN:TRUE"
#> [1] "23 low:1.13467094371426 high:2.44866890248497IN:FALSE"
#> [1] "24 low:0.809900230039209 high:2.30582231275445IN:TRUE"
#> [1] "25 low:0.515060690844832 high:1.89370963959479IN:TRUE"
#> [1] "26 low:0.835556989622599 high:2.06630877079744IN:TRUE"
#> [1] "27 low:-0.280816138853137 high:1.5538206777436IN:TRUE"
#> [1] "28 low:-0.670468207803513 high:1.66326991846305IN:TRUE"
#> [1] "29 low:0.10919628280384 high:2.52788877712305IN:TRUE"
#> [1] "30 low:0.923894534201471 high:2.817798789128IN:TRUE"
#> [1] "31 low:0.129640381809539 high:1.97607666291088IN:TRUE"
#> [1] "32 low:-0.606643469383606 high:0.919080533301907IN:FALSE"
#> [1] "33 low:-0.041766184850216 high:2.02262857926485IN:TRUE"
#> [1] "34 low:0.550115690446327 high:2.44330646668723IN:TRUE"
#> [1] "35 low:-0.506714909321073 high:1.55340571287899IN:TRUE"
#> [1] "36 low:0.525273772307372 high:1.9574454498437IN:TRUE"
#> [1] "37 low:-0.181528098178158 high:1.89383937689009IN:TRUE"
#> [1] "38 low:0.0685010234697453 high:2.04147993396937IN:TRUE"
#> [1] "39 low:0.169815955214786 high:1.60965859435823IN:TRUE"
#> [1] "40 low:-0.543271360260816 high:1.10314649040415IN:TRUE"
#> [1] "41 low:0.511092464263166 high:3.12229826954965IN:TRUE"
#> [1] "42 low:0.185137741259482 high:1.95661525342344IN:TRUE"
#> [1] "43 low:-0.257113268336401 high:1.48733328967985IN:TRUE"
#> [1] "44 low:-0.555720141127892 high:1.23375577187655IN:TRUE"
#> [1] "45 low:1.04383398316383 high:2.89534229605636IN:FALSE"
#> [1] "46 low:0.19978780410619 high:1.901322998979IN:TRUE"
#> [1] "47 low:0.520965174156679 high:2.01728303430081IN:TRUE"
#> [1] "48 low:0.528948908615514 high:2.02267830540377IN:TRUE"
#> [1] "49 low:0.0470894704744862 high:1.81023805383865IN:TRUE"
#> [1] "50 low:-0.160789704526307 high:1.63076337256022IN:TRUE"
#> [1] "51 low:-0.372970734188929 high:1.26620169416134IN:TRUE"
#> [1] "52 low:0.12028428036506 high:1.92874673865644IN:TRUE"
#> [1] "53 low:-0.166421717611771 high:1.37289480584568IN:TRUE"
#> [1] "54 low:0.91166641650146 high:2.58801867005921IN:TRUE"
#> [1] "55 low:0.244162044811665 high:2.02700420709269IN:TRUE"
#> [1] "56 low:0.133694653920098 high:1.90256989808986IN:TRUE"
#> [1] "57 low:0.592183021522753 high:1.93364553984358IN:TRUE"
#> [1] "58 low:0.605713369505856 high:2.30572507068862IN:TRUE"
#> [1] "59 low:-0.238119517690532 high:1.42783607203058IN:TRUE"
#> [1] "60 low:0.725497378194344 high:2.17973379575794IN:TRUE"
#> [1] "61 low:0.43970035162191 high:2.22436091192558IN:TRUE"
#> [1] "62 low:0.745930619257842 high:2.8172650604904IN:TRUE"
#> [1] "63 low:-0.0932468066080567 high:1.44939033359317IN:TRUE"
#> [1] "64 low:0.340323684909866 high:2.49243346054793IN:TRUE"
#> [1] "65 low:0.0792157152617428 high:1.64421838548735IN:TRUE"
#> [1] "66 low:0.241896813313381 high:1.91437818126427IN:TRUE"
#> [1] "67 low:-0.644581672163779 high:1.28416186481583IN:TRUE"
#> [1] "68 low:0.142872044918629 high:1.92106023897643IN:TRUE"
#> [1] "69 low:0.597246483587959 high:2.22270378980564IN:TRUE"
#> [1] "70 low:0.132580249640259 high:1.99275081082521IN:TRUE"
#> [1] "71 low:-0.0123505281652538 high:1.73821505933441IN:TRUE"
#> [1] "72 low:-0.0372961573476773 high:2.22133755950591IN:TRUE"
#> [1] "73 low:0.840427021338789 high:2.34176526228458IN:TRUE"
#> [1] "74 low:0.224277493279422 high:1.88115283975473IN:TRUE"
#> [1] "75 low:0.234079521391762 high:2.26463011073602IN:TRUE"
#> [1] "76 low:0.818321283693936 high:2.48029627064039IN:TRUE"
#> [1] "77 low:-0.160892866042726 high:2.17026390830978IN:TRUE"
#> [1] "78 low:0.71754645961291 high:2.44512259891029IN:TRUE"
#> [1] "79 low:0.64390205526497 high:2.67830783853253IN:TRUE"
#> [1] "80 low:-0.218846062556957 high:1.45837321558676IN:TRUE"
#> [1] "81 low:-0.213890933890146 high:1.30688588342523IN:TRUE"
#> [1] "82 low:0.0901119823828596 high:1.74244783695996IN:TRUE"
#> [1] "83 low:0.331588078245431 high:2.03104732896363IN:TRUE"
#> [1] "84 low:0.81964914268881 high:2.57682015835622IN:TRUE"
#> [1] "85 low:0.343897037513333 high:1.8678445356983IN:TRUE"
#> [1] "86 low:0.0580087950326416 high:1.62810625653238IN:TRUE"
#> [1] "87 low:-0.0602654130344334 high:1.5046336886241IN:TRUE"
#> [1] "88 low:0.219174955373736 high:1.6430280074827IN:TRUE"
#> [1] "89 low:-0.41852337884343 high:1.36594050333821IN:TRUE"
#> [1] "90 low:-0.137182479211929 high:2.10580536581027IN:TRUE"
#> [1] "91 low:-0.130321269871184 high:1.44993811595306IN:TRUE"
#> [1] "92 low:0.44847446640596 high:1.94912221915236IN:TRUE"
#> [1] "93 low:-0.375256554132816 high:1.37125897711769IN:TRUE"
#> [1] "94 low:-0.350519460089096 high:1.24133938149304IN:TRUE"
#> [1] "95 low:-0.0858426199907861 high:1.66261673286453IN:TRUE"
#> [1] "96 low:-0.26582365737895 high:1.36243738045896IN:TRUE"
#> [1] "97 low:0.410007197056931 high:2.10053753081522IN:TRUE"
#> [1] "98 low:-0.796939394981893 high:1.3250871677135IN:TRUE"
#> [1] "99 low:0.0317536637930901 high:1.58857607079337IN:TRUE"
#> [1] "100 low:0.69768978888672 high:2.34327433632892IN:TRUE"
#> [1] "101 low:0.0311674950547232 high:1.73680467848302IN:TRUE"
#> [1] "102 low:0.0282556560201699 high:1.563262161508IN:TRUE"
#> [1] "103 low:0.242259617107821 high:2.04211205729679IN:TRUE"
#> [1] "104 low:0.66032678965548 high:2.40568499679823IN:TRUE"
#> [1] "105 low:0.1473249301809 high:1.78532907242312IN:TRUE"
#> [1] "106 low:0.153407835845542 high:2.02325423071853IN:TRUE"
#> [1] "107 low:-0.635763202455523 high:1.0825259077554IN:TRUE"
#> [1] "108 low:0.214056083737074 high:2.31817214960472IN:TRUE"
#> [1] "109 low:0.86546550839443 high:2.39829054613584IN:TRUE"
#> [1] "110 low:-0.246814657410541 high:1.60813186968283IN:TRUE"
#> [1] "111 low:-0.137258554791695 high:1.93469021701116IN:TRUE"
#> [1] "112 low:-0.00748685213526734 high:1.88887253612359IN:TRUE"
#> [1] "113 low:0.709032807204915 high:2.35925225207424IN:TRUE"
#> [1] "114 low:0.540651512839081 high:2.0183374718673IN:TRUE"
#> [1] "115 low:0.493080518679033 high:2.34288747487071IN:TRUE"
#> [1] "116 low:0.49900087853072 high:2.03461102491906IN:TRUE"
#> [1] "117 low:-0.302530025746433 high:1.56406298727624IN:TRUE"
#> [1] "118 low:-0.512367692950663 high:1.27640205421074IN:TRUE"
#> [1] "119 low:0.0590374102047631 high:1.84209916850383IN:TRUE"
#> [1] "120 low:-0.675656509196204 high:0.944692531550873IN:FALSE"
#> [1] "121 low:-0.649416842688369 high:0.824907314818101IN:FALSE"
#> [1] "122 low:-0.34357747568758 high:2.10996790035299IN:TRUE"
#> [1] "123 low:-0.105985362057607 high:1.62288643249688IN:TRUE"
#> [1] "124 low:0.183798384030836 high:1.97149126561889IN:TRUE"
#> [1] "125 low:0.195801518142156 high:1.64969822246262IN:TRUE"
#> [1] "126 low:-0.612485559670389 high:1.4246709672607IN:TRUE"
#> [1] "127 low:-0.485066017782644 high:1.27175656858104IN:TRUE"
#> [1] "128 low:0.152957836261429 high:1.96685290178727IN:TRUE"
#> [1] "129 low:0.404872189432948 high:2.28569747382074IN:TRUE"
#> [1] "130 low:0.183973754237714 high:1.53331136955315IN:TRUE"
#> [1] "131 low:0.204109858477304 high:2.26211885004223IN:TRUE"
#> [1] "132 low:-0.697206497690234 high:1.22208817277775IN:TRUE"
#> [1] "133 low:0.819098943115338 high:2.13621054212016IN:TRUE"
#> [1] "134 low:-0.7987179493218 high:1.01244197952352IN:TRUE"
#> [1] "135 low:-0.0570562468445305 high:1.81999285813528IN:TRUE"
#> [1] "136 low:0.100652295277294 high:1.54016181754037IN:TRUE"
#> [1] "137 low:0.36821432847155 high:2.20826098942872IN:TRUE"
#> [1] "138 low:0.496149945836252 high:2.52435701270934IN:TRUE"
#> [1] "139 low:0.26095440472242 high:1.95549537104028IN:TRUE"
#> [1] "140 low:0.096022173720659 high:1.7571349088428IN:TRUE"
#> [1] "141 low:-0.151495748691705 high:1.66247297879098IN:TRUE"
#> [1] "142 low:-0.415827422903166 high:1.69923228035878IN:TRUE"
#> [1] "143 low:0.767722800383835 high:2.32345404913604IN:TRUE"
#> [1] "144 low:-0.0718123365447083 high:1.73568427780934IN:TRUE"
#> [1] "145 low:-0.439947232430193 high:1.25635566335876IN:TRUE"
#> [1] "146 low:-1.01631044231719 high:1.45361033956828IN:TRUE"
#> [1] "147 low:0.121537983051462 high:1.77320989499518IN:TRUE"
#> [1] "148 low:-0.0795847649731883 high:1.77835060354611IN:TRUE"
#> [1] "149 low:-0.726941511628402 high:1.082954968696IN:TRUE"
#> [1] "150 low:0.17368975522881 high:1.93939237961337IN:TRUE"
#> [1] "151 low:0.428934161634757 high:2.23500273558928IN:TRUE"
#> [1] "152 low:-0.597860804848844 high:1.27006395035766IN:TRUE"
#> [1] "153 low:-0.621864058168126 high:1.56794835640916IN:TRUE"
#> [1] "154 low:0.574081133603853 high:2.6239894664523IN:TRUE"
#> [1] "155 low:-0.0993743139408021 high:2.02127382124019IN:TRUE"
#> [1] "156 low:0.642292829424868 high:2.46232271875156IN:TRUE"
#> [1] "157 low:0.0566594683075086 high:1.85728866359025IN:TRUE"
#> [1] "158 low:0.0871897134285646 high:1.40680447707672IN:TRUE"
#> [1] "159 low:-0.331991575246591 high:1.23773811097866IN:TRUE"
#> [1] "160 low:-0.0660276096942055 high:1.4789066566393IN:TRUE"
#> [1] "161 low:1.08762316304344 high:2.7486721238206IN:FALSE"
#> [1] "162 low:0.192301779568988 high:2.02696320267609IN:TRUE"
#> [1] "163 low:-0.312796903809277 high:1.76617052382988IN:TRUE"
#> [1] "164 low:-0.0610425699231305 high:2.0884526946759IN:TRUE"
#> [1] "165 low:0.578432421762496 high:2.27375636005446IN:TRUE"
#> [1] "166 low:-0.276490819730346 high:1.67037375538457IN:TRUE"
#> [1] "167 low:0.687459068717505 high:2.31018664361098IN:TRUE"
#> [1] "168 low:0.153678935311787 high:1.85745371041073IN:TRUE"
#> [1] "169 low:-0.0229318838719164 high:1.61874084845364IN:TRUE"
#> [1] "170 low:-0.220388560856852 high:1.44150658586397IN:TRUE"
#> [1] "171 low:0.639944826953307 high:2.1597325812128IN:TRUE"
#> [1] "172 low:0.270109232510506 high:2.35264255934219IN:TRUE"
#> [1] "173 low:0.754706457603912 high:2.71170736415065IN:TRUE"
#> [1] "174 low:0.144362688174676 high:1.94132679504466IN:TRUE"
#> [1] "175 low:0.182380880617493 high:1.65106652821515IN:TRUE"
#> [1] "176 low:0.872473065445322 high:2.46015442440316IN:TRUE"
#> [1] "177 low:0.258517426890897 high:2.04777127641046IN:TRUE"
#> [1] "178 low:-0.115796664251251 high:1.46550449215198IN:TRUE"
#> [1] "179 low:0.625451787148451 high:2.45778652563372IN:TRUE"
#> [1] "180 low:-0.0165334518445689 high:1.720107241928IN:TRUE"
#> [1] "181 low:0.2625499846659 high:1.67338038414459IN:TRUE"
#> [1] "182 low:0.222414426187524 high:1.88224267052589IN:TRUE"
#> [1] "183 low:0.745025959532322 high:2.49697884688597IN:TRUE"
#> [1] "184 low:0.440819461051313 high:1.9487428357584IN:TRUE"
#> [1] "185 low:-0.00755321595818215 high:2.66879507303316IN:TRUE"
#> [1] "186 low:0.311492020013943 high:2.04827371919294IN:TRUE"
#> [1] "187 low:0.677405966531773 high:2.37263948889948IN:TRUE"
#> [1] "188 low:0.415605288337687 high:1.94646253475656IN:TRUE"
#> [1] "189 low:0.421627028208922 high:2.25763547547156IN:TRUE"
#> [1] "190 low:-0.227638402020936 high:1.51462939354148IN:TRUE"
#> [1] "191 low:0.224363703478461 high:2.15862941577986IN:TRUE"
#> [1] "192 low:1.3250901549075 high:3.32668180181585IN:FALSE"
#> [1] "193 low:0.602337458262576 high:2.41012652318584IN:TRUE"
#> [1] "194 low:0.344008828046594 high:1.91221835086831IN:TRUE"
#> [1] "195 low:0.019088276144188 high:1.8478890756094IN:TRUE"
#> [1] "196 low:0.248520366703777 high:1.74997510612532IN:TRUE"
#> [1] "197 low:-0.0973708293834125 high:1.41535646934746IN:TRUE"
#> [1] "198 low:0.259939980619016 high:1.96015438164706IN:TRUE"
#> [1] "199 low:0.238699834290364 high:1.42723201167998IN:TRUE"
#> [1] "200 low:-0.106999879069858 high:1.54954722516472IN:TRUE"
# Porcentaje de esos experimentos en los que el CI contiene a 1
sum(CI_in_50$isin)/200*100
#> [1] 96

# Evolución del ancho de los CIs
ggplot() +
  geom_ribbon(data = CI_in, aes(x = index, ymin = low, ymax = high), fill = "#1380A1", alpha = 0.6) + 
  geom_ribbon(data = CI_in_50, aes(x = index, ymin = low, ymax = high), fill = "#ED6A5A", alpha = 0.6) +
  geom_hline(yintercept = 1, color = "black", linetype = "dashed")

4 - La interacción

Utilizando el dataset cats del paquete {MASS}, ajuste un modelo lineal para estimar el peso del corazón (en g) Hwt a partir de la variable continua peso del cuerpo (en Kg) Bwt y la variable discreta sexo Sex.

Vizualice los resultados e interprete la salida de summary() ¿Hay diferencia entre summary() y Anova(. , type =3)? ¿Por qué?

Escriba la ecuación y estime el peso del corazón para un gato macho de 3 Kg y para una gata hembra de 2 Kg.

Escribe el modelo lineal para gatos hembras y machos agrupando pendientes y ordenadas al origen ¿Para qué sexo la pendiente es más empinada?

# install.packages("MASS")
library(MASS)

# Modelo gatos
model_cats <- cats %>% lm(Hwt ~ Bwt * Sex, .)
summary(model_cats)
#> 
#> Call:
#> lm(formula = Hwt ~ Bwt * Sex, data = .)
#> 
#> Residuals:
#>     Min      1Q  Median      3Q     Max 
#> -3.7728 -1.0118 -0.1196  0.9272  4.8646 
#> 
#> Coefficients:
#>             Estimate Std. Error t value Pr(>|t|)    
#> (Intercept)   2.9813     1.8428   1.618 0.107960    
#> Bwt           2.6364     0.7759   3.398 0.000885 ***
#> SexM         -4.1654     2.0618  -2.020 0.045258 *  
#> Bwt:SexM      1.6763     0.8373   2.002 0.047225 *  
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 1.442 on 140 degrees of freedom
#> Multiple R-squared:  0.6566, Adjusted R-squared:  0.6493 
#> F-statistic: 89.24 on 3 and 140 DF,  p-value: < 2.2e-16
Anova(model_cats, type = 3)
#> Anova Table (Type III tests)
#> 
#> Response: Hwt
#>              Sum Sq  Df F value    Pr(>F)    
#> (Intercept)   5.441   1  2.6172 0.1079605    
#> Bwt          24.002   1 11.5455 0.0008846 ***
#> Sex           8.485   1  4.0817 0.0452578 *  
#> Bwt:Sex       8.332   1  4.0077 0.0472246 *  
#> Residuals   291.047 140                      
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# PesoCora = 2.9813 + 2.6364 * PesoTotal - 4.1654 * SexoM + 1.6763 * PesoTotal x SexoM
# Gato hembra:
# PesoCoraH = 2.9813 + 2.6364 * PesoTotal
# Gato macho:
# PesoCoraM = (2.9813 - 4.1654) + (2.6364 + 1.6763) * PesoTotal

5 - Muchas variables

Utilizando el dataset birth14 del paquete {openintro}, ajuste un modelo lineal para estimar el peso de los recién nacidos (en libras) weight utilizando como regresores la longitud del embarazo en semanas weeks, la edad de la madre en años mage, el sexo del bebé sex, si la madre era fumadora habit, y el número de visitas al hospital durante el embarazo visits.

Escriba la ecuación del modelo completo.

Interprete las pendientes de weeks y habit en este contexo.

Calcule los residuos para el primer valor del dataset.

Estime el peso en Kg para un bebé female nacido después de 39 semanas de embarazo, con una madre de 38 años de edad, fumadora y con 12 visitas al hospital durante el embarazo.

# El modelo
library(openintro)

model_births <- births14 %>% 
  drop_na() %>% 
  lm(weight ~ weeks + mage + sex + habit + visits, .) 

summary(model_births)
#> 
#> Call:
#> lm(formula = weight ~ weeks + mage + sex + habit + visits, data = .)
#> 
#> Residuals:
#>     Min      1Q  Median      3Q     Max 
#> -3.4351 -0.6882  0.0084  0.7018  3.7021 
#> 
#> Coefficients:
#>              Estimate Std. Error t value Pr(>|t|)    
#> (Intercept) -3.650658   0.631919  -5.777 1.09e-08 ***
#> weeks        0.263989   0.015514  17.017  < 2e-16 ***
#> mage         0.014886   0.006679   2.229  0.02612 *  
#> sexmale      0.424400   0.074285   5.713 1.57e-08 ***
#> habitsmoker -0.449862   0.140693  -3.197  0.00144 ** 
#> visits       0.006442   0.009624   0.669  0.50346    
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 1.043 on 788 degrees of freedom
#> Multiple R-squared:  0.315,  Adjusted R-squared:  0.3106 
#> F-statistic: 72.46 on 5 and 788 DF,  p-value: < 2.2e-16

# Peso = -3.650658 + 0.263989 * weeks + 0.014886 * mage + 0.424400 * sexmale - 0.449862 habitsmoker + 0.006442 * visits

# Residuos
residuo <- abs(predict.lm(model_births, births14[1,]) - births14$weight[1])

# Predicciones
predict.lm(model_births, tibble(sex = "female", weeks = 39, mage = 38, habit = "smoker", visits = 12)) * 0.453592
#>        1 
#> 3.101662