Estamos interesados en la relación entre ingresos y gastos en alimentos para una muestra de hogares belgas de clase trabajadora en 1857 (los datos de Engel).

library(quantreg)
## Warning: package 'SparseM' was built under R version 4.1.1
library(ggplot2)

Datos

Leemos ahora el conjunto de datos engel:

datos=data(engel)
datos=engel
head(datos)
##     income  foodexp
## 1 420.1577 255.8394
## 2 541.4117 310.9587
## 3 901.1575 485.6800
## 4 639.0802 402.9974
## 5 750.8756 495.5608
## 6 945.7989 633.7978

#Diagrama me dispercion

Ejercicio

Use el conjunto de datos de engel para ajustar un modelo usando regresión lineal simple mediante el enfoque de mímimos cuadrados Ordinarios (MCO). Realice lo siguiente: a) Realice el ajuste a este conjunto de datos, explique y concluya b) Realice la prueba de hipótesis para la pendiente y concluya. c) Verifique los supuestos y concluya en cada caso.

##Diagrama de dispersion

En r base

plot(datos)

#Solucion:a)

AJUSTE DEL MODELO

ajuste de la linea de regresion

regresion <- lm (foodexp ~ income, data = datos)

summary(regresion)
## 
## Call:
## lm(formula = foodexp ~ income, data = datos)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -725.70  -60.24   -4.32   53.41  515.77 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 147.47539   15.95708   9.242   <2e-16 ***
## income        0.48518    0.01437  33.772   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 114.1 on 233 degrees of freedom
## Multiple R-squared:  0.8304, Adjusted R-squared:  0.8296 
## F-statistic:  1141 on 1 and 233 DF,  p-value: < 2.2e-16

intervalos de confianza

confint(regresion,level=0.95)
##                   2.5 %     97.5 %
## (Intercept) 116.0367916 178.913985
## income        0.4568738   0.513483
confint(regresion,level=0.99)
##                   0.5 %      99.5 %
## (Intercept) 106.0333557 188.9174213
## income        0.4478676   0.5224893

muestra un anolisis de varianza

anova(regresion)
## Analysis of Variance Table
## 
## Response: foodexp
##            Df   Sum Sq  Mean Sq F value    Pr(>F)    
## income      1 14850458 14850458  1140.5 < 2.2e-16 ***
## Residuals 233  3033805    13021                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Predicciones

datos$predicciones <- predict(regresion) 

ggplot(datos, aes(x=income, y=foodexp)) + 
  geom_point(aes(colour = income)) + theme_light()+
  geom_smooth(method='lm', formula=y~x, se=FALSE, col='red') +
  geom_segment(aes(xend=income, yend=predicciones), col='red',
               lty='dashed') +
  geom_point(aes(y=predicciones), col='red') +
  labs(x = "income",
       y = "foodexp",
       title = "diagrama de dispersion income vs foodexp")

intervalos de confianza y prediccion para todas las predicciones

futuro_y <- predict(object=regresion, 
                    interval="prediction", level=0.95)
## Warning in predict.lm(object = regresion, interval = "prediction", level = 0.95): predictions on current data refer to _future_ responses
nuevos_datos <- cbind(datos, futuro_y)

library(ggplot2)

ggplot(nuevos_datos, aes(x=income, y=foodexp)) + 
  geom_point() +
  geom_smooth(method="lm", formula=y~x, se=TRUE, level=0.95, 
              col='blue', fill='green')  +
  labs(x = "income",
       y = "foodexp",
       title = "diagrama de dispersion income vs foodexp") +
  theme_light()

VALIDACIoN DEL MODELO

residuos = rstandard(regresion) 
valores.ajustados = fitted(regresion)

#prueba de normalidad

library(car)
## Loading required package: carData
qqPlot(regresion)

## [1] 105 138
shapiro.test(residuos) 
## 
##  Shapiro-Wilk normality test
## 
## data:  residuos
## W = 0.85318, p-value = 3.54e-14

homogeneidad

datos2=cbind(datos, residuos,valores.ajustados)
datos2
##        income   foodexp predicciones     residuos valores.ajustados
## 1    420.1577  255.8394     351.3268 -0.840721146          351.3268
## 2    541.4117  310.9587     410.1567 -0.872542485          410.1567
## 3    901.1575  485.6800     584.6975 -0.869651204          584.6975
## 4    639.0802  402.9974     457.5433 -0.479491418          457.5433
## 5    750.8756  495.5608     511.7840 -0.142538992          511.7840
## 6    945.7989  633.7978     606.3566  0.241000412          606.3566
## 7    829.3979  630.7566     549.8813  0.710406048          549.8813
## 8    979.1648  700.4409     622.5450  0.684107890          622.5450
## 9   1309.8789  830.9586     783.0004  0.421545252          783.0004
## 10  1492.3987  815.3602     871.5551 -0.494546713          871.5551
## 11   502.8390  338.0014     391.4420 -0.470195398          391.4420
## 12   616.7168  412.3613     446.6931 -0.301834677          446.6931
## 13   790.9225  520.0006     531.2139 -0.098507836          531.2139
## 14   555.8786  452.4015     417.1757  0.309813639          417.1757
## 15   713.4412  512.7201     493.6217  0.167825324          493.6217
## 16   838.7561  658.8395     554.4218  0.917182919          554.4218
## 17   535.0766  392.5995     407.0830 -0.127402447          407.0830
## 18   596.4408  443.5586     436.8556  0.058938275          436.8556
## 19   924.5619  640.1164     596.0529  0.386990943          596.0529
## 20   487.7583  333.8394     384.1252 -0.442489779          384.1252
## 21   692.6397  466.9583     483.5292 -0.145628868          483.5292
## 22   997.8770  543.3969     631.6238 -0.774839624          631.6238
## 23   506.9995  317.7198     393.4606 -0.666381160          393.4606
## 24   654.1587  424.3209     464.8591 -0.356325894          464.8591
## 25   933.9193  518.9617     600.5929 -0.716926370          600.5929
## 26   433.6813  338.0014     357.8882 -0.175073009          357.8882
## 27   587.5962  419.6412     432.5644 -0.113637199          432.5644
## 28   896.4746  476.3200     582.4255 -0.931909169          582.4255
## 29   454.4782  386.3602     367.9784  0.161794127          367.9784
## 30   584.9989  423.2783     431.3042 -0.070574904          431.3042
## 31   800.7990  503.3572     536.0058 -0.286806598          536.0058
## 32   502.4369  354.6389     391.2469 -0.322095578          391.2469
## 33   713.5197  497.3182     493.6597  0.032148113          493.6597
## 34   906.0006  588.5195     587.0473  0.012929245          587.0473
## 35   880.5969  654.5971     574.7220  0.701548358          574.7220
## 36   796.8289  550.7274     534.0796  0.146247086          534.0796
## 37   854.8791  528.3770     562.2443 -0.297472486          562.2443
## 38  1167.3716  640.4813     713.8589 -0.644601900          713.8589
## 39   523.8000  401.3204     401.6119 -0.002564425          401.6119
## 40   670.7792  435.9990     472.9230 -0.324529711          472.9230
## 41   377.0584  276.5606     330.4160 -0.474361937          330.4160
## 42   851.5430  588.3488     560.6257  0.243507207          560.6257
## 43  1121.0937  664.1978     691.4059 -0.238986919          691.4059
## 44   625.5179  444.8602     450.9632 -0.053653289          450.9632
## 45   805.5377  462.8995     538.3049 -0.662400663          538.3049
## 46   558.5812  377.7792     418.4869 -0.358021171          418.4869
## 47   884.4005  553.1504     576.5674 -0.205671650          576.5674
## 48  1257.4989  810.8962     757.5867  0.468463736          757.5867
## 49  2051.1789 1067.9541    1142.6632 -0.662167809         1142.6632
## 50  1466.3330 1049.8788     858.9085  1.680299671          858.9085
## 51   730.0989  522.7012     501.7036  0.184501491          501.7036
## 52   800.7990  572.0807     536.0058  0.316904972          536.0058
## 53  1245.6964  907.3969     751.8604  1.366728505          751.8604
## 54  1201.0002  811.5776     730.1748  0.715178946          730.1748
## 55   634.4002  427.7975     455.2727 -0.241529191          455.2727
## 56   956.2315  649.9985     611.4183  0.338826239          611.4183
## 57  1148.6010  860.6002     704.7518  1.369013438          704.7518
## 58  1768.8236 1143.4211    1005.6705  1.215771099         1005.6705
## 59  2822.5330 2032.6792    1516.9075  4.656920794         1516.9075
## 60   922.3548  590.6183     594.9821 -0.038324812          594.9821
## 61  2293.1920 1570.3911    1260.0827  2.763281894         1260.0827
## 62   627.4726  483.4800     451.9116  0.277523511          451.9116
## 63   889.9809  600.4804     579.2749  0.186246392          579.2749
## 64  1162.2000  696.2021     711.3497 -0.133065778          711.3497
## 65  1197.0794  774.7962     728.2725  0.408736867          728.2725
## 66   530.7972  390.5984     405.0067 -0.126744542          405.0067
## 67  1142.1526  612.5619     701.6232 -0.782325095          701.6232
## 68  1088.0039  708.7622     675.3514  0.293450542          675.3514
## 69   484.6612  296.9192     382.6225 -0.754164767          382.6225
## 70  1536.0201 1071.4627     892.7192  1.573628472          892.7192
## 71   678.8974  496.5976     476.8618  0.173453972          476.8618
## 72   671.8802  503.3974     473.4571  0.263147914          473.4571
## 73   690.4683  357.6411     482.4757 -1.097084316          482.4757
## 74   860.6948  430.3376     565.0659 -1.183369679          565.0659
## 75   873.3095  624.6990     571.1863  0.470011514          571.1863
## 76   894.4598  582.5413     581.4480  0.009601879          581.4480
## 77  1148.6470  580.2215     704.7741 -1.094103422          704.7741
## 78   926.8762  543.8807     597.1757 -0.468066024          597.1757
## 79   839.0414  588.6372     554.5602  0.299325001          554.5602
## 80   829.4974  627.9999     549.9296  0.685767144          549.9296
## 81  1264.0043  712.1012     760.7430 -0.427458870          760.7430
## 82  1937.9771  968.3949    1087.7401 -1.055830154         1087.7401
## 83   698.8317  482.5816     486.5335 -0.034729025          486.5335
## 84   920.4199  593.1694     594.0433 -0.007675020          594.0433
## 85  1897.5711 1033.5658    1068.1360 -0.305651365         1068.1360
## 86   891.6824  693.6795     580.1004  0.997554918          580.1004
## 87   889.6784  693.6795     579.1281  1.006097599          579.1281
## 88  1221.4818  761.2791     740.1120  0.185980843          740.1120
## 89   544.5991  361.3981     411.7031 -0.442472040          411.7031
## 90  1031.4491  628.4522     647.9122 -0.170907794          647.9122
## 91  1462.9497  771.4486     857.2670 -0.755075307          857.2670
## 92   830.4353  757.1187     550.3847  1.815942036          550.3847
## 93   975.0415  821.5970     620.5445  1.765712054          620.5445
## 94  1337.9983 1022.3202     796.6433  1.983967503          796.6433
## 95   867.6427  679.4407     568.4369  0.974975421          568.4369
## 96   725.7459  538.7491     499.5917  0.344074601          499.5917
## 97   989.0056  679.9981     627.3196  0.462640809          627.3196
## 98  1525.0005  977.0033     887.3727  0.789016516          887.3727
## 99   672.1960  561.2015     473.6104  0.769844744          473.6104
## 100  923.3977  728.3997     595.4880  1.167308168          595.4880
## 101  472.3215  372.3186     376.6356 -0.037992050          376.6356
## 102  590.7601  361.5210     434.0994 -0.638188714          434.0994
## 103  940.9218  517.9196     603.9903 -0.755912730          603.9903
## 104  643.3571  459.8177     459.6184  0.001751561          459.6184
## 105 2551.6615  863.9199    1385.4865 -4.673078260         1385.4865
## 106 1795.3226  831.4407    1018.5272 -1.651766183         1018.5272
## 107 1165.7734  534.7610     713.0835 -1.566506986          713.0835
## 108  815.6212  392.0502     543.1972 -1.327717817          543.1972
## 109 1264.2066  934.9752     760.8411  1.530271211          760.8411
## 110 1095.4056  813.3081     678.9426  1.180163412          678.9426
## 111  447.4479  263.7100     364.5674 -0.887789188          364.5674
## 112 1178.9742  769.0838     719.4882  0.435699497          719.4882
## 113  975.8023  630.5863     620.9136  0.084948763          620.9136
## 114 1017.8522  645.9874     641.3153  0.041032250          641.3153
## 115  423.8798  319.5584     353.1327 -0.295596356          353.1327
## 116  558.7767  348.4518     418.5818 -0.616787115          418.5818
## 117  943.2487  614.5068     605.1193  0.082445108          605.1193
## 118 1348.3002  662.0096     801.6416 -1.227603940          801.6416
## 119 2340.6174 1504.3708    1283.0924  1.972517025         1283.0924
## 120  587.1792  406.2180     432.3621 -0.229891869          432.3621
## 121 1540.9741  692.1689     895.1228 -1.786851222          895.1228
## 122 1115.8481  588.1371     688.8608 -0.884715304          688.8608
## 123 1044.6843  511.2609     654.3337 -1.256552242          654.3337
## 124 1389.7929  700.5600     821.7729 -1.065941368          821.7729
## 125 2497.7860 1301.1451    1359.3472 -0.520757052         1359.3472
## 126 1585.3809  879.0660     916.6680 -0.331193083          916.6680
## 127 1862.0438  912.8851    1050.8989 -1.219617357         1050.8989
## 128 2008.8546 1509.7812    1122.1283  3.433409428         1122.1283
## 129  697.3099  484.0605     485.7951 -0.015243545          485.7951
## 130  571.2517  399.6703     424.6344 -0.219538806          424.6344
## 131  598.3465  444.1001     437.7802  0.055568893          437.7802
## 132  461.0977  248.8101     371.1901 -1.077115085          371.1901
## 133  977.1107  527.8014     621.5484 -0.823318768          621.5484
## 134  883.9849  500.6313     576.3658 -0.665176900          576.3658
## 135  718.3594  436.8107     496.0079 -0.520178158          496.0079
## 136  543.8971  374.7990     411.3625 -0.321605450          411.3625
## 137 1587.3480  726.3921     917.6224 -1.684361234          917.6224
## 138 4957.8130 1827.2000    2552.8993 -7.367024307         2552.8993
## 139  969.6838  523.4911     617.9450 -0.829527560          617.9450
## 140  419.9980  334.9998     351.2494 -0.143069732          351.2494
## 141  561.9990  473.2009     420.1452  0.466610420          420.1452
## 142  689.5988  581.2029     482.0539  0.871355964          482.0539
## 143 1398.5203  929.7540     826.0072  0.912396742          826.0072
## 144  820.8168  591.1974     545.7180  0.399498616          545.7180
## 145  875.1716  637.5483     572.0898  0.574931394          572.0898
## 146 1392.4499  674.9509     823.0620 -1.302505543          823.0620
## 147 1256.3174  776.7589     757.0135  0.173514596          757.0135
## 148 1362.8590  959.5170     808.7052  1.326008132          808.7052
## 149 1999.2552 1250.9643    1117.4709  1.182152788         1117.4709
## 150 1209.4730  737.8201     734.2856  0.031053835          734.2856
## 151 1125.0356  810.6772     693.3184  1.030851740          693.3184
## 152 1827.4010  983.0009    1034.0909 -0.451261586         1034.0909
## 153 1014.1540  708.8968     639.5210  0.609286827          639.5210
## 154  880.3944  633.1200     574.6238  0.513776502          574.6238
## 155 2432.3910 1424.8047    1327.6190  0.868167778         1327.6190
## 156 1177.8547  830.9586     718.9451  0.984039958          718.9451
## 157 1222.5939  925.5795     740.6516  1.624845422          740.6516
## 158 1519.5811 1162.0024     884.7434  2.440593149          884.7434
## 159  687.6638  383.4580     481.1150 -0.858251138          481.1150
## 160  953.1192  621.1173     609.9083  0.098442413          609.9083
## 161  953.1192  621.1173     609.9083  0.098442413          609.9083
## 162  953.1192  621.1173     609.9083  0.098442413          609.9083
## 163  939.0418  548.6002     603.0782 -0.478451237          603.0782
## 164 1283.4025  745.2353     770.1546 -0.219007423          770.1546
## 165 1511.5789  837.8005     880.8608 -0.379016073          880.8608
## 166 1342.5821  795.3402     798.8673 -0.031007570          798.8673
## 167  511.7980  418.5976     395.7887  0.200669071          395.7887
## 168  689.7988  508.7974     482.1509  0.234178766          482.1509
## 169 1532.3074  883.2780     890.9179 -0.067258036          890.9179
## 170 1056.0808  742.5276     659.8630  0.726019359          659.8630
## 171  387.3195  242.3202     335.3945 -0.819723993          335.3945
## 172  387.3195  242.3202     335.3945 -0.819723993          335.3945
## 173  410.9987  266.0010     346.8831 -0.712187641          346.8831
## 174  832.7554  614.7588     551.5104  0.555568382          551.5104
## 175  614.9986  385.3184     445.8594 -0.532264334          445.8594
## 176  887.4658  515.6200     578.0547 -0.548361912          578.0547
## 177 1024.8177  708.4787     644.6948  0.560179691          644.6948
## 178 1006.4353  734.2356     635.7761  0.864708859          635.7761
## 179  726.0000  433.0010     499.7149 -0.586211346          499.7149
## 180  494.4174  327.4188     387.3560 -0.527390030          387.3560
## 181  748.6413  429.0399     510.7000 -0.717478711          510.7000
## 182  987.6417  619.6408     626.6578 -0.061625800          626.6578
## 183  788.0961  400.7990     529.8426 -1.133645695          529.8426
## 184  831.7983  620.8006     551.0460  0.612719725          551.0460
## 185 1139.4945  819.9964     700.3335  1.051126539          700.3335
## 186  507.5169  360.8780     393.7116 -0.288875088          393.7116
## 187  576.1972  395.7608     427.0338 -0.275011993          427.0338
## 188  696.5991  442.0001     485.4502 -0.381842531          485.4502
## 189  650.8180  404.0384     463.2382 -0.520368439          463.2382
## 190  949.5802  670.7993     608.1912  0.549850182          608.1912
## 191  497.1193  297.5702     388.6669 -0.801547111          388.6669
## 192  570.1674  353.4882     424.1083 -0.621050907          424.1083
## 193  724.7306  383.9376     499.0990 -1.011922049          499.0990
## 194  408.3399  284.8008     345.5931 -0.535304955          345.5931
## 195  638.6713  431.1000     457.3449 -0.230709464          457.3449
## 196 1225.7890  801.3518     742.2018  0.519720216          742.2018
## 197  715.3701  448.4513     494.5575 -0.405150874          494.5575
## 198  800.4708  577.9111     535.8465  0.369522482          535.8465
## 199  975.5974  570.5210     620.8142 -0.441691924          620.8142
## 200 1613.7565  865.3205     930.4352 -0.573682038          930.4352
## 201  608.5019  444.5578     442.7074  0.016268901          442.7074
## 202  958.6634  680.4198     612.5982  0.595635375          612.5982
## 203  835.9426  576.2779     553.0567  0.203971057          553.0567
## 204  873.7375  631.7982     571.3940  0.530540034          571.3940
## 205  951.4432  608.6419     609.0951 -0.003980669          609.0951
## 206  473.0022  300.9999     376.9658 -0.668541031          376.9658
## 207  601.0030  377.9984     439.0691 -0.536965714          439.0691
## 208  713.9979  397.0015     493.8918 -0.851411823          493.8918
## 209  829.2984  588.5195     549.8331  0.339820536          549.8331
## 210  959.7953  681.7616     613.1473  0.602595855          613.1473
## 211 1212.9613  807.3603     735.9780  0.627168165          735.9780
## 212  958.8743  696.8011     612.7005  0.738603049          612.7005
## 213 1129.4431  811.1962     695.4568  1.016637433          695.4568
## 214 1943.0419 1305.7201    1090.1974  1.906849799         1090.1974
## 215  539.6388  442.0001     409.2965  0.287663157          409.2965
## 216  463.5990  353.6013     372.4036 -0.165483453          372.4036
## 217  562.6400  468.0008     420.4562  0.418139960          420.4562
## 218  736.7584  526.7573     504.9347  0.191746390          504.9347
## 219 1415.4461  890.2390     834.2193  0.492720162          834.2193
## 220 2208.7897 1318.8033    1219.1325  0.886012281         1219.1325
## 221  636.0009  331.0005     456.0493 -1.099271449          456.0493
## 222  759.4010  416.4015     515.9204 -0.874354639          515.9204
## 223 1078.8382  596.8406     670.9044 -0.650501877          670.9044
## 224  499.7510  408.4992     389.9438  0.163263072          389.9438
## 225 1020.0225  775.0209     642.3683  1.165012977          642.3683
## 226 1595.1611 1138.1620     921.4131  1.909275582          921.4131
## 227  776.5958  485.5198     524.2629 -0.340370254          524.2629
## 228 1230.9235  772.7611     744.6929  0.246625790          744.6929
## 229 1807.9520  993.9630    1024.6547 -0.271018651         1024.6547
## 230  415.4407  305.4390     349.0383 -0.383887517          349.0383
## 231  440.5174  306.5191     361.2049 -0.481396458          361.2049
## 232  541.2006  299.1993     410.0542 -0.975077778          410.0542
## 233  581.3599  468.0008     429.5387  0.338220873          429.5387
## 234  743.0772  522.6019     508.0004  0.128293603          508.0004
## 235 1057.6767  750.3202     660.6373  0.787660632          660.6373
library(ggplot2)
ggplot(datos2, aes(x=valores.ajustados, y=residuos)) + 
  geom_hline(yintercept = 0)+
  geom_point(col='red') + theme_light()+
  labs(x = "valores.ajustados",
       y = "residuos",
       title = "Residuos Vs Ajustados")

library(lmtest) 
## Warning: package 'lmtest' was built under R version 4.1.1
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
bptest(regresion) #prueba de homogeneidad de Breusch-Pagan
## 
##  studentized Breusch-Pagan test
## 
## data:  regresion
## BP = 109.26, df = 1, p-value < 2.2e-16

independencia - AUTOcorrelacion DURWIN WATSON

library(lmtest)
dwtest(regresion,alternative = "two.sided")
## 
##  Durbin-Watson test
## 
## data:  regresion
## DW = 1.4108, p-value = 5.354e-06
## alternative hypothesis: true autocorrelation is not 0
acf(residuos)

plot(as.ts(residuos),col="red")
abline(h=0,col="blue")

Residuals, Fitted, leverage…

par(mfrow = c(2,2))
plot(regresion,which=1:3)      

library (car)
residualPlots(regresion, las=1)

##            Test stat Pr(>|Test stat|)    
## income         -8.01        5.546e-14 ***
## Tukey test     -8.01        1.147e-15 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
marginalModelPlots(regresion)

#Solucion:b)

#Solucion:c)