setwd("C:/Users/marcogeovanni/Desktop/Modern Guide To Econometrics/Chapter 4")
library(readstata13)
Icecream <- read.dta13("icecream.dta") #Utilizamos la base de datos icecream.dta disponible en MIU
summary(Icecream)
##       time            cons            price            income     
##  Min.   : 1.00   Min.   :0.2560   Min.   :0.2600   Min.   :76.00  
##  1st Qu.: 8.25   1st Qu.:0.3113   1st Qu.:0.2685   1st Qu.:79.25  
##  Median :15.50   Median :0.3515   Median :0.2770   Median :83.50  
##  Mean   :15.50   Mean   :0.3594   Mean   :0.2753   Mean   :84.60  
##  3rd Qu.:22.75   3rd Qu.:0.3912   3rd Qu.:0.2815   3rd Qu.:89.25  
##  Max.   :30.00   Max.   :0.5480   Max.   :0.2920   Max.   :96.00  
##       temp      
##  Min.   :24.00  
##  1st Qu.:32.25  
##  Median :49.50  
##  Mean   :49.10  
##  3rd Qu.:63.75  
##  Max.   :72.00
attach(Icecream)
plot(time, temp/100) # Por motivos de escala se divide la variable temp para poder graficarlo con el resto de variables
lines(temp/100)
lines(cons, col="blue")
lines(price, col="orange") # Al terminar el gráfico se puede ver que los errores parece estar correlacionados con los errores del periodo anterior

E1 <- lm(cons~ price + income + temp) #corremos una regresión para explicar el consumo
summary(E1)
## 
## Call:
## lm(formula = cons ~ price + income + temp)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.065302 -0.011873  0.002737  0.015953  0.078986 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.1973149  0.2702161   0.730  0.47179    
## price       -1.0444131  0.8343570  -1.252  0.22180    
## income       0.0033078  0.0011714   2.824  0.00899 ** 
## temp         0.0034584  0.0004455   7.762  3.1e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.03683 on 26 degrees of freedom
## Multiple R-squared:  0.719,  Adjusted R-squared:  0.6866 
## F-statistic: 22.17 on 3 and 26 DF,  p-value: 2.451e-07
plot(time, fitted(E1))# graficamos el nuestros valores estimados
lines(fitted(E1))
library(lmtest) # activamos el paquete lmtest para realizar la prueba Durbin-Watson
## Warning: package 'lmtest' was built under R version 3.2.3
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric

dwtest(E1)# esta es la función de la prueba para autocorrelación
## 
##  Durbin-Watson test
## 
## data:  E1
## DW = 1.0212, p-value = 0.0003024
## alternative hypothesis: true autocorrelation is greater than 0
ResidualesE1 <- residuals(E1)#Definimos una variable para guardar los resisudales de E1
LagResidualesE1 <-c(NA, ResidualesE1[1:29])#Definimos esta variable para el lag de los residuales
Aux1 <- lm(ResidualesE1~LagResidualesE1-1)# corremos una regresión auxiliar para hacer pruebas t y X^2
summary(Aux1)
## 
## Call:
## lm(formula = ResidualesE1 ~ LagResidualesE1 - 1)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.063581 -0.014006 -0.000714  0.009123  0.080090 
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(>|t|)  
## LagResidualesE1   0.4006     0.1774   2.258   0.0319 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.03023 on 28 degrees of freedom
##   (1 observation deleted due to missingness)
## Multiple R-squared:  0.1541, Adjusted R-squared:  0.1238 
## F-statistic: 5.099 on 1 and 28 DF,  p-value: 0.03192
library("orcutt", lib.loc="~/R/win-library/3.2") #Descargamos el paquete Orcutt
## Warning: package 'orcutt' was built under R version 3.2.3
cochrane.orcutt(E1) #Calculamos GLS de la forma Cochrane.orcutt cuyo estimado es más eficiente bajo condiciones de autocorrelación
## $Cochrane.Orcutt
## 
## Call:
## lm(formula = YB ~ XB - 1)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.061510 -0.013400 -0.000524  0.013603  0.082052 
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(>|t|)    
## XB(Intercept)  0.1571481  0.2896292   0.543   0.5922    
## XBprice       -0.8923964  0.8108501  -1.101   0.2816    
## XBincome       0.0032027  0.0015461   2.072   0.0488 *  
## XBtemp         0.0035584  0.0005547   6.415 1.02e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.03191 on 25 degrees of freedom
## Multiple R-squared:  0.9823, Adjusted R-squared:  0.9795 
## F-statistic: 346.9 on 4 and 25 DF,  p-value: < 2.2e-16
## 
## 
## $rho
## [1] 0.4009259
## 
## $number.interaction
## [1] 10
temp_1 <- c(NA,temp[1:29])# Con esto construimos el lag de la variable temp, es decir la retrazamos un periodo 
E2 <- lm(cons~ price + income + temp + temp_1) #Esta es la nueva especificación incluyendo el Lag de temperatura
summary(E2)
## 
## Call:
## lm(formula = cons ~ price + income + temp + temp_1)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.049070 -0.015391 -0.006745  0.014766  0.080892 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.1894822  0.2323169   0.816  0.42274    
## price       -0.8383021  0.6880205  -1.218  0.23490    
## income       0.0028673  0.0010533   2.722  0.01189 *  
## temp         0.0053321  0.0006704   7.953  3.5e-08 ***
## temp_1      -0.0022039  0.0007307  -3.016  0.00597 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.02987 on 24 degrees of freedom
##   (1 observation deleted due to missingness)
## Multiple R-squared:  0.8285, Adjusted R-squared:  0.7999 
## F-statistic: 28.98 on 4 and 24 DF,  p-value: 7.099e-09
dwtest(E2)# volvemos a ejecutar otra prueba de Autocorrelación
## 
##  Durbin-Watson test
## 
## data:  E2
## DW = 1.5822, p-value = 0.02876
## alternative hypothesis: true autocorrelation is greater than 0