Intro

Ejercicio 9

Carga de datos y gráfico descriptivo

# Lectura de datos
edad <- c(40, 38, 40, 35, 36, 37, 41, 40, 37, 38, 40, 38,
          40, 36, 40, 38, 42, 39, 40, 37, 36, 38, 39, 40)
peso <- c(2968, 2795, 3163, 2925, 2625, 2847, 3292, 3473, 
          2628, 3176, 3421, 2975, 3317, 2729, 2935, 2754, 
          3210, 2817, 3126, 2539, 2412, 2991, 2875, 3231)
sexo <- gl(2, 12, labels=c("H", "M"))
ejer09 <- data.frame(edad, peso, sexo)

# Gráfico
ggplot(ejer09, aes(x = edad, y = peso, color = sexo)) + 
  geom_point() 

Estimación

fit.M1 <- lm(peso ~ edad*sexo, data = ejer09)
ols_step_backward_p(fit.M1, prem = 0.05)
## 
## 
##                             Elimination Summary                             
## ---------------------------------------------------------------------------
##         Variable                   Adj.                                        
## Step     Removed     R-Square    R-Square     C(p)       AIC         RMSE      
## ---------------------------------------------------------------------------
##    1    edad:sexo       0.640      0.6057    2.1945    321.3909    177.1159    
## ---------------------------------------------------------------------------
fit.M1 <- lm(peso ~ edad + sexo, data = ejer09)
# Parámetros estimados
tab_model(fit.M1,
          show.r2 = FALSE, 
          show.p = FALSE)
  peso
Predictors Estimates CI
(Intercept) -1610.28 -3245.02 – 24.46
edad 120.89 78.34 – 163.45
sexo [M] -163.04 -314.45 – -11.63
Observations 24

Diagnóstico

Análisis gráfico

# Valores de diagnóstico
diagnostico <- fortify(fit.M1)
# Gráfico
ggplot(diagnostico,aes(x = edad, y = .stdresid, colour = sexo)) + 
   geom_point() +
   geom_hline(yintercept = 0, col = "red") +
   facet_wrap(. ~ sexo)

Tests estadísticos

# Tests de hipótesis
ols_test_normality(fit.M1)
## -----------------------------------------------
##        Test             Statistic       pvalue  
## -----------------------------------------------
## Shapiro-Wilk              0.9286         0.0906 
## Kolmogorov-Smirnov        0.1677         0.4601 
## Cramer-von Mises          2.375          0.0000 
## Anderson-Darling          0.7078         0.0562 
## -----------------------------------------------
leveneTest(.stdresid ~ sexo, data = diagnostico)
## Levene's Test for Homogeneity of Variance (center = median)
##       Df F value Pr(>F)
## group  1  0.4347 0.5165
##       22
# Influencia
ols_plot_cooksd_chart(fit.M1)

Predicción

plot_model(fit.M1, "pred", terms = c("edad", "sexo"),
           title ="Predicción de la media")

Ejercicio 9

Carga de datos y gráfico descriptivo

# Lectura de datos
ejer11 <- read_csv("https://goo.gl/OX9wgM", col_types = "ddddddc")

# Gráfico
g1 <- ggplot(ejer11, aes(x = claridad, y = calidad, color = region)) + 
  geom_point() 
g2 <- ggplot(ejer11, aes(x = aroma, y = calidad, color = region)) + 
  geom_point()
g3 <- ggplot(ejer11, aes(x = cuerpo, y = calidad, color = region)) + 
  geom_point()
g4 <- ggplot(ejer11, aes(x = olor, y = calidad, color = region)) + 
  geom_point()
g5 <- ggplot(ejer11, aes(x = matiz, y = calidad, color = region)) + 
  geom_point()

library(ggpubr)
ggarrange(g1, g2, g3, g4, g5,
          ncol = 2, nrow = 3)

Estimación

fit.M1 <- lm(calidad ~ (claridad + aroma + cuerpo + olor + matiz)*region, data = ejer11)
ols_step_backward_p(fit.M1, prem = 0.05)
## 
## 
##                               Elimination Summary                                
## --------------------------------------------------------------------------------
##         Variable                         Adj.                                       
## Step        Removed        R-Square    R-Square     C(p)        AIC        RMSE     
## --------------------------------------------------------------------------------
##    1    aroma:region          0.905      0.8402     4.0312    105.7803    0.8178    
##    2    aroma                0.9039      0.8453     2.2624    104.2163    0.8044    
##    3    claridad             0.9039      0.8453     0.2624    104.2163    0.8044    
##    4    cuerpo               0.9039      0.8453    -1.7376    104.2163    0.8044    
##    5    olor:region          0.8971      0.8476    -2.3044    102.8133    0.7984    
##    6    matiz                0.8971      0.8476    -4.3044    102.8133    0.7984    
##    7    matiz:region          0.875      0.8348    -1.6521    104.1960    0.8313    
##    8    claridad:region      0.8456      0.8158     2.5310    106.2064    0.8779    
##    9    cuerpo:region        0.8242      0.8087     5.0522    105.1516    0.8946    
## --------------------------------------------------------------------------------
fit.M1 <- lm(calidad ~ olor + region, data = ejer11)
# Parámetros estimados
tab_model(fit.M1,
          show.r2 = FALSE, 
          show.p = FALSE)
  calidad
Predictors Estimates CI
(Intercept) 7.09 5.49 – 8.70
olor 1.12 0.76 – 1.47
region [B] -1.53 -2.28 – -0.78
region [C] 1.22 0.41 – 2.04
Observations 38

Diagnóstico

Análisis gráfico

# Valores de diagnóstico
diagnostico <- fortify(fit.M1)
# Gráfico
ggplot(diagnostico,aes(x = olor, y = .stdresid, colour = region)) + 
   geom_point() +
   geom_hline(yintercept = 0, col = "red") +
   facet_wrap(. ~ region)

Tests estadísticos

# Tests de hipótesis
ols_test_normality(fit.M1)
## Warning in ks.test(y, "pnorm", mean(y), sd(y)): ties should not be present for
## the Kolmogorov-Smirnov test
## -----------------------------------------------
##        Test             Statistic       pvalue  
## -----------------------------------------------
## Shapiro-Wilk              0.9884         0.9577 
## Kolmogorov-Smirnov        0.0849         0.9472 
## Cramer-von Mises          3.0963         0.0000 
## Anderson-Darling          0.2026         0.8686 
## -----------------------------------------------
leveneTest(.stdresid ~ region, data = diagnostico)
## Warning in leveneTest.default(y = y, group = group, ...): group coerced to
## factor.
## Levene's Test for Homogeneity of Variance (center = median)
##       Df F value Pr(>F)
## group  2  0.0851 0.9186
##       35
# Influencia
ols_plot_cooksd_chart(fit.M1)

Predicción

plot_model(fit.M1, "pred", terms = c("olor", "region"),
           title ="Predicción de la media")

Ejercicio 13

Carga de datos y gráfico descriptivo

# Lectura de datos
ejer13 <- read_csv("https://goo.gl/V6hyVW", col_types = "ddc") 
ejer13 <- ejer13 %>%
  mutate_if(sapply(ejer13, is.character), as.factor) 
ggplot(ejer13, aes(x = TempExt, y = Calor, color = Cristal)) + 
  geom_point()

Estimación

fit.M1 <- lm(Calor ~ TempExt*Cristal, data = ejer13)
ols_step_backward_p(fit.M1, prem = 0.05)
## 
## 
##                               Elimination Summary                                
## --------------------------------------------------------------------------------
##         Variable                         Adj.                                       
## Step        Removed        R-Square    R-Square     C(p)        AIC        RMSE     
## --------------------------------------------------------------------------------
##    1    TempExt:Cristal      0.9828      0.9816    -1.6468    103.5045    0.5418    
## --------------------------------------------------------------------------------
fit.M1 <- lm(Calor ~ TempExt + Cristal, data = ejer13)
# Parámetros estimados
tab_model(fit.M1,
          show.r2 = FALSE, 
          show.p = FALSE)
  Calor
Predictors Estimates CI
(Intercept) 12.66 12.18 – 13.15
TempExt -0.14 -0.15 – -0.13
Cristal [B] 4.07 3.67 – 4.46
Cristal [C] 6.87 6.48 – 7.27
Cristal [D] 9.01 8.61 – 9.40
Observations 60

Diagnóstico

Análisis gráfico

# Valores de diagnóstico
diagnostico <- fortify(fit.M1)
# Gráfico
ggplot(diagnostico,aes(x = TempExt, y = .stdresid, colour = Cristal)) + 
   geom_point() +
   geom_hline(yintercept = 0, col = "red") +
   facet_wrap(. ~ Cristal)

Tests estadísticos

# Tests de hipótesis
ols_test_normality(fit.M1)
## Warning in ks.test(y, "pnorm", mean(y), sd(y)): ties should not be present for
## the Kolmogorov-Smirnov test
## -----------------------------------------------
##        Test             Statistic       pvalue  
## -----------------------------------------------
## Shapiro-Wilk              0.9859         0.7148 
## Kolmogorov-Smirnov        0.073          0.9061 
## Cramer-von Mises          6.6198         0.0000 
## Anderson-Darling          0.2631         0.6893 
## -----------------------------------------------
leveneTest(.stdresid ~ Cristal, data = diagnostico)
## Levene's Test for Homogeneity of Variance (center = median)
##       Df F value Pr(>F)
## group  3  0.2962  0.828
##       56
# Influencia
ols_plot_cooksd_chart(fit.M1)

Predicción

plot_model(fit.M1, "pred", terms = c("TempExt", "Cristal"),
           title ="Predicción de la media")

Ejercicio 13 polinomico

Estimación

fit.M1 <- lm(Calor ~ (TempExt + I(TempExt^2) + I(TempExt^3) + I(TempExt^4))*Cristal, data = ejer13)
ols_step_backward_p(fit.M1, prem = 0.05)
## 
## 
##                                  Elimination Summary                                  
## -------------------------------------------------------------------------------------
##         Variable                              Adj.                                       
## Step          Removed           R-Square    R-Square     C(p)        AIC        RMSE     
## -------------------------------------------------------------------------------------
##    1    I(TempExt^4)              0.9889      0.9836    -2.0000    107.2356    0.5104    
##    2    I(TempExt^4):Cristal      0.9886      0.9847    -2.9606    100.7747    0.4929    
##    3    TempExt:Cristal           0.9883      0.9853    -3.6766     96.6232    0.4843    
##    4    I(TempExt^3):Cristal      0.9881      0.9859    -4.9205     91.6856    0.4738    
##    5    I(TempExt^2):Cristal       0.988      0.9866    -6.5742     86.1661    0.4620    
## -------------------------------------------------------------------------------------
fit.M1 <- lm(Calor ~ TempExt + I(TempExt^2) + I(TempExt^3) + Cristal, data = ejer13)
# Parámetros estimados
tab_model(fit.M1,
          show.r2 = FALSE, 
          show.p = FALSE)
  Calor
Predictors Estimates CI
(Intercept) 20.87 17.16 – 24.59
TempExt -0.87 -1.19 – -0.55
TempExt^2 0.02 0.01 – 0.03
TempExt^3 -0.00 -0.00 – -0.00
Cristal [B] 4.07 3.73 – 4.41
Cristal [C] 6.87 6.53 – 7.21
Cristal [D] 9.01 8.67 – 9.35
Observations 60

Diagnóstico

Análisis gráfico

# Valores de diagnóstico
diagnostico <- fortify(fit.M1)
# Gráfico
ggplot(diagnostico,aes(x = TempExt, y = .stdresid, colour = Cristal)) + 
   geom_point() +
   geom_hline(yintercept = 0, col = "red") +
   facet_wrap(. ~ Cristal)

Tests estadísticos

# Tests de hipótesis
ols_test_normality(fit.M1)
## Warning in ks.test(y, "pnorm", mean(y), sd(y)): ties should not be present for
## the Kolmogorov-Smirnov test
## -----------------------------------------------
##        Test             Statistic       pvalue  
## -----------------------------------------------
## Shapiro-Wilk              0.9224         0.0010 
## Kolmogorov-Smirnov        0.0981         0.6107 
## Cramer-von Mises          8.2567         0.0000 
## Anderson-Darling          0.6036         0.1119 
## -----------------------------------------------
leveneTest(.stdresid ~ Cristal, data = diagnostico)
## Levene's Test for Homogeneity of Variance (center = median)
##       Df F value Pr(>F)
## group  3  0.4903 0.6904
##       56
# Influencia
ols_plot_cooksd_chart(fit.M1)

Predicción

plot_model(fit.M1, "pred", terms = c("TempExt", "Cristal"),
           title ="Predicción de la media")