##Carga

library(wooldridge)
data("hprice1")
head(force(hprice1),n=5)
##   price assess bdrms lotsize sqrft colonial   lprice  lassess llotsize   lsqrft
## 1   300  349.1     4    6126  2438        1 5.703783 5.855359 8.720297 7.798934
## 2   370  351.5     3    9903  2076        1 5.913503 5.862210 9.200593 7.638198
## 3   191  217.7     3    5200  1374        0 5.252274 5.383118 8.556414 7.225482
## 4   195  231.8     3    4600  1448        1 5.273000 5.445875 8.433811 7.277938
## 5   373  319.1     4    6095  2514        1 5.921578 5.765504 8.715224 7.829630
library(wooldridge)
data("hprice1")
head(force(hprice1),n=5)
##   price assess bdrms lotsize sqrft colonial   lprice  lassess llotsize   lsqrft
## 1   300  349.1     4    6126  2438        1 5.703783 5.855359 8.720297 7.798934
## 2   370  351.5     3    9903  2076        1 5.913503 5.862210 9.200593 7.638198
## 3   191  217.7     3    5200  1374        0 5.252274 5.383118 8.556414 7.225482
## 4   195  231.8     3    4600  1448        1 5.273000 5.445875 8.433811 7.277938
## 5   373  319.1     4    6095  2514        1 5.921578 5.765504 8.715224 7.829630

##ESTIMACION

library(stargazer)
modelo_hrpice <- lm(formula = price ~ lotsize + sqrft + bdrms, data = hprice1)
stargazer(modelo_hrpice, title = "precios", type = "text", digits = 5)
## 
## precios
## ===============================================
##                         Dependent variable:    
##                     ---------------------------
##                                price           
## -----------------------------------------------
## lotsize                     0.00207***         
##                              (0.00064)         
##                                                
## sqrft                       0.12278***         
##                              (0.01324)         
##                                                
## bdrms                        13.85252          
##                              (9.01015)         
##                                                
## Constant                     -21.77031         
##                             (29.47504)         
##                                                
## -----------------------------------------------
## Observations                    88             
## R2                            0.67236          
## Adjusted R2                   0.66066          
## Residual Std. Error     59.83348 (df = 84)     
## F Statistic          57.46023*** (df = 3; 84)  
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01

##PRUEBA JB

library(moments)
residuosJB <- residuals(modelo_hrpice)
nJB <- length(residuosJB)
asimetriaJB <- skewness(residuosJB)
curtosisJB <- kurtosis(residuosJB)-3
JB <- nJB /6 * (asimetriaJB^2 + 1/4*curtosisJB^2)
print(JB)
## [1] 32.27791

#TABLA

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(gt)
library(gtExtras)
residuosJB %>%
  as_tibble() %>%
  mutate(posicion=row_number()) %>% 
  arrange(value) %>% 
  mutate(dist1=row_number()/n()) %>%
  mutate(dist2=(row_number()-1)/n()) %>%
  mutate(zi=as.vector(scale(value,center=TRUE ))) %>%
  mutate(pi=pnorm(zi, lower.tail=TRUE)) %>%
  mutate(dif1=abs(dist1-pi)) %>%
  mutate(dif2=abs(dist2-pi)) %>%
  rename(residuales=value) -> tabla_JB 
#FORMATO
tabla_JB %>%
  gt() %>%
  tab_header("TABLA PARA CALCULAR JB")
TABLA PARA CALCULAR JB
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

#GRAFICO

library(fastGraph)
residuosJB_graph <- scale(modelo_hrpice$residuals)
hist(residuosJB_graph, fred= FALSE, main= "distribucion de los residuos estandarizados" ,xlab= "residuos estandarizados")

curve(dnorm (x, mean = mean(residuosJB_graph), sd=sd(residuosJB_graph)), add = TRUE, col="blue",lwd=2)

#prueba ks

residuosks <- residuals(modelo_hrpice)
n_ks <-length(residuosks)
residuosoredenadosks <- sort(residuosks)
d_plus <- max((1:n_ks )/n_ks - pnorm(residuosoredenadosks))
d_minus <- max(pnorm(residuosoredenadosks)-(0:(n_ks-1))/n_ks)
D_KS <- max(d_plus, d_minus)
print(D_KS)
## [1] 0.5537946

##TABLA

library(dplyr)
library(gt)
library(gtExtras)
residuosks %>%
  as_tibble() %>%
  mutate(posicion=row_number()) %>%
  arrange(value) %>%
  mutate(dist1=row_number()/n()) %>%
  mutate(dist2=(row_number()-1/n())) %>%
           mutate(zi=as.vector(scale(value,center=TRUE))) %>%
           mutate(pi=pnorm(zi,lower.tail=TRUE)) %>%
           mutate(dif1=abs(dist1-pi)) %>%
  mutate(dif2=abs(dist2-pi)) %>%
  rename(residuales=value) -> tabla_ks

tabla_ks %>%
  gt() %>%
  tab_header("tabla para calsular KS")
tabla para calsular KS
residuales posicion dist1 dist2 zi pi dif1 dif2
-120.026447 81 0.01136364 0.9886364 -2.041515459 0.02059981 0.0092361731 0.9680366
-115.508697 77 0.02272727 1.9886364 -1.964673586 0.02472601 0.0019987418 1.9639103
-107.080889 24 0.03409091 2.9886364 -1.821326006 0.03427866 0.0001877487 2.9543577
-91.243980 48 0.04545455 3.9886364 -1.551957925 0.06033615 0.0148816002 3.9283002
-85.461169 12 0.05681818 4.9886364 -1.453598781 0.07302879 0.0162106057 4.9156076
-77.172687 32 0.06818182 5.9886364 -1.312620980 0.09465535 0.0264735301 5.8939810
-74.702719 54 0.07954545 6.9886364 -1.270609602 0.10193378 0.0223883300 6.8867026
-65.502849 39 0.09090909 7.9886364 -1.114130117 0.13261169 0.0417025941 7.8560247
-63.699108 69 0.10227273 8.9886364 -1.083450505 0.13930425 0.0370315271 8.8493321
-62.566594 83 0.11363636 9.9886364 -1.064187703 0.14362184 0.0299854747 9.8450145
-59.845223 36 0.12500000 10.9886364 -1.017900230 0.15436269 0.0293626861 10.8342737
-54.466158 13 0.13636364 11.9886364 -0.926408352 0.17711690 0.0407532663 11.8115195
-54.300415 14 0.14772727 12.9886364 -0.923589260 0.17785010 0.0301228311 12.8107863
-52.129801 15 0.15909091 13.9886364 -0.886669532 0.18762842 0.0285375141 13.8010079
-51.441108 17 0.17045455 14.9886364 -0.874955638 0.19079902 0.0203444766 14.7978373
-48.704980 47 0.18181818 15.9886364 -0.828417174 0.20371714 0.0218989601 15.7849192
-48.350295 29 0.19318182 16.9886364 -0.822384375 0.20542908 0.0122472664 16.7832073
-47.855859 11 0.20454545 17.9886364 -0.813974573 0.20782976 0.0032843043 17.7808066
-45.639765 1 0.21590909 18.9886364 -0.776281294 0.21879146 0.0028823668 18.7698449
-43.142550 9 0.22727273 19.9886364 -0.733806463 0.23153335 0.0042606233 19.7571030
-41.749618 57 0.23863636 20.9886364 -0.710114247 0.23881665 0.0001802823 20.7498197
-40.869022 27 0.25000000 21.9886364 -0.695136302 0.24348494 0.0065150566 21.7451514
-37.749811 34 0.26136364 22.9886364 -0.642082009 0.26040997 0.0009536682 22.7282264
-36.663785 71 0.27272727 23.9886364 -0.623609925 0.26644190 0.0062853771 23.7221945
-36.646568 79 0.28409091 24.9886364 -0.623317083 0.26653809 0.0175528221 24.7220983
-33.801248 37 0.29545455 25.9886364 -0.574921384 0.28267223 0.0127823120 25.7059641
-29.766931 16 0.30681818 26.9886364 -0.506302171 0.30632227 0.0004959124 26.6823141
-26.696234 22 0.31818182 27.9886364 -0.454073044 0.32488813 0.0067063089 27.6637482
-24.271531 23 0.32954545 28.9886364 -0.412831567 0.33986501 0.0103195566 28.6487714
-23.651448 86 0.34090909 29.9886364 -0.402284648 0.34373728 0.0028281851 29.6448991
-19.683427 88 0.35227273 30.9886364 -0.334793052 0.36889060 0.0166178738 30.6197458
-17.817835 10 0.36363636 31.9886364 -0.303061413 0.38092153 0.0172851663 31.6077148
-16.762094 60 0.37500000 32.9886364 -0.285104441 0.38778206 0.0127820638 32.6008543
-16.596960 21 0.38636364 33.9886364 -0.282295711 0.38885839 0.0024947507 33.5997780
-16.271207 58 0.39772727 34.9886364 -0.276755010 0.39098411 0.0067431583 34.5976522
-13.815798 56 0.40909091 35.9886364 -0.234991254 0.40710776 0.0019831485 35.5815286
-13.462160 75 0.42045455 36.9886364 -0.228976273 0.40944368 0.0110108666 36.5791927
-12.081520 4 0.43181818 37.9886364 -0.205493119 0.41859344 0.0132247451 37.5700429
-11.629207 51 0.44318182 38.9886364 -0.197799788 0.42160086 0.0215809622 38.5670355
-11.312669 74 0.45454545 39.9886364 -0.192415834 0.42370825 0.0308372092 39.5649281
-8.236558 3 0.46590909 40.9886364 -0.140094626 0.44429261 0.0216164775 40.5443438
-7.662789 70 0.47727273 41.9886364 -0.130335452 0.44815052 0.0291222111 41.5404858
-6.752801 67 0.48863636 42.9886364 -0.114857588 0.45427900 0.0343573625 42.5343574
-6.707262 31 0.50000000 43.9886364 -0.114083016 0.45458599 0.0454140074 43.5340504
-6.402439 85 0.51136364 44.9886364 -0.108898313 0.45664157 0.0547220642 44.5319948
-5.446904 82 0.52272727 45.9886364 -0.092645733 0.46309251 0.0596347676 45.5255439
-3.537785 43 0.53409091 46.9886364 -0.060173762 0.47600862 0.0580822876 46.5126277
-2.824941 61 0.54545455 47.9886364 -0.048049090 0.48083856 0.0646159857 47.5077978
-2.745208 68 0.55681818 48.9886364 -0.046692922 0.48137899 0.0754391961 48.5072574
-0.195089 65 0.56818182 49.9886364 -0.003318245 0.49867621 0.0695056040 49.4899601
1.399296 55 0.57954545 50.9886364 0.023800450 0.50949411 0.0700513452 50.4791423
5.363331 26 0.59090909 51.9886364 0.091224254 0.53634280 0.0545662924 51.4522936
6.700640 53 0.60227273 52.9886364 0.113970383 0.54536936 0.0569033628 52.4432670
7.386314 80 0.61363636 53.9886364 0.125632935 0.54998875 0.0636476093 53.4386476
9.099900 41 0.62500000 54.9886364 0.154779103 0.56150227 0.0634977329 54.4271341
12.433611 46 0.63636364 55.9886364 0.211481796 0.58374433 0.0526193043 55.4048920
16.718018 62 0.64772727 56.9886364 0.284354766 0.61193074 0.0357965328 56.3767056
18.093192 5 0.65909091 57.9886364 0.307744934 0.62086179 0.0382291219 57.3677746
18.801816 38 0.67045455 58.9886364 0.319797835 0.62543921 0.0450153400 58.3631972
19.168108 33 0.68181818 59.9886364 0.326028052 0.62779843 0.0540197476 59.3608379
19.219211 72 0.69318182 60.9886364 0.326897255 0.62812720 0.0650546167 60.3605092
20.334434 59 0.70454545 61.9886364 0.345865960 0.63527827 0.0692671805 61.3533581
24.909926 78 0.71590909 62.9886364 0.423689939 0.66410402 0.0518050676 62.3245323
26.236229 40 0.72727273 63.9886364 0.446248874 0.67229126 0.0549814685 63.3163451
30.924022 25 0.73863636 64.9886364 0.525982978 0.70054998 0.0380863808 64.2880864
32.253952 45 0.75000000 65.9886364 0.548603608 0.70836125 0.0416387548 65.2802751
32.529367 49 0.76136364 66.9886364 0.553288104 0.70996693 0.0513967091 66.2786694
32.675968 18 0.77272727 67.9886364 0.555781630 0.71081993 0.0619073452 67.2778164
33.275839 20 0.78409091 68.9886364 0.565984762 0.71429793 0.0697929786 68.2743384
36.031430 52 0.79545455 69.9886364 0.612854281 0.73001365 0.0654408934 69.2586227
37.147186 84 0.80681818 70.9886364 0.631832029 0.73625168 0.0705665028 70.2523847
40.320875 7 0.81818182 71.9886364 0.685812928 0.75358446 0.0645973596 71.2350519
44.334467 30 0.82954545 72.9886364 0.754079634 0.77459930 0.0549461574 72.2140371
46.907165 28 0.84090909 73.9886364 0.797838357 0.78751785 0.0533912405 73.2011185
54.418366 87 0.85227273 74.9886364 0.925595465 0.82267187 0.0296008528 74.1659645
55.091131 35 0.86363636 75.9886364 0.937038450 0.82563061 0.0380057535 75.1630058
55.470305 44 0.87500000 76.9886364 0.943487765 0.82728426 0.0477157353 76.1613521
62.939597 6 0.88636364 77.9886364 1.070532059 0.85781006 0.0285535797 77.1308263
66.478628 50 0.89772727 78.9886364 1.130727018 0.87091500 0.0268122757 78.1177214
67.426518 63 0.90909091 79.9886364 1.146849569 0.87427810 0.0348128083 79.1143583
67.603959 19 0.92045455 80.9886364 1.149867648 0.87490081 0.0455537393 80.1137356
69.707122 64 0.93181818 81.9886364 1.185640095 0.88211777 0.0497004123 81.1065186
69.843246 8 0.94318182 82.9886364 1.187955411 0.88257451 0.0606073068 82.1060619
74.848732 2 0.95454545 83.9886364 1.273093116 0.89850750 0.0560379553 83.0901289
112.729191 66 0.96590909 84.9886364 1.917397313 0.97240626 0.0064971714 84.0162301
163.795081 73 0.97727273 85.9886364 2.785970904 0.99733162 0.0200588896 84.9913047
198.660139 42 0.98863636 86.9886364 3.378986513 0.99963623 0.0109998685 85.9890001
209.375830 76 1.00000000 87.9886364 3.561248407 0.99981545 0.0001845478 86.9888209

PRUEBA SM

residuos_sw <- residuals(modelo_hrpice)
n_sw <- length(residuos_sw)
SW <- shapiro.test(residuos_sw)$statistic
print(SW)
##         W 
## 0.9413208
library(dplyr)
library(gt)
library(gtExtras)
residuos_sw %>%
  as_tibble() %>%
  mutate(posicion=row_number()) %>%
  arrange(value)  %>%
  mutate(dist1=row_number()/n())  %>%
  mutate(dist2=(row_number()-1/n())) %>%
  mutate(zi=as.vector(scale(value,center=TRUE))) %>%
  mutate(pi=pnorm(zi,lower.tail = TRUE)) %>%
  mutate(dif1=abs(dist1-pi)) %>%
  mutate(dist2=abs(dist2-pi)) %>%
  rename(residuales=value) -> tabla_sw

tabla_sw  %>%
  gt()  %>%
  tab_header("tabla para calsular sw")
tabla para calsular sw
residuales posicion dist1 dist2 zi pi dif1
-120.026447 81 0.01136364 0.9680366 -2.041515459 0.02059981 0.0092361731
-115.508697 77 0.02272727 1.9639103 -1.964673586 0.02472601 0.0019987418
-107.080889 24 0.03409091 2.9543577 -1.821326006 0.03427866 0.0001877487
-91.243980 48 0.04545455 3.9283002 -1.551957925 0.06033615 0.0148816002
-85.461169 12 0.05681818 4.9156076 -1.453598781 0.07302879 0.0162106057
-77.172687 32 0.06818182 5.8939810 -1.312620980 0.09465535 0.0264735301
-74.702719 54 0.07954545 6.8867026 -1.270609602 0.10193378 0.0223883300
-65.502849 39 0.09090909 7.8560247 -1.114130117 0.13261169 0.0417025941
-63.699108 69 0.10227273 8.8493321 -1.083450505 0.13930425 0.0370315271
-62.566594 83 0.11363636 9.8450145 -1.064187703 0.14362184 0.0299854747
-59.845223 36 0.12500000 10.8342737 -1.017900230 0.15436269 0.0293626861
-54.466158 13 0.13636364 11.8115195 -0.926408352 0.17711690 0.0407532663
-54.300415 14 0.14772727 12.8107863 -0.923589260 0.17785010 0.0301228311
-52.129801 15 0.15909091 13.8010079 -0.886669532 0.18762842 0.0285375141
-51.441108 17 0.17045455 14.7978373 -0.874955638 0.19079902 0.0203444766
-48.704980 47 0.18181818 15.7849192 -0.828417174 0.20371714 0.0218989601
-48.350295 29 0.19318182 16.7832073 -0.822384375 0.20542908 0.0122472664
-47.855859 11 0.20454545 17.7808066 -0.813974573 0.20782976 0.0032843043
-45.639765 1 0.21590909 18.7698449 -0.776281294 0.21879146 0.0028823668
-43.142550 9 0.22727273 19.7571030 -0.733806463 0.23153335 0.0042606233
-41.749618 57 0.23863636 20.7498197 -0.710114247 0.23881665 0.0001802823
-40.869022 27 0.25000000 21.7451514 -0.695136302 0.24348494 0.0065150566
-37.749811 34 0.26136364 22.7282264 -0.642082009 0.26040997 0.0009536682
-36.663785 71 0.27272727 23.7221945 -0.623609925 0.26644190 0.0062853771
-36.646568 79 0.28409091 24.7220983 -0.623317083 0.26653809 0.0175528221
-33.801248 37 0.29545455 25.7059641 -0.574921384 0.28267223 0.0127823120
-29.766931 16 0.30681818 26.6823141 -0.506302171 0.30632227 0.0004959124
-26.696234 22 0.31818182 27.6637482 -0.454073044 0.32488813 0.0067063089
-24.271531 23 0.32954545 28.6487714 -0.412831567 0.33986501 0.0103195566
-23.651448 86 0.34090909 29.6448991 -0.402284648 0.34373728 0.0028281851
-19.683427 88 0.35227273 30.6197458 -0.334793052 0.36889060 0.0166178738
-17.817835 10 0.36363636 31.6077148 -0.303061413 0.38092153 0.0172851663
-16.762094 60 0.37500000 32.6008543 -0.285104441 0.38778206 0.0127820638
-16.596960 21 0.38636364 33.5997780 -0.282295711 0.38885839 0.0024947507
-16.271207 58 0.39772727 34.5976522 -0.276755010 0.39098411 0.0067431583
-13.815798 56 0.40909091 35.5815286 -0.234991254 0.40710776 0.0019831485
-13.462160 75 0.42045455 36.5791927 -0.228976273 0.40944368 0.0110108666
-12.081520 4 0.43181818 37.5700429 -0.205493119 0.41859344 0.0132247451
-11.629207 51 0.44318182 38.5670355 -0.197799788 0.42160086 0.0215809622
-11.312669 74 0.45454545 39.5649281 -0.192415834 0.42370825 0.0308372092
-8.236558 3 0.46590909 40.5443438 -0.140094626 0.44429261 0.0216164775
-7.662789 70 0.47727273 41.5404858 -0.130335452 0.44815052 0.0291222111
-6.752801 67 0.48863636 42.5343574 -0.114857588 0.45427900 0.0343573625
-6.707262 31 0.50000000 43.5340504 -0.114083016 0.45458599 0.0454140074
-6.402439 85 0.51136364 44.5319948 -0.108898313 0.45664157 0.0547220642
-5.446904 82 0.52272727 45.5255439 -0.092645733 0.46309251 0.0596347676
-3.537785 43 0.53409091 46.5126277 -0.060173762 0.47600862 0.0580822876
-2.824941 61 0.54545455 47.5077978 -0.048049090 0.48083856 0.0646159857
-2.745208 68 0.55681818 48.5072574 -0.046692922 0.48137899 0.0754391961
-0.195089 65 0.56818182 49.4899601 -0.003318245 0.49867621 0.0695056040
1.399296 55 0.57954545 50.4791423 0.023800450 0.50949411 0.0700513452
5.363331 26 0.59090909 51.4522936 0.091224254 0.53634280 0.0545662924
6.700640 53 0.60227273 52.4432670 0.113970383 0.54536936 0.0569033628
7.386314 80 0.61363636 53.4386476 0.125632935 0.54998875 0.0636476093
9.099900 41 0.62500000 54.4271341 0.154779103 0.56150227 0.0634977329
12.433611 46 0.63636364 55.4048920 0.211481796 0.58374433 0.0526193043
16.718018 62 0.64772727 56.3767056 0.284354766 0.61193074 0.0357965328
18.093192 5 0.65909091 57.3677746 0.307744934 0.62086179 0.0382291219
18.801816 38 0.67045455 58.3631972 0.319797835 0.62543921 0.0450153400
19.168108 33 0.68181818 59.3608379 0.326028052 0.62779843 0.0540197476
19.219211 72 0.69318182 60.3605092 0.326897255 0.62812720 0.0650546167
20.334434 59 0.70454545 61.3533581 0.345865960 0.63527827 0.0692671805
24.909926 78 0.71590909 62.3245323 0.423689939 0.66410402 0.0518050676
26.236229 40 0.72727273 63.3163451 0.446248874 0.67229126 0.0549814685
30.924022 25 0.73863636 64.2880864 0.525982978 0.70054998 0.0380863808
32.253952 45 0.75000000 65.2802751 0.548603608 0.70836125 0.0416387548
32.529367 49 0.76136364 66.2786694 0.553288104 0.70996693 0.0513967091
32.675968 18 0.77272727 67.2778164 0.555781630 0.71081993 0.0619073452
33.275839 20 0.78409091 68.2743384 0.565984762 0.71429793 0.0697929786
36.031430 52 0.79545455 69.2586227 0.612854281 0.73001365 0.0654408934
37.147186 84 0.80681818 70.2523847 0.631832029 0.73625168 0.0705665028
40.320875 7 0.81818182 71.2350519 0.685812928 0.75358446 0.0645973596
44.334467 30 0.82954545 72.2140371 0.754079634 0.77459930 0.0549461574
46.907165 28 0.84090909 73.2011185 0.797838357 0.78751785 0.0533912405
54.418366 87 0.85227273 74.1659645 0.925595465 0.82267187 0.0296008528
55.091131 35 0.86363636 75.1630058 0.937038450 0.82563061 0.0380057535
55.470305 44 0.87500000 76.1613521 0.943487765 0.82728426 0.0477157353
62.939597 6 0.88636364 77.1308263 1.070532059 0.85781006 0.0285535797
66.478628 50 0.89772727 78.1177214 1.130727018 0.87091500 0.0268122757
67.426518 63 0.90909091 79.1143583 1.146849569 0.87427810 0.0348128083
67.603959 19 0.92045455 80.1137356 1.149867648 0.87490081 0.0455537393
69.707122 64 0.93181818 81.1065186 1.185640095 0.88211777 0.0497004123
69.843246 8 0.94318182 82.1060619 1.187955411 0.88257451 0.0606073068
74.848732 2 0.95454545 83.0901289 1.273093116 0.89850750 0.0560379553
112.729191 66 0.96590909 84.0162301 1.917397313 0.97240626 0.0064971714
163.795081 73 0.97727273 84.9913047 2.785970904 0.99733162 0.0200588896
198.660139 42 0.98863636 85.9890001 3.378986513 0.99963623 0.0109998685
209.375830 76 1.00000000 86.9888209 3.561248407 0.99981545 0.0001845478

#GRAFICO

library(fastGraph)
residuossw_graph <- scale(modelo_hrpice$residuals)
hist(residuossw_graph, freq=FALSE, main="distribucion de los residuos estandarizados")
curve(dnorm(x, mean=mean(residuossw_graph),sd=sd(residuossw_graph)),add=TRUE, col="blue", lwd=2)