1 Ecuación de regresión lineal múltiple

\[y_i=\beta_0 + \beta_1{\times}x_{i,1} + \beta_2{\times}x_{i,2} + \cdots + \beta_p{\times}x_{i,p} + error_{i}\]

\[error_{i}{\sim}N(0,\sigma_{error}^2)\]

\[\boldsymbol{y}_{(n{\times}1)}=\boldsymbol{X}_{(n{\times}p)}\boldsymbol{\beta}_{(p{\times}1)}+\boldsymbol{error}_{(n{\times}1)}\]

\[\boldsymbol{error}_{(n{\times}1)}{\sim}N(\boldsymbol{0}_{(n{\times}1)},\sigma_{error}^2\boldsymbol{I}_{(n{\times}1)})\]

2 Librería

library(car)
library(broom)
library(tidyverse)
library(ggfortify)
library(mosaic)
library(huxtable)
library(jtools)
library(latex2exp)
library(pubh)
library(sjlabelled)
library(sjPlot)
library(sjmisc)
library(Ecdat)

3 Base de datos

data(birthwt, package = "MASS")
library(tidyverse)
birthwt <- birthwt %>%
  mutate(
    age = as.numeric(age),
    lwt = as.numeric(lwt),
    smoke = factor(smoke, labels = c("Non-smoker", "Smoker")),
    race = factor(race, labels = c("White", "African American", "Other")),
    bwt = as.numeric(bwt)
    ) %>%
  var_labels(
    bwt = 'Birth weight (g)',
    smoke = 'Smoking status',
    race = 'Race'
    )
  • state: estado

  • year: año

  • cpi: índice de precios al consumidor

  • pop: población estatal

  • packpc: número de paquetes consumidos per cápita

  • income: ingresos personales estatales (total, nominal)

  • tax: promedio de impuestos especiales estatales, federales y locales promedio para el año fiscal

  • avgprs: precio promedio durante el año fiscal, incluidos los impuestos sobre las ventas

  • taxs: impuestos especiales promedio para el año fiscal, incluidos los impuestos sobre las ventas

glimpse(birthwt)
## Rows: 189
## Columns: 10
## $ low   <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ age   <dbl> 19, 33, 20, 21, 18, 21, 22, 17, 29, 26, 19, 19, 22, 30, 18, 18,…
## $ lwt   <dbl> 182, 155, 105, 108, 107, 124, 118, 103, 123, 113, 95, 150, 95, …
## $ race  <fct> African American, Other, White, White, White, Other, White, Oth…
## $ smoke <fct> Non-smoker, Non-smoker, Smoker, Smoker, Smoker, Non-smoker, Non…
## $ ptl   <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, …
## $ ht    <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ ui    <int> 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, …
## $ ftv   <int> 0, 3, 1, 2, 0, 0, 1, 1, 1, 0, 0, 1, 0, 2, 0, 0, 0, 3, 0, 1, 2, …
## $ bwt   <dbl> 2523, 2551, 2557, 2594, 2600, 2622, 2637, 2637, 2663, 2665, 272…

4 Análisis exploratorio

birthwt %>%
  group_by(race, smoke) %>%
  summarise(
    n = n(),
    Mean = mean(bwt, na.rm = TRUE),
    SD = sd(bwt, na.rm = TRUE),
    Median = median(bwt, na.rm = TRUE),
    CV = rel_dis(bwt)
  ) 
## # A tibble: 6 x 7
## # Groups:   race [3]
##   race             smoke          n  Mean    SD Median    CV
##   <fct>            <fct>      <int> <dbl> <dbl>  <dbl> <dbl>
## 1 White            Non-smoker    44 3429.  710.  3593  0.207
## 2 White            Smoker        52 2827.  626.  2776. 0.222
## 3 African American Non-smoker    16 2854.  621.  2920  0.218
## 4 African American Smoker        10 2504   637.  2381  0.254
## 5 Other            Non-smoker    55 2816.  709.  2807  0.252
## 6 Other            Smoker        12 2757.  810.  3146. 0.294
birthwt %>%
  gen_bst_df(bwt ~ race|smoke)
Birth weight (g)LowerCIUpperCIRaceSmoking status
3.43e+033.21e+033.63e+03WhiteNon-smoker
2.83e+032.67e+032.99e+03WhiteSmoker
2.85e+032.58e+033.16e+03African AmericanNon-smoker
2.5e+03 2.1e+03 2.86e+03African AmericanSmoker
2.82e+032.62e+033.01e+03OtherNon-smoker
2.76e+032.29e+033.15e+03OtherSmoker
birthwt %>%
  bar_error(bwt ~ race, fill = ~ smoke) %>%
  axis_labs() %>%
  gf_labs(fill = "Smoking status:")

4.1 Análisis de correlación

library(PerformanceAnalytics)
chart.Correlation(birthwt[,c(2,3,10)], histogram = TRUE, pch = 19)

4.2 Análisis de datos faltantes

sapply(birthwt, function(x) sum(is.na(x)))
##   low   age   lwt  race smoke   ptl    ht    ui   ftv   bwt 
##     0     0     0     0     0     0     0     0     0     0
cor.mtest <- function(mat, ...) {
  mat <- as.matrix(mat)
  n <- ncol(mat)
  p.mat<- matrix(NA, n, n)
  diag(p.mat) <- 0
  for (i in 1:(n - 1)) {
    for (j in (i + 1):n) {
      tmp <- cor.test(mat[, i], mat[, j], ...)
      p.mat[i, j] <- p.mat[j, i] <- tmp$p.value
    }
  }
  colnames(p.mat) <- rownames(p.mat) <- colnames(mat)
  p.mat
}

p.mat <- cor.mtest(birthwt[,c(2,3,10)])

library(corrplot)
birthwt.cor <- cor(birthwt[,c(2,3,10)])
corrplot(birthwt.cor, method = "number", type = "upper",
         tl.cex = 0.9, number.cex = 0.6,  order="hclust",  diag = FALSE,
         addCoef.col = "black", tl.col = "black", 
         p.mat = p.mat, sig.level = 0.05, insig = "blank")

5 Partición de la base de datos

set.seed(0123456789)

library(dplyr)
birthwt.train <- sample_frac(tbl = birthwt, replace = FALSE, size = 0.80)
birthwt.test <- anti_join(birthwt, birthwt.train)

6 Ajuste de un modelo lineal a los datos

model_norm <- lm(bwt ~ smoke + race, data = birthwt.train)

6.1 Diagnósticos del modelo lineal

library(ggfortify)
autoplot(model_norm)

6.2 Resúmen del modelo lineal

model_norm %>% augment() %>% as_tibble()
bwtsmokerace.fitted.resid.std.resid.hat.sigma.cooksd
3.94e+03Non-smokerOther2.91e+031.03e+031.69   0.02086110.0152  
3.27e+03Non-smokerOther2.91e+03364       0.597  0.02086170.00189 
3.77e+03Non-smokerOther2.91e+03860       1.41   0.02086130.0106  
2.77e+03SmokerWhite2.91e+03-139       -0.229  0.019 6170.000253
3.06e+03Non-smokerWhite3.34e+03-276       -0.454  0.02216170.00116 
3.06e+03SmokerWhite2.91e+03154       0.252  0.019 6170.000307
3.57e+03SmokerOther2.48e+031.09e+031.81   0.03946110.0336  
2.3e+03 Non-smokerOther2.91e+03-609       -1      0.02086150.00532 
2.73e+03Non-smokerOther2.91e+03-177       -0.291  0.02086170.000451
2.95e+03SmokerWhite2.91e+0339.6     0.065  0.019 6182.04e-05
2.66e+03SmokerWhite2.91e+03-245       -0.403  0.019 6170.000784
2.47e+03SmokerWhite2.91e+03-442       -0.726  0.019 6160.00255 
2.66e+03SmokerWhite2.91e+03-243       -0.399  0.019 6170.000771
2.08e+03SmokerWhite2.91e+03-824       -1.35   0.019 6140.00885 
2.84e+03Non-smokerOther2.91e+03-75.5     -0.124  0.02086178.15e-05
2.06e+03Non-smokerOther2.91e+03-855       -1.4    0.02086130.0105  
2.92e+03SmokerWhite2.91e+0313.6     0.0223 0.019 6182.41e-06
3.2e+03 Non-smokerOther2.91e+03293       0.48   0.02086170.00122 
1.89e+03Non-smokerOther2.91e+03-1.02e+03-1.67   0.02086120.0148  
2.41e+03SmokerWhite2.91e+03-494       -0.811  0.019 6160.00318 
3.32e+03Non-smokerAfrican American3.02e+03298       0.498  0.05066170.0033  
2.75e+03Non-smokerOther2.91e+03-159       -0.262  0.02086170.000364
3.06e+03SmokerWhite2.91e+03154       0.252  0.019 6170.000307
1.59e+03Non-smokerOther2.91e+03-1.32e+03-2.17   0.02086080.025   
4.05e+03Non-smokerWhite3.34e+03716       1.18   0.02216150.00783 
3.86e+03SmokerWhite2.91e+03948       1.55   0.019 6120.0117  
2.44e+03Non-smokerAfrican American3.02e+03-581       -0.968  0.05066160.0125  
2.5e+03 Non-smokerAfrican American3.02e+03-524       -0.873  0.05066160.0102  
3.94e+03SmokerWhite2.91e+031.03e+031.69   0.019 6110.0139  
2.82e+03SmokerWhite2.91e+03-87.4     -0.143  0.019 6179.94e-05
3.63e+03SmokerWhite2.91e+03721       1.18   0.019 6150.00676 
1.47e+03Non-smokerOther2.91e+03-1.44e+03-2.36   0.02086060.0295  
2.47e+03SmokerOther2.48e+03-14.7     -0.0244 0.03946186.12e-06
3.94e+03Non-smokerWhite3.34e+03603       0.991  0.02216150.00555 
3.76e+03SmokerWhite2.91e+03848       1.39   0.019 6130.00936 
3.1e+03 Non-smokerOther2.91e+03194       0.318  0.02086170.000536
3.09e+03Non-smokerWhite3.34e+03-248       -0.408  0.02216170.00094 
3.32e+03SmokerOther2.48e+03840       1.39   0.03946130.0199  
4.59e+03Non-smokerWhite3.34e+031.25e+032.06   0.02216090.0241  
3.33e+03SmokerOther2.48e+03850       1.41   0.03946130.0204  
2.12e+03SmokerOther2.48e+03-356       -0.59   0.03946170.00357 
2.13e+03SmokerAfrican American2.59e+03-463       -0.774  0.05616160.00891 
2.37e+03SmokerAfrican American2.59e+03-222       -0.371  0.05616170.00205 
3.54e+03Non-smokerOther2.91e+03634       1.04   0.02086150.00574 
2.32e+03Non-smokerOther2.91e+03-585       -0.961  0.02086160.0049  
2.08e+03SmokerWhite2.91e+03-824       -1.35   0.019 6140.00885 
2.45e+03Non-smokerOther2.91e+03-460       -0.756  0.02086160.00303 
3.15e+03SmokerWhite2.91e+03239       0.391  0.019 6170.000741
3.03e+03SmokerWhite2.91e+03125       0.204  0.019 6170.000202
2.5e+03 SmokerOther2.48e+0314.3     0.0236 0.03946185.73e-06
3.8e+03 Non-smokerWhite3.34e+03461       0.757  0.02216160.00324 
3.91e+03Non-smokerWhite3.34e+03574       0.943  0.02216160.00503 
1.73e+03Non-smokerOther2.91e+03-1.18e+03-1.94   0.02086100.02    
2.28e+03Non-smokerOther2.91e+03-628       -1.03   0.02086150.00565 
2.35e+03Non-smokerWhite3.34e+03-985       -1.62   0.02216120.0148  
3.77e+03Non-smokerOther2.91e+03860       1.41   0.02086130.0106  
2.84e+03Non-smokerWhite3.34e+03-502       -0.825  0.02216160.00385 
2.59e+03SmokerWhite2.91e+03-314       -0.516  0.019 6170.00129 
2.3e+03 SmokerWhite2.91e+03-612       -1      0.019 6150.00488 
1.82e+03SmokerWhite2.91e+03-1.09e+03-1.79   0.019 6110.0155  
2.42e+03SmokerWhite2.91e+03-484       -0.795  0.019 6160.00306 
3.4e+03 Non-smokerAfrican American3.02e+03383       0.639  0.05066170.00544 
3.2e+03 SmokerOther2.48e+03722       1.2    0.03946150.0147  
3.86e+03Non-smokerAfrican American3.02e+03841       1.4    0.05066130.0262  
2.72e+03Non-smokerOther2.91e+03-188       -0.309  0.02086170.000508
3.42e+03Non-smokerWhite3.34e+0377.9     0.128  0.02216179.26e-05
4e+03       Non-smokerWhite3.34e+03659       1.08   0.02216150.00663 
2.47e+03SmokerWhite2.91e+03-442       -0.726  0.019 6160.00255 
1.88e+03SmokerWhite2.91e+03-1.02e+03-1.68   0.019 6120.0136  
3.65e+03SmokerWhite2.91e+03743       1.22   0.019 6140.00718 
3.7e+03 Non-smokerWhite3.34e+03361       0.593  0.02216170.00199 
3.3e+03 SmokerOther2.48e+03822       1.36   0.03946140.0191  
2.56e+03SmokerWhite2.91e+03-351       -0.576  0.019 6170.00161 
2.06e+03Non-smokerOther2.91e+03-855       -1.4    0.02086130.0105  
3.37e+03Non-smokerAfrican American3.02e+03355       0.593  0.05066170.00468 
2.24e+03Non-smokerOther2.91e+03-670       -1.1    0.02086150.00643 
3.22e+03Non-smokerOther2.91e+03315       0.516  0.02086170.00142 
3.54e+03Non-smokerOther2.91e+03634       1.04   0.02086150.00574 
3.61e+03Non-smokerWhite3.34e+03276       0.453  0.02216170.00116 
2.55e+03Non-smokerOther2.91e+03-359       -0.59   0.02086170.00185 
3.65e+03Non-smokerWhite3.34e+03313       0.514  0.02216170.0015  
3.46e+03Non-smokerWhite3.34e+03121       0.199  0.02216170.000223
2.91e+03SmokerWhite2.91e+03-2.39    -0.003920.019 6187.42e-08
3.09e+03Non-smokerOther2.91e+03180       0.295  0.02086170.000461
3.23e+03Non-smokerOther2.91e+03322       0.528  0.02086170.00148 
4.11e+03Non-smokerWhite3.34e+03773       1.27   0.02216140.00912 
2.88e+03Non-smokerWhite3.34e+03-461       -0.758  0.02216160.00325 
1.94e+03SmokerWhite2.91e+03-972       -1.6    0.019 6120.0123  
2.38e+03Non-smokerOther2.91e+03-529       -0.869  0.02086160.00401 
1.7e+03 Non-smokerAfrican American3.02e+03-1.32e+03-2.2    0.05066070.0643  
1.9e+03 Non-smokerOther2.91e+03-1.01e+03-1.66   0.02086120.0146  
2.78e+03Non-smokerAfrican American3.02e+03-241       -0.401  0.05066170.00214 
2.35e+03SmokerWhite2.91e+03-555       -0.911  0.019 6160.00402 
2.62e+03Non-smokerOther2.91e+03-288       -0.474  0.02086170.00119 
4.17e+03Non-smokerWhite3.34e+03829       1.36   0.02216140.0105  
2.75e+03Non-smokerOther2.91e+03-160       -0.264  0.02086170.000368
3.65e+03Non-smokerWhite3.34e+03313       0.514  0.02216170.0015  
3.2e+03 Non-smokerOther2.91e+03293       0.48   0.02086170.00122 
3.88e+03SmokerWhite2.91e+03976       1.6    0.019 6120.0124  
3.08e+03Non-smokerWhite3.34e+03-258       -0.424  0.02216170.00102 
3.61e+03Non-smokerWhite3.34e+03276       0.453  0.02216170.00116 
3.57e+03Non-smokerOther2.91e+03662       1.09   0.02086150.00626 
2.22e+03SmokerWhite2.91e+03-683       -1.12   0.019 6150.00608 
2.08e+03Non-smokerWhite3.34e+03-1.26e+03-2.06   0.02216090.0241  
3.06e+03Non-smokerWhite3.34e+03-276       -0.454  0.02216170.00116 
2.88e+03Non-smokerOther2.91e+03-33.5     -0.055  0.02086181.6e-05 
2.92e+03Non-smokerWhite3.34e+03-418       -0.687  0.02216170.00267 
1.79e+03SmokerWhite2.91e+03-1.12e+03-1.83   0.019 6100.0163  
1.93e+03SmokerWhite2.91e+03-980       -1.61   0.019 6120.0125  
3.23e+03Non-smokerWhite3.34e+03-104       -0.171  0.02216170.000166
2.21e+03SmokerOther2.48e+03-270       -0.447  0.03946170.00205 
3.22e+03Non-smokerOther2.91e+03315       0.516  0.02086170.00142 
3.26e+03SmokerOther2.48e+03779       1.29   0.03946140.0171  
2.1e+03 Non-smokerWhite3.34e+03-1.24e+03-2.03   0.02216090.0234  
3.86e+03Non-smokerWhite3.34e+03522       0.858  0.02216160.00416 
2.95e+03SmokerAfrican American2.59e+03359       0.601  0.05616170.00537 
4.15e+03Non-smokerWhite3.34e+03815       1.34   0.02216140.0101  
2.98e+03Non-smokerAfrican American3.02e+03-41.6     -0.0694 0.05066186.42e-05
2.6e+03 SmokerWhite2.91e+03-308       -0.506  0.019 6170.00124 
2.38e+03SmokerAfrican American2.59e+03-208       -0.348  0.05616170.0018  
3e+03       SmokerWhite2.91e+0396.6     0.158  0.019 6170.000122
3.1e+03 Non-smokerWhite3.34e+03-238       -0.391  0.02216170.000866
3.79e+03Non-smokerAfrican American3.02e+03771       1.29   0.05066140.022   
2.92e+03SmokerAfrican American2.59e+03331       0.554  0.05616170.00456 
3.32e+03SmokerWhite2.91e+03409       0.67   0.019 6170.00217 
2.98e+03SmokerWhite2.91e+0368.6     0.113  0.019 6186.13e-05
4.17e+03Non-smokerWhite3.34e+03836       1.37   0.02216140.0107  
3.43e+03SmokerWhite2.91e+03522       0.856  0.019 6160.00354 
3.73e+03Non-smokerWhite3.34e+03390       0.641  0.02216170.00232 
2.3e+03 SmokerAfrican American2.59e+03-293       -0.49   0.05616170.00357 
3.47e+03Non-smokerWhite3.34e+03135       0.222  0.02216170.000278
2.38e+03SmokerAfrican American2.59e+03-208       -0.348  0.05616170.0018  
3.06e+03Non-smokerAfrican American3.02e+0343.4     0.0723 0.05066186.97e-05
2.52e+03Non-smokerAfrican American3.02e+03-496       -0.827  0.05066160.0091  
2.64e+03Non-smokerOther2.91e+03-273       -0.449  0.02086170.00107 
3.77e+03Non-smokerWhite3.34e+03432       0.71   0.02216160.00285 
2.86e+03Non-smokerOther2.91e+03-47.5     -0.078  0.02086183.23e-05
3.37e+03SmokerWhite2.91e+03466       0.764  0.019 6160.00282 
2.92e+03Non-smokerAfrican American3.02e+03-98.6     -0.164  0.05066170.00036 
3.44e+03SmokerAfrican American2.59e+03855       1.43   0.05616130.0304  
2.41e+03SmokerWhite2.91e+03-498       -0.818  0.019 6160.00323 
2.78e+03SmokerWhite2.91e+03-126       -0.207  0.019 6170.000208
3.18e+03Non-smokerOther2.91e+03265       0.434  0.02086170.001   
2.41e+03SmokerWhite2.91e+03-498       -0.818  0.019 6160.00323 
3.27e+03Non-smokerOther2.91e+03364       0.597  0.02086170.00189 
3.64e+03SmokerWhite2.91e+03729       1.2    0.019 6150.00691 
3.83e+03Non-smokerWhite3.34e+03489       0.803  0.02216160.00365 
3.88e+03Non-smokerOther2.91e+03974       1.6    0.02086120.0136  
3.04e+03SmokerAfrican American2.59e+03453       0.758  0.05616160.00854 
2.81e+03Non-smokerOther2.91e+03-103       -0.17   0.02086170.000153
3.46e+03Non-smokerWhite3.34e+03122       0.2    0.02216170.000227

6.3 Coeficientes del modelo

model_norm %>% tidy()
termestimatestd.errorstatisticp.value
(Intercept)3.34e+0391.536.5 1.58e-75
smokeSmoker-430       108  -3.990.000104
raceAfrican American-320       149  -2.140.0341  
raceOther-428       117  -3.650.000367

6.3.1 Intervalos de confianza

model_norm %>% confint() %>% as_tibble()
2.5 %97.5 %
3.16e+033.52e+03
-643       -217       
-615       -24.3     
-659       -196       
model_norm %>% 
  glm_coef(labels = model_labels(model_norm))
ParameterCoefficientPr(>|t|)
Constant3338.12 (3157.2, 3519.04)< 0.001
Smoking status: Smoker-429.74 (-642.58, -216.9)< 0.001
Race: African American-319.5 (-614.66, -24.35)0.034
Race: Other-427.65 (-659.34, -195.95)< 0.001
model_norm %>%
  glm_coef(se_rob = TRUE, labels = model_labels(model_norm))
ParameterCoefficientPr(>|t|)
Constant3338.12 (3157.12, 3519.13)< 0.001
Smoking status: Smoker-429.74 (-644.83, -214.65)< 0.001
Race: African American-319.5 (-587.4, -51.61)0.02
Race: Other-427.65 (-671.48, -183.81)< 0.001
model_norm %>%
  plot_model("pred", terms = ~race|smoke, dot.size = 1.5, title = "")

emmip(model_norm, smoke ~ race) %>%
  gf_labs(y = get_label(birthwt$bwt), x = "", col = "Smoking status")

6.4 Multicollinealidad del modelo

library(regclass)
model_norm %>% VIF() %>% as_tibble()
GVIFDfGVIF^(1/(2*Df))
1.1211.06
1.1221.03

6.5 Residuales del modelo ajustado

p1 <- ggplot(birthwt.train, aes(birthwt.train[,2], residuals(model_norm))) +
    geom_point() + geom_smooth(color = "blue")
p2 <- ggplot(birthwt.train, aes(birthwt.train[,3], residuals(model_norm))) +
  geom_point() + geom_smooth(color = "blue")
p3 <- ggplot(birthwt.train, aes(birthwt.train[,10], residuals(model_norm))) +
  geom_point() + geom_smooth(color = "blue")

library(pdp)
grid.arrange(p1, p2, p3)

6.6 Datos arípicos

library(olsrr)
model_norm %>% ols_plot_cooksd_bar()

6.7 Reajustando el modelo de regresión lineal

6.7.1 Algoritmo paso a paso

model_norm %>% 
  Anova() %>% 
  tidy()
termsumsqdfstatisticp.value
smoke6.03e+06115.9 0.000104
race5.46e+0627.210.00103 
Residuals5.57e+07147          
model_norm %>% 
  tidy()
termestimatestd.errorstatisticp.value
(Intercept)3.34e+0391.536.5 1.58e-75
smokeSmoker-430       108  -3.990.000104
raceAfrican American-320       149  -2.140.0341  
raceOther-428       117  -3.650.000367
model_norm %>% 
  glm_coef(labels = model_labels(model_norm))
ParameterCoefficientPr(>|t|)
Constant3338.12 (3157.2, 3519.04)< 0.001
Smoking status: Smoker-429.74 (-642.58, -216.9)< 0.001
Race: African American-319.5 (-614.66, -24.35)0.034
Race: Other-427.65 (-659.34, -195.95)< 0.001
model_norm %>%
  glm_coef(se_rob = TRUE, labels = model_labels(model_norm))
ParameterCoefficientPr(>|t|)
Constant3338.12 (3157.12, 3519.13)< 0.001
Smoking status: Smoker-429.74 (-644.83, -214.65)< 0.001
Race: African American-319.5 (-587.4, -51.61)0.02
Race: Other-427.65 (-671.48, -183.81)< 0.001

6.8 Criterios de ajuste del modelo líneal

model_norm %>% glance()
r.squaredadj.r.squaredsigmastatisticp.valuedflogLikAICBICdeviancedf.residualnobs
0.1360.1196157.737.88e-053-1.18e+032.37e+032.39e+035.57e+07147151
library(MASS)
model_norm_AIC <- stepAIC(model_norm, trace = 0)
model_norm_AIC %>% 
  Anova() %>% 
  tidy()
termsumsqdfstatisticp.value
smoke6.03e+06115.9 0.000104
race5.46e+0627.210.00103 
Residuals5.57e+07147          
model_norm_AIC %>% glance()
r.squaredadj.r.squaredsigmastatisticp.valuedflogLikAICBICdeviancedf.residualnobs
0.1360.1196157.737.88e-053-1.18e+032.37e+032.39e+035.57e+07147151

6.9 Análisis de varianza

model_norm %>% Anova() %>% tidy()
termsumsqdfstatisticp.value
smoke6.03e+06115.9 0.000104
race5.46e+0627.210.00103 
Residuals5.57e+07147          

6.10 Comparación de criterios

AIC(model_norm, model_norm_AIC)
dfAIC
52.37e+03
52.37e+03

6.11 Importancia relativa decada variable

#library(relaimpo)
#calc.relimp(model_norm_AIC, type = c("lmg", "last", "first", "pratt", "betasq"), rela = T)
#boot <- boot.relimp(model_norm, b = 1000, type = c("lmg", "last", "first", "pratt"), 
#                    rank = TRUE, diff = TRUE, rela = TRUE)
#booteval.relimp(boot) 
#plot(booteval.relimp(boot,sort=TRUE))