1. Estime el siguiente modelo:
price = ˆα + ˆα1(lotsize) + ˆα2(sqrft) + ˆα3(bdrms) + E
library(stargazer)
##
## Please cite as:
## Hlavac, Marek (2022). stargazer: Well-Formatted Regression and Summary Statistics Tables.
## R package version 5.2.3. https://CRAN.R-project.org/package=stargazer
stargazer(modelo, type = "text",
title = "Resultados de la regresiĂłn",
dep.var.labels = "Precio",
covariate.labels = c("Tamaño del lote", "Tamaño de la casa", "Número de habitaciones"),
digits = 4)
##
## Resultados de la regresiĂłn
## ==================================================
## Dependent variable:
## ---------------------------
## Precio
## --------------------------------------------------
## Tamaño del lote 0.0021***
## (0.0006)
##
## Tamaño de la casa 0.1228***
## (0.0132)
##
## NĂşmero de habitaciones 13.8525
## (9.0101)
##
## Constant -21.7703
## (29.4750)
##
## --------------------------------------------------
## Observations 88
## R2 0.6724
## Adjusted R2 0.6607
## Residual Std. Error 59.8335 (df = 84)
## F Statistic 57.4602*** (df = 3; 84)
## ==================================================
## Note: *p<0.1; **p<0.05; ***p<0.01
2. Verique el supuesto de normalidad, a través de:
a) La prueba JB
library(moments) # Para calcular asimetrĂa y curtosis
residuos <- modelo$residuals # Extrae los residuos del modelo
n <- length(residuos) # NĂşmero de observaciones
S <- skewness(residuos) # Calcula la asimetrĂa
K <- kurtosis(residuos) # Calcula la curtosis
JB <- (n/6)*(S^2 + ((K-3)^2)/4) # FĂłrmula de Jarque-Bera
JB # Muestra el estadĂstico JB
## [1] 32.27791
library(tseries) # LibrerĂa para prueba JB
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
jb <- jarque.bera.test(residuos) # Aplica prueba JB
JB <- (n/6)*(S^2 + ((K-3)^2)/4)
library(fastGraph) # Para graficar distribuciĂłn
gl <- 2
JB <- as.numeric(JB)
alpha <- 0.05 # Nivel de significancia
gl <- 2 # Grados de libertad
VC <- qchisq(1-alpha, gl) # Valor crĂtico chi-cuadrado
shadeDist("dchisq",
parm1 = gl,
xshade = JB,
lower.tail = FALSE,
sub = paste("VC:", round(VC,2), " JB:", round(JB,2)))

La gráfica de la distribución chi-cuadrado con 2 grados de libertad
muestra la regiĂłn crĂtica en la cola derecha. El estadĂstico de
Jarque-Bera (JB) se ubica en comparaciĂłn con el valor crĂtico (VC). Si
el valor de JB es mayor que el valor crĂtico, se rechaza la hipĂłtesis
nula de normalidad. En caso contrario, no se rechaza. Por lo tanto, al
observar la posiciĂłn del estadĂstico JB en la gráfica, se concluye que
[rechazamos / no rechazamos] la hipĂłtesis de que los residuos siguen una
distribuciĂłn normal.
b) La prueba KS
library(dplyr) # ManipulaciĂłn de datos
##
## 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) # Tablas
tabla_ks <- data.frame(residuos) %>% # Convierte residuos a data frame
mutate(posicion = row_number()) %>% # Crea Ăndice
arrange(residuos) %>% # Ordena de menor a mayor
mutate(F_emp = row_number()/n()) %>% # FunciĂłn empĂrica
mutate(F_emp_ant = (row_number()-1)/n()) %>% # FunciĂłn empĂrica anterior
mutate(z = (residuos - mean(residuos))/sd(residuos)) %>% # Estandariza
mutate(F_teo = pnorm(z)) %>% # DistribuciĂłn normal teĂłrica
mutate(D1 = abs(F_emp - F_teo)) %>% # Diferencia 1
mutate(D2 = abs(F_emp_ant - F_teo)) %>% # Diferencia 2
rename(Residuos = residuos) # Renombra
tabla_ks %>%
gt() %>%
tab_header(title = "Cálculo manual KS") %>%
tab_source_note(source_note = "Fuente: ElaboraciĂłn propia")
| Cálculo manual KS |
| Residuos |
posicion |
F_emp |
F_emp_ant |
z |
F_teo |
D1 |
D2 |
| -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 |
D <- max(c(tabla_ks$D1, tabla_ks$D2)) # Máxima diferencia
D
## [1] 0.0754392
ks <- ks.test(residuos, "pnorm", mean(residuos), sd(residuos)) # Prueba KS
ks
##
## Exact one-sample Kolmogorov-Smirnov test
##
## data: residuos
## D = 0.075439, p-value = 0.67
## alternative hypothesis: two-sided
c) La prueba SW
tabla_sw <- data.frame(residuos) %>% # Data frame
arrange(residuos) %>% # Ordena
mutate(posicion = row_number()) %>% # ĂŤndice
rename(Residuos = residuos) # Renombra
tabla_sw %>%
gt() %>%
tab_header(title = "Ordenamiento residuos (SW)") %>%
tab_source_note(source_note = "Fuente: ElaboraciĂłn propia")
| Ordenamiento residuos (SW) |
| Residuos |
posicion |
| -120.026447 |
1 |
| -115.508697 |
2 |
| -107.080889 |
3 |
| -91.243980 |
4 |
| -85.461169 |
5 |
| -77.172687 |
6 |
| -74.702719 |
7 |
| -65.502849 |
8 |
| -63.699108 |
9 |
| -62.566594 |
10 |
| -59.845223 |
11 |
| -54.466158 |
12 |
| -54.300415 |
13 |
| -52.129801 |
14 |
| -51.441108 |
15 |
| -48.704980 |
16 |
| -48.350295 |
17 |
| -47.855859 |
18 |
| -45.639765 |
19 |
| -43.142550 |
20 |
| -41.749618 |
21 |
| -40.869022 |
22 |
| -37.749811 |
23 |
| -36.663785 |
24 |
| -36.646568 |
25 |
| -33.801248 |
26 |
| -29.766931 |
27 |
| -26.696234 |
28 |
| -24.271531 |
29 |
| -23.651448 |
30 |
| -19.683427 |
31 |
| -17.817835 |
32 |
| -16.762094 |
33 |
| -16.596960 |
34 |
| -16.271207 |
35 |
| -13.815798 |
36 |
| -13.462160 |
37 |
| -12.081520 |
38 |
| -11.629207 |
39 |
| -11.312669 |
40 |
| -8.236558 |
41 |
| -7.662789 |
42 |
| -6.752801 |
43 |
| -6.707262 |
44 |
| -6.402439 |
45 |
| -5.446904 |
46 |
| -3.537785 |
47 |
| -2.824941 |
48 |
| -2.745208 |
49 |
| -0.195089 |
50 |
| 1.399296 |
51 |
| 5.363331 |
52 |
| 6.700640 |
53 |
| 7.386314 |
54 |
| 9.099900 |
55 |
| 12.433611 |
56 |
| 16.718018 |
57 |
| 18.093192 |
58 |
| 18.801816 |
59 |
| 19.168108 |
60 |
| 19.219211 |
61 |
| 20.334434 |
62 |
| 24.909926 |
63 |
| 26.236229 |
64 |
| 30.924022 |
65 |
| 32.253952 |
66 |
| 32.529367 |
67 |
| 32.675968 |
68 |
| 33.275839 |
69 |
| 36.031430 |
70 |
| 37.147186 |
71 |
| 40.320875 |
72 |
| 44.334467 |
73 |
| 46.907165 |
74 |
| 54.418366 |
75 |
| 55.091131 |
76 |
| 55.470305 |
77 |
| 62.939597 |
78 |
| 66.478628 |
79 |
| 67.426518 |
80 |
| 67.603959 |
81 |
| 69.707122 |
82 |
| 69.843246 |
83 |
| 74.848732 |
84 |
| 112.729191 |
85 |
| 163.795081 |
86 |
| 198.660139 |
87 |
| 209.375830 |
88 |
| Fuente: ElaboraciĂłn propia |
sw <- shapiro.test(residuos) # Prueba SW
sw
##
## Shapiro-Wilk normality test
##
## data: residuos
## W = 0.94132, p-value = 0.0005937
library(fastGraph)
qqnorm(residuos) # Gráfico normal
qqline(residuos) # LĂnea de referencia
