# Estimar el modelo: price en función de lotsize, sqrft y bdrms
data(hprice1)
modelo_hprice <- lm(price ~ lotsize + sqrft + bdrms, data = hprice1)
# Presentación de resultados estilo stargazer
stargazer(modelo_hprice, title = "Modelo para hprice1", type = "html")
| Dependent variable: | |
| price | |
| lotsize | 0.002*** |
| (0.001) | |
| sqrft | 0.123*** |
| (0.013) | |
| bdrms | 13.853 |
| (9.010) | |
| Constant | -21.770 |
| (29.475) | |
| Observations | 88 |
| R2 | 0.672 |
| Adjusted R2 | 0.661 |
| Residual Std. Error | 59.833 (df = 84) |
| F Statistic | 57.460*** (df = 3; 84) |
| Note: | p<0.1; p<0.05; p<0.01 |
# Extracción de los residuos para las pruebas de normalidad
residuos <- modelo_hprice$residuals
# Verificando el ajuste con fitdistrplus
fit_normal <- fitdist(data = residuos, distr = "norm")
plot(fit_normal)
summary(fit_normal)
## Fitting of the distribution ' norm ' by maximum likelihood
## Parameters :
## estimate Std. Error
## mean -2.321494e-15 6.231625
## sd 5.845781e+01 4.406424
## Loglikelihood: -482.8775 AIC: 969.7549 BIC: 974.7096
## Correlation matrix:
## mean sd
## mean 1 0
## sd 0 1
# Prueba JB usando tseries
options(scipen = 999)
salida_JB <- jarque.bera.test(residuos)
salida_JB
##
## Jarque Bera Test
##
## data: residuos
## X-squared = 32.278, df = 2, p-value = 0.00000009794
# Gráfica con fastGraph
options(scipen = 999)
alpha_sig <- 0.05
JB <- salida_JB$statistic
gl <- salida_JB$parameter
VC <- qchisq(1 - alpha_sig, gl, lower.tail = TRUE)
shadeDist(JB, ddist = "dchisq", parm1 = gl, lower.tail = FALSE, xmin = 0,
sub = paste("VC:", round(VC, 2), " ", "JB:", round(JB, 2)))
# Resumen manual de los residuos para JB usando dplyr
residuos %>%
as_tibble() %>%
summarise(n = n(),
Media = mean(value),
Desv.Est = sd(value)) %>%
gt() %>%
tab_header("Resumen Manual para JB")
| Resumen Manual para JB | ||
| n | Media | Desv.Est |
|---|---|---|
| 88 | -0.000000000000002321494 | 58.79282 |
Conclusión: Dado que el p-value 0.00000009794 es menor al nivel de significancia 0.05, se rechaza la hipótesis nula . Por lo tanto, bajo la prueba Jarque-Bera, los residuos no siguen una distribución normal, lo cual se confirma visualmente en la gráfica de Chi-cuadrado donde el estadístico JB cae muy lejos del valor crítico.
# Tabla de cálculo manual KS usando dplyr y gt
tabla_KS <- residuos %>%
as_tibble() %>%
mutate(posicion = row_number()) %>%
arrange(value) %>%
mutate(dist1 = row_number() / n(),
dist2 = (row_number() - 1) / n(),
zi = as.vector(scale(value, center = TRUE)),
pi = pnorm(zi, lower.tail = TRUE),
dif1 = abs(dist1 - pi),
dif2 = abs(dist2 - pi)) %>%
rename(residuales = value)
# Aplicación de formato gt y resaltado de máximos
tabla_KS %>%
gt() %>%
tab_header("Tabla para calcular el Estadistico KS") %>%
tab_source_note(source_note = "Fuente: Elaboración propia") %>%
tab_style(
style = list(cell_fill(color = "#A569BD"), cell_text(style = "italic")),
locations = cells_body(columns = dif1, rows = dif1 == max(dif1))
) %>%
tab_style(
style = list(cell_fill(color = "#3498DB"), cell_text(style = "italic")),
locations = cells_body(columns = dif2, rows = dif2 == max(dif2))
)
| Tabla para calcular el Estadistico KS | |||||||
| residuales | posicion | dist1 | dist2 | zi | pi | dif1 | dif2 |
|---|---|---|---|---|---|---|---|
| -120.026447 | 81 | 0.01136364 | 0.00000000 | -2.041515459 | 0.02059981 | 0.0092361731 | 0.0205998094 |
| -115.508697 | 77 | 0.02272727 | 0.01136364 | -1.964673586 | 0.02472601 | 0.0019987418 | 0.0133623781 |
| -107.080889 | 24 | 0.03409091 | 0.02272727 | -1.821326006 | 0.03427866 | 0.0001877487 | 0.0115513850 |
| -91.243980 | 48 | 0.04545455 | 0.03409091 | -1.551957925 | 0.06033615 | 0.0148816002 | 0.0262452366 |
| -85.461169 | 12 | 0.05681818 | 0.04545455 | -1.453598781 | 0.07302879 | 0.0162106057 | 0.0275742421 |
| -77.172687 | 32 | 0.06818182 | 0.05681818 | -1.312620980 | 0.09465535 | 0.0264735301 | 0.0378371665 |
| -74.702719 | 54 | 0.07954545 | 0.06818182 | -1.270609602 | 0.10193378 | 0.0223883300 | 0.0337519664 |
| -65.502849 | 39 | 0.09090909 | 0.07954545 | -1.114130117 | 0.13261169 | 0.0417025941 | 0.0530662305 |
| -63.699108 | 69 | 0.10227273 | 0.09090909 | -1.083450505 | 0.13930425 | 0.0370315271 | 0.0483951634 |
| -62.566594 | 83 | 0.11363636 | 0.10227273 | -1.064187703 | 0.14362184 | 0.0299854747 | 0.0413491110 |
| -59.845223 | 36 | 0.12500000 | 0.11363636 | -1.017900230 | 0.15436269 | 0.0293626861 | 0.0407263225 |
| -54.466158 | 13 | 0.13636364 | 0.12500000 | -0.926408352 | 0.17711690 | 0.0407532663 | 0.0521169027 |
| -54.300415 | 14 | 0.14772727 | 0.13636364 | -0.923589260 | 0.17785010 | 0.0301228311 | 0.0414864675 |
| -52.129801 | 15 | 0.15909091 | 0.14772727 | -0.886669532 | 0.18762842 | 0.0285375141 | 0.0399011505 |
| -51.441108 | 17 | 0.17045455 | 0.15909091 | -0.874955638 | 0.19079902 | 0.0203444766 | 0.0317081129 |
| -48.704980 | 47 | 0.18181818 | 0.17045455 | -0.828417174 | 0.20371714 | 0.0218989601 | 0.0332625965 |
| -48.350295 | 29 | 0.19318182 | 0.18181818 | -0.822384375 | 0.20542908 | 0.0122472664 | 0.0236109028 |
| -47.855859 | 11 | 0.20454545 | 0.19318182 | -0.813974573 | 0.20782976 | 0.0032843043 | 0.0146479407 |
| -45.639765 | 1 | 0.21590909 | 0.20454545 | -0.776281294 | 0.21879146 | 0.0028823668 | 0.0142460032 |
| -43.142550 | 9 | 0.22727273 | 0.21590909 | -0.733806463 | 0.23153335 | 0.0042606233 | 0.0156242596 |
| -41.749618 | 57 | 0.23863636 | 0.22727273 | -0.710114247 | 0.23881665 | 0.0001802823 | 0.0115439187 |
| -40.869022 | 27 | 0.25000000 | 0.23863636 | -0.695136302 | 0.24348494 | 0.0065150566 | 0.0048485798 |
| -37.749811 | 34 | 0.26136364 | 0.25000000 | -0.642082009 | 0.26040997 | 0.0009536682 | 0.0104099682 |
| -36.663785 | 71 | 0.27272727 | 0.26136364 | -0.623609925 | 0.26644190 | 0.0062853771 | 0.0050782592 |
| -36.646568 | 79 | 0.28409091 | 0.27272727 | -0.623317083 | 0.26653809 | 0.0175528221 | 0.0061891857 |
| -33.801248 | 37 | 0.29545455 | 0.28409091 | -0.574921384 | 0.28267223 | 0.0127823120 | 0.0014186757 |
| -29.766931 | 16 | 0.30681818 | 0.29545455 | -0.506302171 | 0.30632227 | 0.0004959124 | 0.0108677240 |
| -26.696234 | 22 | 0.31818182 | 0.30681818 | -0.454073044 | 0.32488813 | 0.0067063089 | 0.0180699452 |
| -24.271531 | 23 | 0.32954545 | 0.31818182 | -0.412831567 | 0.33986501 | 0.0103195566 | 0.0216831929 |
| -23.651448 | 86 | 0.34090909 | 0.32954545 | -0.402284648 | 0.34373728 | 0.0028281851 | 0.0141918214 |
| -19.683427 | 88 | 0.35227273 | 0.34090909 | -0.334793052 | 0.36889060 | 0.0166178738 | 0.0279815102 |
| -17.817835 | 10 | 0.36363636 | 0.35227273 | -0.303061413 | 0.38092153 | 0.0172851663 | 0.0286488027 |
| -16.762094 | 60 | 0.37500000 | 0.36363636 | -0.285104441 | 0.38778206 | 0.0127820638 | 0.0241457002 |
| -16.596960 | 21 | 0.38636364 | 0.37500000 | -0.282295711 | 0.38885839 | 0.0024947507 | 0.0138583870 |
| -16.271207 | 58 | 0.39772727 | 0.38636364 | -0.276755010 | 0.39098411 | 0.0067431583 | 0.0046204781 |
| -13.815798 | 56 | 0.40909091 | 0.39772727 | -0.234991254 | 0.40710776 | 0.0019831485 | 0.0093804879 |
| -13.462160 | 75 | 0.42045455 | 0.40909091 | -0.228976273 | 0.40944368 | 0.0110108666 | 0.0003527698 |
| -12.081520 | 4 | 0.43181818 | 0.42045455 | -0.205493119 | 0.41859344 | 0.0132247451 | 0.0018611087 |
| -11.629207 | 51 | 0.44318182 | 0.43181818 | -0.197799788 | 0.42160086 | 0.0215809622 | 0.0102173258 |
| -11.312669 | 74 | 0.45454545 | 0.44318182 | -0.192415834 | 0.42370825 | 0.0308372092 | 0.0194735728 |
| -8.236558 | 3 | 0.46590909 | 0.45454545 | -0.140094626 | 0.44429261 | 0.0216164775 | 0.0102528411 |
| -7.662789 | 70 | 0.47727273 | 0.46590909 | -0.130335452 | 0.44815052 | 0.0291222111 | 0.0177585748 |
| -6.752801 | 67 | 0.48863636 | 0.47727273 | -0.114857588 | 0.45427900 | 0.0343573625 | 0.0229937262 |
| -6.707262 | 31 | 0.50000000 | 0.48863636 | -0.114083016 | 0.45458599 | 0.0454140074 | 0.0340503710 |
| -6.402439 | 85 | 0.51136364 | 0.50000000 | -0.108898313 | 0.45664157 | 0.0547220642 | 0.0433584278 |
| -5.446904 | 82 | 0.52272727 | 0.51136364 | -0.092645733 | 0.46309251 | 0.0596347676 | 0.0482711313 |
| -3.537785 | 43 | 0.53409091 | 0.52272727 | -0.060173762 | 0.47600862 | 0.0580822876 | 0.0467186512 |
| -2.824941 | 61 | 0.54545455 | 0.53409091 | -0.048049090 | 0.48083856 | 0.0646159857 | 0.0532523493 |
| -2.745208 | 68 | 0.55681818 | 0.54545455 | -0.046692922 | 0.48137899 | 0.0754391961 | 0.0640755598 |
| -0.195089 | 65 | 0.56818182 | 0.55681818 | -0.003318245 | 0.49867621 | 0.0695056040 | 0.0581419676 |
| 1.399296 | 55 | 0.57954545 | 0.56818182 | 0.023800450 | 0.50949411 | 0.0700513452 | 0.0586877088 |
| 5.363331 | 26 | 0.59090909 | 0.57954545 | 0.091224254 | 0.53634280 | 0.0545662924 | 0.0432026561 |
| 6.700640 | 53 | 0.60227273 | 0.59090909 | 0.113970383 | 0.54536936 | 0.0569033628 | 0.0455397265 |
| 7.386314 | 80 | 0.61363636 | 0.60227273 | 0.125632935 | 0.54998875 | 0.0636476093 | 0.0522839730 |
| 9.099900 | 41 | 0.62500000 | 0.61363636 | 0.154779103 | 0.56150227 | 0.0634977329 | 0.0521340965 |
| 12.433611 | 46 | 0.63636364 | 0.62500000 | 0.211481796 | 0.58374433 | 0.0526193043 | 0.0412556680 |
| 16.718018 | 62 | 0.64772727 | 0.63636364 | 0.284354766 | 0.61193074 | 0.0357965328 | 0.0244328965 |
| 18.093192 | 5 | 0.65909091 | 0.64772727 | 0.307744934 | 0.62086179 | 0.0382291219 | 0.0268654856 |
| 18.801816 | 38 | 0.67045455 | 0.65909091 | 0.319797835 | 0.62543921 | 0.0450153400 | 0.0336517036 |
| 19.168108 | 33 | 0.68181818 | 0.67045455 | 0.326028052 | 0.62779843 | 0.0540197476 | 0.0426561112 |
| 19.219211 | 72 | 0.69318182 | 0.68181818 | 0.326897255 | 0.62812720 | 0.0650546167 | 0.0536909803 |
| 20.334434 | 59 | 0.70454545 | 0.69318182 | 0.345865960 | 0.63527827 | 0.0692671805 | 0.0579035442 |
| 24.909926 | 78 | 0.71590909 | 0.70454545 | 0.423689939 | 0.66410402 | 0.0518050676 | 0.0404414312 |
| 26.236229 | 40 | 0.72727273 | 0.71590909 | 0.446248874 | 0.67229126 | 0.0549814685 | 0.0436178321 |
| 30.924022 | 25 | 0.73863636 | 0.72727273 | 0.525982978 | 0.70054998 | 0.0380863808 | 0.0267227444 |
| 32.253952 | 45 | 0.75000000 | 0.73863636 | 0.548603608 | 0.70836125 | 0.0416387548 | 0.0302751184 |
| 32.529367 | 49 | 0.76136364 | 0.75000000 | 0.553288104 | 0.70996693 | 0.0513967091 | 0.0400330727 |
| 32.675968 | 18 | 0.77272727 | 0.76136364 | 0.555781630 | 0.71081993 | 0.0619073452 | 0.0505437088 |
| 33.275839 | 20 | 0.78409091 | 0.77272727 | 0.565984762 | 0.71429793 | 0.0697929786 | 0.0584293423 |
| 36.031430 | 52 | 0.79545455 | 0.78409091 | 0.612854281 | 0.73001365 | 0.0654408934 | 0.0540772571 |
| 37.147186 | 84 | 0.80681818 | 0.79545455 | 0.631832029 | 0.73625168 | 0.0705665028 | 0.0592028664 |
| 40.320875 | 7 | 0.81818182 | 0.80681818 | 0.685812928 | 0.75358446 | 0.0645973596 | 0.0532337232 |
| 44.334467 | 30 | 0.82954545 | 0.81818182 | 0.754079634 | 0.77459930 | 0.0549461574 | 0.0435825211 |
| 46.907165 | 28 | 0.84090909 | 0.82954545 | 0.797838357 | 0.78751785 | 0.0533912405 | 0.0420276041 |
| 54.418366 | 87 | 0.85227273 | 0.84090909 | 0.925595465 | 0.82267187 | 0.0296008528 | 0.0182372164 |
| 55.091131 | 35 | 0.86363636 | 0.85227273 | 0.937038450 | 0.82563061 | 0.0380057535 | 0.0266421172 |
| 55.470305 | 44 | 0.87500000 | 0.86363636 | 0.943487765 | 0.82728426 | 0.0477157353 | 0.0363520989 |
| 62.939597 | 6 | 0.88636364 | 0.87500000 | 1.070532059 | 0.85781006 | 0.0285535797 | 0.0171899433 |
| 66.478628 | 50 | 0.89772727 | 0.88636364 | 1.130727018 | 0.87091500 | 0.0268122757 | 0.0154486394 |
| 67.426518 | 63 | 0.90909091 | 0.89772727 | 1.146849569 | 0.87427810 | 0.0348128083 | 0.0234491719 |
| 67.603959 | 19 | 0.92045455 | 0.90909091 | 1.149867648 | 0.87490081 | 0.0455537393 | 0.0341901029 |
| 69.707122 | 64 | 0.93181818 | 0.92045455 | 1.185640095 | 0.88211777 | 0.0497004123 | 0.0383367759 |
| 69.843246 | 8 | 0.94318182 | 0.93181818 | 1.187955411 | 0.88257451 | 0.0606073068 | 0.0492436705 |
| 74.848732 | 2 | 0.95454545 | 0.94318182 | 1.273093116 | 0.89850750 | 0.0560379553 | 0.0446743189 |
| 112.729191 | 66 | 0.96590909 | 0.95454545 | 1.917397313 | 0.97240626 | 0.0064971714 | 0.0178608078 |
| 163.795081 | 73 | 0.97727273 | 0.96590909 | 2.785970904 | 0.99733162 | 0.0200588896 | 0.0314225260 |
| 198.660139 | 42 | 0.98863636 | 0.97727273 | 3.378986513 | 0.99963623 | 0.0109998685 | 0.0223635048 |
| 209.375830 | 76 | 1.00000000 | 0.98863636 | 3.561248407 | 0.99981545 | 0.0001845478 | 0.0111790885 |
| Fuente: Elaboración propia | |||||||
# Prueba KS-Lilliefors automática
prueba_KS <- lillie.test(residuos)
prueba_KS
##
## Lilliefors (Kolmogorov-Smirnov) normality test
##
## data: residuos
## D = 0.075439, p-value = 0.2496
Conclusión: En este caso, el estadístico D obtenido es 0.0754, el cual es menor al Valor Crítico calculado de 0.0944. Asimismo, el p-value resultante es 0.2496, el cual es mayor al nivel de significancia de 0.05. Por lo tanto, no se rechaza la Hipótesis Nula y se concluye que, bajo la prueba de Lilliefors, los residuos siguen una distribución normal.
library(dplyr)
library(gt)
# Cálculo Manual
# 1. Preparación de la tabla base
residuos <- modelo_hprice$residuals
residuos %>%
as_tibble() %>%
rename(residuales = value) %>%
arrange(residuales) %>%
mutate(pi = (row_number() - 0.375) / (n() + 0.25)) %>%
mutate(mi = qnorm(pi, lower.tail = TRUE)) %>%
mutate(ai = 0) -> tabla_SW
# 2.Definición de parámetros para el cálculo de ai
m <- sum(tabla_SW$mi^2)
n <- nrow(hprice1) # Tamaño de muestra de hprice1
theta <- 1 / sqrt(n)
# 3.Cálculo de los coeficientes ai (Fórmulas de aproximación de Royston)
tabla_SW$ai[n] <- -2.706056*theta^5 + 4.434685*theta^4 - 2.071190*theta^3 - 0.147981*theta^2 + 0.2211570*theta + tabla_SW$mi[n]/sqrt(m)
tabla_SW$ai[n-1] <- -3.582633*theta^5 + 5.682633*theta^4 - 1.752461*theta^3 - 0.293762*theta^2 + 0.042981*theta + tabla_SW$mi[n-1]/sqrt(m)
tabla_SW$ai[1] <- -tabla_SW$ai[n]
tabla_SW$ai[2] <- -tabla_SW$ai[n-1]
# Cálculo de ai para los valores intermedios
omega <- (m - 2*tabla_SW$mi[n]^2 - 2*tabla_SW$mi[n-1]^2) / (1 - 2*tabla_SW$ai[n]^2 - 2*tabla_SW$ai[n-1]^2)
tabla_SW$ai[3:(n-2)] <- tabla_SW$mi[3:(n-2)] / sqrt(omega)
# Cálculo de productos y cuadrados para el estadístico W
tabla_SW %>%
mutate(ai_ui = ai * residuales, ui2 = residuales^2) -> tabla_SW
# 4. Generación de la Tabla con formato gt
tabla_SW %>%
gt() %>%
tab_header("Tabla para calcular el Estadistico W") %>%
tab_source_note(source_note = "Fuente: Elaboración propia")
| Tabla para calcular el Estadistico W | |||||
| residuales | pi | mi | ai | ai_ui | ui2 |
|---|---|---|---|---|---|
| -120.026447 | 0.007082153 | -2.45306927 | -0.286093929 | 34.338837782 | 14406.34799223 |
| -115.508697 | 0.018413598 | -2.08767462 | -0.226331231 | 26.143225495 | 13342.25903657 |
| -107.080889 | 0.029745042 | -1.88455395 | -0.201511408 | 21.578020632 | 11466.31670225 |
| -91.243980 | 0.041076487 | -1.73832835 | -0.185875811 | 16.960048752 | 8325.46388922 |
| -85.461169 | 0.052407932 | -1.62194155 | -0.173430814 | 14.821600075 | 7303.61136157 |
| -77.172687 | 0.063739377 | -1.52411994 | -0.162970954 | 12.576906330 | 5955.62354189 |
| -74.702719 | 0.075070822 | -1.43903134 | -0.153872609 | 11.494702279 | 5580.49626206 |
| -65.502849 | 0.086402266 | -1.36324747 | -0.145769197 | 9.548297773 | 4290.62326804 |
| -63.699108 | 0.097733711 | -1.29457343 | -0.138426027 | 8.817614500 | 4057.57641853 |
| -62.566594 | 0.109065156 | -1.23151500 | -0.131683320 | 8.238976839 | 3914.57869135 |
| -59.845223 | 0.120396601 | -1.17300649 | -0.125427129 | 7.506214499 | 3581.45072682 |
| -54.466158 | 0.131728045 | -1.11825971 | -0.119573169 | 6.512691096 | 2966.56233834 |
| -54.300415 | 0.143059490 | -1.06667420 | -0.114057239 | 6.193355472 | 2948.53511008 |
| -52.129801 | 0.154390935 | -1.01778137 | -0.108829231 | 5.673246083 | 2717.51610406 |
| -51.441108 | 0.165722380 | -0.97120790 | -0.103849228 | 5.342119306 | 2646.18755812 |
| -48.704980 | 0.177053824 | -0.92665123 | -0.099084876 | 4.825926905 | 2372.17509746 |
| -48.350295 | 0.188385269 | -0.88386232 | -0.094509548 | 4.569564512 | 2337.75102457 |
| -47.855859 | 0.199716714 | -0.84263354 | -0.090101040 | 4.311862673 | 2290.18324033 |
| -45.639765 | 0.211048159 | -0.80278966 | -0.085840618 | 3.917745629 | 2082.98814155 |
| -43.142550 | 0.222379603 | -0.76418130 | -0.081712307 | 3.525277277 | 1861.27961161 |
| -41.749618 | 0.233711048 | -0.72667986 | -0.077702356 | 3.244043648 | 1743.03058469 |
| -40.869022 | 0.245042493 | -0.69017366 | -0.073798824 | 3.016085791 | 1670.27697055 |
| -37.749811 | 0.256373938 | -0.65456498 | -0.069991263 | 2.642156946 | 1425.04821452 |
| -36.663785 | 0.267705382 | -0.61976766 | -0.066270458 | 2.429725818 | 1344.23312095 |
| -36.646568 | 0.279036827 | -0.58570518 | -0.062628228 | 2.295109622 | 1342.97093753 |
| -33.801248 | 0.290368272 | -0.55230918 | -0.059057264 | 1.996209250 | 1142.52439130 |
| -29.766931 | 0.301699717 | -0.51951819 | -0.055550992 | 1.653582575 | 886.07020942 |
| -26.696234 | 0.313031161 | -0.48727661 | -0.052103467 | 1.390966354 | 712.68890388 |
| -24.271531 | 0.324362606 | -0.45553386 | -0.048709282 | 1.182248861 | 589.10722688 |
| -23.651448 | 0.335694051 | -0.42424369 | -0.045363489 | 1.072912217 | 559.39099788 |
| -19.683427 | 0.347025496 | -0.39336354 | -0.042061540 | 0.827915257 | 387.43729851 |
| -17.817835 | 0.358356941 | -0.36285409 | -0.038799229 | 0.691318234 | 317.47522771 |
| -16.762094 | 0.369688385 | -0.33267878 | -0.035572645 | 0.596272007 | 280.96778010 |
| -16.596960 | 0.381019830 | -0.30280344 | -0.032378138 | 0.537378676 | 275.45909399 |
| -16.271207 | 0.392351275 | -0.27319601 | -0.029212277 | 0.475319006 | 264.75217651 |
| -13.815798 | 0.403682720 | -0.24382619 | -0.026071824 | 0.360203050 | 190.87627634 |
| -13.462160 | 0.415014164 | -0.21466524 | -0.022953704 | 0.309006447 | 181.22976154 |
| -12.081520 | 0.426345609 | -0.18568573 | -0.019854987 | 0.239878409 | 145.96311543 |
| -11.629207 | 0.437677054 | -0.15686137 | -0.016772858 | 0.195055032 | 135.23845458 |
| -11.312669 | 0.449008499 | -0.12816677 | -0.013704604 | 0.155035654 | 127.97648221 |
| -8.236558 | 0.460339943 | -0.09957734 | -0.010647596 | 0.087699542 | 67.84088513 |
| -7.662789 | 0.471671388 | -0.07106908 | -0.007599268 | 0.058231584 | 58.71832836 |
| -6.752801 | 0.483002833 | -0.04261848 | -0.004557105 | 0.030773222 | 45.60032533 |
| -6.707262 | 0.494334278 | -0.01420234 | -0.001518626 | 0.010185824 | 44.98736398 |
| -6.402439 | 0.505665722 | 0.01420234 | 0.001518626 | -0.009722911 | 40.99122172 |
| -5.446904 | 0.516997167 | 0.04261848 | 0.004557105 | -0.024822110 | 29.66876028 |
| -3.537785 | 0.528328612 | 0.07106908 | 0.007599268 | -0.026884576 | 12.51592288 |
| -2.824941 | 0.539660057 | 0.09957734 | 0.010647596 | -0.030078835 | 7.98029397 |
| -2.745208 | 0.550991501 | 0.12816677 | 0.013704604 | -0.037621996 | 7.53616965 |
| -0.195089 | 0.562322946 | 0.15686137 | 0.016772858 | -0.003272200 | 0.03805971 |
| 1.399296 | 0.573654391 | 0.18568573 | 0.019854987 | 0.027782994 | 1.95802794 |
| 5.363331 | 0.584985836 | 0.21466524 | 0.022953704 | 0.123108313 | 28.76531940 |
| 6.700640 | 0.596317280 | 0.24382619 | 0.026071824 | 0.174697904 | 44.89857663 |
| 7.386314 | 0.607648725 | 0.27319601 | 0.029212277 | 0.215771059 | 54.55763860 |
| 9.099900 | 0.618980170 | 0.30280344 | 0.032378138 | 0.294637808 | 82.80817401 |
| 12.433611 | 0.630311615 | 0.33267878 | 0.035572645 | 0.442296424 | 154.59467612 |
| 16.718018 | 0.641643059 | 0.36285409 | 0.038799229 | 0.648646203 | 279.49212715 |
| 18.093192 | 0.652974504 | 0.39336354 | 0.042061540 | 0.761027520 | 327.36359375 |
| 18.801816 | 0.664305949 | 0.42424369 | 0.045363489 | 0.852915978 | 353.50828232 |
| 19.168108 | 0.675637394 | 0.45553386 | 0.048709282 | 0.933664777 | 367.41636183 |
| 19.219211 | 0.686968839 | 0.48727661 | 0.052103467 | 1.001387528 | 369.37806665 |
| 20.334434 | 0.698300283 | 0.51951819 | 0.055550992 | 1.129598008 | 413.48922446 |
| 24.909926 | 0.709631728 | 0.55230918 | 0.059057264 | 1.471112049 | 620.50439009 |
| 26.236229 | 0.720963173 | 0.58570518 | 0.062628228 | 1.643128534 | 688.33970624 |
| 30.924022 | 0.732294618 | 0.61976766 | 0.066270458 | 2.049349072 | 956.29510728 |
| 32.253952 | 0.743626062 | 0.65456498 | 0.069991263 | 2.257494854 | 1040.31742689 |
| 32.529367 | 0.754957507 | 0.69017366 | 0.073798824 | 2.400629035 | 1058.15970869 |
| 32.675968 | 0.766288952 | 0.72667986 | 0.077702356 | 2.538999708 | 1067.71890359 |
| 33.275839 | 0.777620397 | 0.76418130 | 0.081712307 | 2.719045583 | 1107.28147309 |
| 36.031430 | 0.788951841 | 0.80278966 | 0.085840618 | 3.092960242 | 1298.26396526 |
| 37.147186 | 0.800283286 | 0.84263354 | 0.090101040 | 3.347000059 | 1379.91339592 |
| 40.320875 | 0.811614731 | 0.88386232 | 0.094509548 | 3.810707636 | 1625.77293960 |
| 44.334467 | 0.822946176 | 0.92665123 | 0.099084876 | 4.392875123 | 1965.54494196 |
| 46.907165 | 0.834277620 | 0.97120790 | 0.103849228 | 4.871272904 | 2200.28216686 |
| 54.418366 | 0.845609065 | 1.01778137 | 0.108829231 | 5.922308882 | 2961.35853839 |
| 55.091131 | 0.856940510 | 1.06667420 | 0.114057239 | 6.283542333 | 3035.03273452 |
| 55.470305 | 0.868271955 | 1.11825971 | 0.119573169 | 6.632760113 | 3076.95468678 |
| 62.939597 | 0.879603399 | 1.17300649 | 0.125427129 | 7.894332885 | 3961.39282116 |
| 66.478628 | 0.890934844 | 1.23151500 | 0.131683320 | 8.754126443 | 4419.40796540 |
| 67.426518 | 0.902266289 | 1.29457343 | 0.138426027 | 9.333585010 | 4546.33534619 |
| 67.603959 | 0.913597734 | 1.36324747 | 0.145769197 | 9.854574914 | 4570.29533539 |
| 69.707122 | 0.924929178 | 1.43903134 | 0.153872609 | 10.726016772 | 4859.08292257 |
| 69.843246 | 0.936260623 | 1.52411994 | 0.162970954 | 11.382420482 | 4878.07906512 |
| 74.848732 | 0.947592068 | 1.62194155 | 0.173430814 | 12.981076532 | 5602.33268291 |
| 112.729191 | 0.958923513 | 1.73832835 | 0.185875811 | 20.953629849 | 12707.87061041 |
| 163.795081 | 0.970254958 | 1.88455395 | 0.201511408 | 33.006577315 | 26828.82842547 |
| 198.660139 | 0.981586402 | 2.08767462 | 0.226331231 | 44.962993843 | 39465.85101402 |
| 209.375830 | 0.992917847 | 2.45306927 | 0.286093929 | 59.901153719 | 43838.23810785 |
| Fuente: Elaboración propia | |||||
# Calculamos el estadístico W manual usando las sumatorias de la tabla
W <- (sum(tabla_SW$ai_ui)^2) / sum(tabla_SW$ui2)
print(W)
## [1] 0.9413208
# Definimos mu y sigma para la normalización (Aproximación de Royston)
mu <- 0.0038915*log(n)^3 - 0.083751*log(n)^2 - 0.31082*log(n) - 1.5861
sigma <- exp(0.0030302*log(n)^2 - 0.082676*log(n) - 0.4803)
# Calculamos el estadístico normalizado Wn
Wn <- (log(1 - W) - mu) / sigma
print(Wn)
## [1] 3.241867
# Obtenemos el p-value usando la distribución normal
p.value <- pnorm(Wn, lower.tail = FALSE)
print(p.value)
## [1] 0.0005937472
#Gráfica de la prueba Shapiro-Wilk
# Graficamos la distribución normal y sombreamos el área del p-value
library(fastGraph)
shadeDist(Wn, ddist = "dnorm", lower.tail = FALSE)
# Validación automática de Shapiro-Wilk
shapiro.test(residuos)
##
## Shapiro-Wilk normality test
##
## data: residuos
## W = 0.94132, p-value = 0.0005937
Conclusión: Tras realizar el cálculo manual y validarlo con la librería, se obtuvo un estadístico Wde aproximadamente 0.9412 y un p-value de 0.00059 . Dado que el p-value es menor al nivel de significancia 0.05, se rechaza la Hipótesis Nula . Esto indica que los residuos no siguen una distribución normal. La gráfica muestra que el valor normalizado Wn se encuentra significativamente alejado del centro de la campana, cayendo en la zona de rechazo.