#install.packages("WDI")
#install.packages("wbstats")
library(WDI)
library(wbstats)
#install.packages("tidyverse")
library(tidyverse)
#install.packages("plm") #paquete para realizar modelos lineales para datos de panel
library(plm)
#install.packages("gplots")
library(gplots)
library(readxl)
library(lmtest)
gdp <- wb_data(country=c("MX","US","CA"), indicator=c("NY.GDP.PCAP.CD",
"SM.POP.NETM"), start_date=1950, end_date=2025)
#generar conjunto de datos de panel
panel_1 <- select(gdp, country, date, NY.GDP.PCAP.CD, SM.POP.NETM)
panel_1 <- subset(panel_1, date == 1960 | date == 1970 | date== 1980 | date== 1990 | date==2000 | date==2010 | date==2020)
panel_1 <- pdata.frame(panel_1, index = c("country","date"))
plotmeans(NY.GDP.PCAP.CD ~ country, main= "Prueba de heterogenidad entre paises para el PIB", data=panel_1)
plotmeans(SM.POP.NETM ~ country, main= "Prueba de heterogenidad entre paises para la migracion neta", data=panel_1)
#si la linea sale casi horizontal, hay poca o nula heterogenidad, por lo que no hay diferencia sitematica que ajustar
#si la linea sale quebrada, sube y baja, hay mucha heterogenidad, por lo que hay que ajustar
#Modelo 1. Regresión agrupada (pooled) ##para poca o nula heterogenidad
#asume que no hay heterogenidad no observada
pooled <- plm(NY.GDP.PCAP.CD ~ SM.POP.NETM, data = panel_1, model="pooling")
summary(pooled)
## Pooling Model
##
## Call:
## plm(formula = NY.GDP.PCAP.CD ~ SM.POP.NETM, data = panel_1, model = "pooling")
##
## Balanced Panel: n = 3, T = 7, N = 21
##
## Residuals:
## Min. 1st Qu. Median 3rd Qu. Max.
## -21506.0 -10924.8 -3728.9 5274.5 45389.3
##
## Coefficients:
## Estimate Std. Error t-value Pr(>|t|)
## (Intercept) 1.2873e+04 4.2134e+03 3.0553 0.006511 **
## SM.POP.NETM 1.8616e-02 7.2324e-03 2.5740 0.018588 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Total Sum of Squares: 7259500000
## Residual Sum of Squares: 5382600000
## R-Squared: 0.25855
## Adj. R-Squared: 0.21952
## F-statistic: 6.62533 on 1 and 19 DF, p-value: 0.018588
#Modelo 2. Efectos fijos (within)
#Cuando las diferencias no observadas son constante en el tiempo
within <- plm(NY.GDP.PCAP.CD ~ SM.POP.NETM, data = panel_1, model="within")
summary(within)
## Oneway (individual) effect Within Model
##
## Call:
## plm(formula = NY.GDP.PCAP.CD ~ SM.POP.NETM, data = panel_1, model = "within")
##
## Balanced Panel: n = 3, T = 7, N = 21
##
## Residuals:
## Min. 1st Qu. Median 3rd Qu. Max.
## -20886.56 -9903.27 -403.03 3407.39 44059.72
##
## Coefficients:
## Estimate Std. Error t-value Pr(>|t|)
## SM.POP.NETM 0.013921 0.014345 0.9705 0.3454
##
## Total Sum of Squares: 5256100000
## Residual Sum of Squares: 4980200000
## R-Squared: 0.052492
## Adj. R-Squared: -0.11471
## F-statistic: 0.94181 on 1 and 17 DF, p-value: 0.34542
#Prueba F
pFtest(within, pooled)
##
## F test for individual effects
##
## data: NY.GDP.PCAP.CD ~ SM.POP.NETM
## F = 0.68685, df1 = 2, df2 = 17, p-value = 0.5166
## alternative hypothesis: significant effects
# Si p-value es menor a 0.05 se prefiere el modelo de efectos fijos
#Modelo 3. Efectos Aleatorios
#Cuando las diferencias no observadas son aleatorias
#Método walhus
walhus <- plm(NY.GDP.PCAP.CD ~ SM.POP.NETM, data = panel_1, model="random", random.method = "walhus")
summary(walhus)
## Oneway (individual) effect Random Effect Model
## (Wallace-Hussain's transformation)
##
## Call:
## plm(formula = NY.GDP.PCAP.CD ~ SM.POP.NETM, data = panel_1, model = "random",
## random.method = "walhus")
##
## Balanced Panel: n = 3, T = 7, N = 21
##
## Effects:
## var std.dev share
## idiosyncratic 278418900 16686 1
## individual 0 0 0
## theta: 0
##
## Residuals:
## Min. 1st Qu. Median 3rd Qu. Max.
## -21506.0 -10924.8 -3728.9 5274.5 45389.3
##
## Coefficients:
## Estimate Std. Error z-value Pr(>|z|)
## (Intercept) 1.2873e+04 4.2134e+03 3.0553 0.002248 **
## SM.POP.NETM 1.8616e-02 7.2324e-03 2.5740 0.010054 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Total Sum of Squares: 7259500000
## Residual Sum of Squares: 5382600000
## R-Squared: 0.25855
## Adj. R-Squared: 0.21952
## Chisq: 6.62533 on 1 DF, p-value: 0.010054
#Método amemiya
amemiya <- plm(NY.GDP.PCAP.CD ~ SM.POP.NETM, data = panel_1, model="random", random.method = "amemiya")
summary(amemiya)
## Oneway (individual) effect Random Effect Model
## (Amemiya's transformation)
##
## Call:
## plm(formula = NY.GDP.PCAP.CD ~ SM.POP.NETM, data = panel_1, model = "random",
## random.method = "amemiya")
##
## Balanced Panel: n = 3, T = 7, N = 21
##
## Effects:
## var std.dev share
## idiosyncratic 276675480 16634 1
## individual 0 0 0
## theta: 0
##
## Residuals:
## Min. 1st Qu. Median 3rd Qu. Max.
## -21506.0 -10924.8 -3728.9 5274.5 45389.3
##
## Coefficients:
## Estimate Std. Error z-value Pr(>|z|)
## (Intercept) 1.2873e+04 4.2134e+03 3.0553 0.002248 **
## SM.POP.NETM 1.8616e-02 7.2324e-03 2.5740 0.010054 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Total Sum of Squares: 7259500000
## Residual Sum of Squares: 5382600000
## R-Squared: 0.25855
## Adj. R-Squared: 0.21952
## Chisq: 6.62533 on 1 DF, p-value: 0.010054
#Método nerlove
nerlove <- plm(NY.GDP.PCAP.CD ~ SM.POP.NETM, data = panel_1, model="random", random.method = "nerlove")
summary(nerlove)
## Oneway (individual) effect Random Effect Model
## (Nerlove's transformation)
##
## Call:
## plm(formula = NY.GDP.PCAP.CD ~ SM.POP.NETM, data = panel_1, model = "random",
## random.method = "nerlove")
##
## Balanced Panel: n = 3, T = 7, N = 21
##
## Effects:
## var std.dev share
## idiosyncratic 237150411 15400 0.864
## individual 37271843 6105 0.136
## theta: 0.31
##
## Residuals:
## Min. 1st Qu. Median 3rd Qu. Max.
## -20850.0 -9773.4 -2826.2 3450.7 45608.0
##
## Coefficients:
## Estimate Std. Error z-value Pr(>|z|)
## (Intercept) 1.3174e+04 5.8290e+03 2.2601 0.02382 *
## SM.POP.NETM 1.7563e-02 9.0595e-03 1.9386 0.05255 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Total Sum of Squares: 6.21e+09
## Residual Sum of Squares: 5184500000
## R-Squared: 0.16513
## Adj. R-Squared: 0.12119
## Chisq: 3.75814 on 1 DF, p-value: 0.052551
#Comparar la r2 ajustada de los 3 métodos y elegir el que tenga el mayor r2 ese lo ponemos en la prueba de hausman
phtest(walhus, within)
##
## Hausman Test
##
## data: NY.GDP.PCAP.CD ~ SM.POP.NETM
## chisq = 0.14364, df = 1, p-value = 0.7047
## alternative hypothesis: one model is inconsistent
#Si el p-value en <0.05, usamos Efectos Fijos
patentes <- read_excel("C:\\Users\\admin\\Downloads\\PATENT 3.xls")
summary(patentes)
## cusip merger employ return
## Min. : 800 Min. :0.0000 Min. : 0.085 Min. :-73.022
## 1st Qu.:368514 1st Qu.:0.0000 1st Qu.: 1.227 1st Qu.: 5.128
## Median :501116 Median :0.0000 Median : 3.842 Median : 7.585
## Mean :514536 Mean :0.0177 Mean : 18.826 Mean : 8.003
## 3rd Qu.:754688 3rd Qu.:0.0000 3rd Qu.: 15.442 3rd Qu.: 10.501
## Max. :878555 Max. :1.0000 Max. :506.531 Max. : 48.675
## NA's :21 NA's :8
## patents patentsg stckpr rnd
## Min. : 0.0 Min. : 0.00 Min. : 0.1875 Min. : 0.0000
## 1st Qu.: 1.0 1st Qu.: 1.00 1st Qu.: 7.6250 1st Qu.: 0.6847
## Median : 3.0 Median : 4.00 Median : 16.5000 Median : 2.1456
## Mean : 22.9 Mean : 27.14 Mean : 22.6270 Mean : 29.3398
## 3rd Qu.: 15.0 3rd Qu.: 19.00 3rd Qu.: 29.2500 3rd Qu.: 11.9168
## Max. :906.0 Max. :1063.00 Max. :402.0000 Max. :1719.3535
## NA's :2
## rndeflt rndstck sales sic
## Min. : 0.0000 Min. : 0.1253 Min. : 1.222 Min. :2000
## 1st Qu.: 0.4788 1st Qu.: 5.1520 1st Qu.: 52.995 1st Qu.:2890
## Median : 1.4764 Median : 13.3532 Median : 174.065 Median :3531
## Mean : 19.7238 Mean : 163.8234 Mean : 1219.601 Mean :3333
## 3rd Qu.: 8.7527 3rd Qu.: 74.5625 3rd Qu.: 728.964 3rd Qu.:3661
## Max. :1000.7876 Max. :9755.3516 Max. :44224.000 Max. :9997
## NA's :157 NA's :3
## year
## Min. :2012
## 1st Qu.:2014
## Median :2016
## Mean :2016
## 3rd Qu.:2019
## Max. :2021
##
str(patentes)
## tibble [2,260 × 13] (S3: tbl_df/tbl/data.frame)
## $ cusip : num [1:2260] 800 800 800 800 800 800 800 800 800 800 ...
## $ merger : num [1:2260] 0 0 0 0 0 0 0 0 0 0 ...
## $ employ : num [1:2260] 9.85 12.32 12.2 11.84 12.99 ...
## $ return : num [1:2260] 5.82 5.69 4.42 5.28 4.91 ...
## $ patents : num [1:2260] 22 34 31 32 40 60 57 77 38 5 ...
## $ patentsg: num [1:2260] 24 32 30 34 28 33 53 47 64 70 ...
## $ stckpr : num [1:2260] 47.6 57.9 33 38.5 35.1 ...
## $ rnd : num [1:2260] 2.56 3.1 3.27 3.24 3.78 ...
## $ rndeflt : num [1:2260] 2.56 2.91 2.8 2.52 2.78 ...
## $ rndstck : num [1:2260] 16.2 17.4 19.6 21.9 23.1 ...
## $ sales : num [1:2260] 344 436 535 567 631 ...
## $ sic : num [1:2260] 3740 3740 3740 3740 3740 3740 3740 3740 3740 3740 ...
## $ year : num [1:2260] 2012 2013 2014 2015 2016 ...
sum(is.na(patentes))#NA´s en la base de datos
## [1] 191
sapply(patentes, function(x) sum(is.na(x))) #NA´s por variable
## cusip merger employ return patents patentsg stckpr rnd
## 0 0 21 8 0 0 2 0
## rndeflt rndstck sales sic year
## 0 157 3 0 0
patentes$employ[is.na(patentes$employ)] <- mean(patentes$employ, na.rm=TRUE)
patentes$return[is.na(patentes$employ)] <- mean(patentes$employ, na.rm=TRUE)
patentes$stckpr[is.na(patentes$employ)] <- mean(patentes$employ, na.rm=TRUE)
patentes$rndstck[is.na(patentes$employ)] <- mean(patentes$employ, na.rm=TRUE)
patentes$sales[is.na(patentes$employ)] <- mean(patentes$employ, na.rm=TRUE)
summary(patentes)
## cusip merger employ return
## Min. : 800 Min. :0.0000 Min. : 0.085 Min. :-73.022
## 1st Qu.:368514 1st Qu.:0.0000 1st Qu.: 1.242 1st Qu.: 5.128
## Median :501116 Median :0.0000 Median : 3.893 Median : 7.585
## Mean :514536 Mean :0.0177 Mean : 18.826 Mean : 8.003
## 3rd Qu.:754688 3rd Qu.:0.0000 3rd Qu.: 16.034 3rd Qu.: 10.501
## Max. :878555 Max. :1.0000 Max. :506.531 Max. : 48.675
## NA's :8
## patents patentsg stckpr rnd
## Min. : 0.0 Min. : 0.00 Min. : 0.1875 Min. : 0.0000
## 1st Qu.: 1.0 1st Qu.: 1.00 1st Qu.: 7.6250 1st Qu.: 0.6847
## Median : 3.0 Median : 4.00 Median : 16.5000 Median : 2.1456
## Mean : 22.9 Mean : 27.14 Mean : 22.6270 Mean : 29.3398
## 3rd Qu.: 15.0 3rd Qu.: 19.00 3rd Qu.: 29.2500 3rd Qu.: 11.9168
## Max. :906.0 Max. :1063.00 Max. :402.0000 Max. :1719.3535
## NA's :2
## rndeflt rndstck sales sic
## Min. : 0.0000 Min. : 0.1253 Min. : 1.222 Min. :2000
## 1st Qu.: 0.4788 1st Qu.: 5.1520 1st Qu.: 52.995 1st Qu.:2890
## Median : 1.4764 Median : 13.3532 Median : 174.065 Median :3531
## Mean : 19.7238 Mean : 163.8234 Mean : 1219.601 Mean :3333
## 3rd Qu.: 8.7527 3rd Qu.: 74.5625 3rd Qu.: 728.964 3rd Qu.:3661
## Max. :1000.7876 Max. :9755.3516 Max. :44224.000 Max. :9997
## NA's :157 NA's :3
## year
## Min. :2012
## 1st Qu.:2014
## Median :2016
## Mean :2016
## 3rd Qu.:2019
## Max. :2021
##
sum(is.na(patentes))
## [1] 170
boxplot(patentes$cusip, horizontal=TRUE)
boxplot(patentes$merger, horizontal=TRUE)
boxplot(patentes$employ, horizontal=TRUE)
boxplot(patentes$patents, horizontal=TRUE)
boxplot(patentes$return, horizontal=TRUE)
boxplot(patentes$patentsg, horizontal=TRUE)
boxplot(patentes$stckpr, horizontal=TRUE)
boxplot(patentes$rnd, horizontal=TRUE)
boxplot(patentes$rndeflt, horizontal=TRUE)
boxplot(patentes$rndstck, horizontal=TRUE)
boxplot(patentes$sales, horizontal=TRUE)
boxplot(patentes$sic, horizontal=TRUE)
boxplot(patentes$year, horizontal=TRUE)
patentes$year <- patentes$year -40
summary(patentes)
## cusip merger employ return
## Min. : 800 Min. :0.0000 Min. : 0.085 Min. :-73.022
## 1st Qu.:368514 1st Qu.:0.0000 1st Qu.: 1.242 1st Qu.: 5.128
## Median :501116 Median :0.0000 Median : 3.893 Median : 7.585
## Mean :514536 Mean :0.0177 Mean : 18.826 Mean : 8.003
## 3rd Qu.:754688 3rd Qu.:0.0000 3rd Qu.: 16.034 3rd Qu.: 10.501
## Max. :878555 Max. :1.0000 Max. :506.531 Max. : 48.675
## NA's :8
## patents patentsg stckpr rnd
## Min. : 0.0 Min. : 0.00 Min. : 0.1875 Min. : 0.0000
## 1st Qu.: 1.0 1st Qu.: 1.00 1st Qu.: 7.6250 1st Qu.: 0.6847
## Median : 3.0 Median : 4.00 Median : 16.5000 Median : 2.1456
## Mean : 22.9 Mean : 27.14 Mean : 22.6270 Mean : 29.3398
## 3rd Qu.: 15.0 3rd Qu.: 19.00 3rd Qu.: 29.2500 3rd Qu.: 11.9168
## Max. :906.0 Max. :1063.00 Max. :402.0000 Max. :1719.3535
## NA's :2
## rndeflt rndstck sales sic
## Min. : 0.0000 Min. : 0.1253 Min. : 1.222 Min. :2000
## 1st Qu.: 0.4788 1st Qu.: 5.1520 1st Qu.: 52.995 1st Qu.:2890
## Median : 1.4764 Median : 13.3532 Median : 174.065 Median :3531
## Mean : 19.7238 Mean : 163.8234 Mean : 1219.601 Mean :3333
## 3rd Qu.: 8.7527 3rd Qu.: 74.5625 3rd Qu.: 728.964 3rd Qu.:3661
## Max. :1000.7876 Max. :9755.3516 Max. :44224.000 Max. :9997
## NA's :157 NA's :3
## year
## Min. :1972
## 1st Qu.:1974
## Median :1976
## Mean :1976
## 3rd Qu.:1979
## Max. :1981
##
#generar conjunto de datos de panel
panel_patentes <- pdata.frame(patentes, index = c("cusip","year"))
plotmeans(patents ~ cusip, main= "Prueba de heterogenidad entre empresas para sus patentes", data=panel_patentes)
#Como la linea sale quebrada, sube y baja, hay mucha heterogenidad, por lo que hay que ajustar
#Modelo 1. Regresión agrupada (pooled) ##para poca o nula heterogenidad
#asume que no hay heterogenidad no observada
pooled_patentes <- plm(patents ~ merger + employ + return + stckpr + rnd + sales + sic, data = panel_patentes, model="pooling")
summary(pooled_patentes)
## Pooling Model
##
## Call:
## plm(formula = patents ~ merger + employ + return + stckpr + rnd +
## sales + sic, data = panel_patentes, model = "pooling")
##
## Unbalanced Panel: n = 226, T = 6-10, N = 2252
##
## Residuals:
## Min. 1st Qu. Median 3rd Qu. Max.
## -320.73823 -10.11533 0.96527 7.41046 433.49935
##
## Coefficients:
## Estimate Std. Error t-value Pr(>|t|)
## (Intercept) -4.5823e-01 5.2868e+00 -0.0867 0.93094
## merger -1.1674e+01 7.2521e+00 -1.6097 0.10760
## employ 1.3694e+00 4.2028e-02 32.5828 < 2e-16 ***
## return -5.5049e-03 1.8177e-01 -0.0303 0.97584
## stckpr 6.5251e-01 4.3209e-02 15.1012 < 2e-16 ***
## rnd -1.3928e-01 1.6132e-02 -8.6336 < 2e-16 ***
## sales -3.1901e-03 4.7031e-04 -6.7830 1.5e-11 ***
## sic -2.6784e-03 1.4851e-03 -1.8035 0.07144 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Total Sum of Squares: 10994000
## Residual Sum of Squares: 4594700
## R-Squared: 0.58209
## Adj. R-Squared: 0.58079
## F-statistic: 446.512 on 7 and 2244 DF, p-value: < 2.22e-16
#Modelo 2. Efectos fijos (within)
#Cuando las diferencias no observadas son constante en el tiempo
within_patentes <- plm(patents ~ merger + employ + return + stckpr + rnd + sales + sic, data = panel_patentes, model="within")
summary(within_patentes)
## Oneway (individual) effect Within Model
##
## Call:
## plm(formula = patents ~ merger + employ + return + stckpr + rnd +
## sales + sic, data = panel_patentes, model = "within")
##
## Unbalanced Panel: n = 226, T = 6-10, N = 2252
##
## Residuals:
## Min. 1st Qu. Median 3rd Qu. Max.
## -497.16507 -1.62011 -0.19851 1.64094 184.59512
##
## Coefficients:
## Estimate Std. Error t-value Pr(>|t|)
## merger 3.35351504 4.17165385 0.8039 0.42156
## employ 0.11699838 0.07080752 1.6523 0.09862 .
## return -0.07161127 0.11004982 -0.6507 0.51530
## stckpr -0.01097329 0.03251309 -0.3375 0.73577
## rnd -0.19865376 0.01446336 -13.7350 < 2.2e-16 ***
## sales -0.00310168 0.00041645 -7.4479 1.397e-13 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Total Sum of Squares: 1091400
## Residual Sum of Squares: 819090
## R-Squared: 0.24952
## Adj. R-Squared: 0.1637
## F-statistic: 111.938 on 6 and 2020 DF, p-value: < 2.22e-16
#Prueba F
pFtest(within_patentes, pooled_patentes)
##
## F test for individual effects
##
## data: patents ~ merger + employ + return + stckpr + rnd + sales + sic
## F = 41.568, df1 = 224, df2 = 2020, p-value < 2.2e-16
## alternative hypothesis: significant effects
# como p-value es menor a 0.05 se avanza a los siguientes modelos
#Modelo 3. Efectos Aleatorios
#Cuando las diferencias no observadas son aleatorias
#Método walhus
walhus_patentes <- plm(patents ~ merger + employ + return + stckpr + rnd + sales + sic, data = panel_patentes, model="random", random.method = "walhus")
summary(walhus_patentes)
## Oneway (individual) effect Random Effect Model
## (Wallace-Hussain's transformation)
##
## Call:
## plm(formula = patents ~ merger + employ + return + stckpr + rnd +
## sales + sic, data = panel_patentes, model = "random", random.method = "walhus")
##
## Unbalanced Panel: n = 226, T = 6-10, N = 2252
##
## Effects:
## var std.dev share
## idiosyncratic 549.82 23.45 0.264
## individual 1531.45 39.13 0.736
## theta:
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.7624 0.8138 0.8138 0.8135 0.8138 0.8138
##
## Residuals:
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## -434.3797 -3.8922 -1.7663 0.0049 0.7622 211.3411
##
## Coefficients:
## Estimate Std. Error z-value Pr(>|z|)
## (Intercept) 11.81991653 13.02952376 0.9072 0.3643
## merger 4.44477469 4.51229785 0.9850 0.3246
## employ 1.09431950 0.04897343 22.3452 < 2.2e-16 ***
## return -0.13029160 0.11861251 -1.0985 0.2720
## stckpr 0.16740658 0.03358392 4.9847 6.205e-07 ***
## rnd -0.14658043 0.01471279 -9.9628 < 2.2e-16 ***
## sales -0.00392718 0.00042924 -9.1492 < 2.2e-16 ***
## sic -0.00097605 0.00383427 -0.2546 0.7991
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Total Sum of Squares: 1434600
## Residual Sum of Squares: 1090600
## R-Squared: 0.23978
## Adj. R-Squared: 0.2374
## Chisq: 708.033 on 7 DF, p-value: < 2.22e-16
#Método amemiya
amemiya_patentes <- plm(patents ~ merger + employ + return + stckpr + rnd + sales,
data = panel_patentes, model="random", random.method = "amemiya")
summary(amemiya_patentes)
## Oneway (individual) effect Random Effect Model
## (Amemiya's transformation)
##
## Call:
## plm(formula = patents ~ merger + employ + return + stckpr + rnd +
## sales, data = panel_patentes, model = "random", random.method = "amemiya")
##
## Unbalanced Panel: n = 226, T = 6-10, N = 2252
##
## Effects:
## var std.dev share
## idiosyncratic 405.49 20.14 0.051
## individual 7547.88 86.88 0.949
## theta:
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.9058 0.9269 0.9269 0.9268 0.9269 0.9269
##
## Residuals:
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## -454.5441 -2.9615 -1.6605 0.0033 0.6178 193.2184
##
## Coefficients:
## Estimate Std. Error z-value Pr(>|z|)
## (Intercept) 22.75387365 5.92762406 3.8386 0.0001237 ***
## merger 3.92493580 4.12056765 0.9525 0.3408318
## employ 0.49109781 0.06164783 7.9662 1.637e-15 ***
## return -0.09571453 0.10863461 -0.8811 0.3782808
## stckpr 0.04664956 0.03171241 1.4710 0.1412860
## rnd -0.17973247 0.01409500 -12.7515 < 2.2e-16 ***
## sales -0.00343629 0.00040743 -8.4341 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Total Sum of Squares: 1144300
## Residual Sum of Squares: 891640
## R-Squared: 0.22083
## Adj. R-Squared: 0.21875
## Chisq: 636.323 on 6 DF, p-value: < 2.22e-16
#Método nerlove
nerlove_patentes <- plm(patents ~ merger + employ + return + stckpr + rnd + sales + sic, data = panel_patentes, model="random", random.method = "nerlove")
summary(nerlove_patentes)
## Oneway (individual) effect Random Effect Model
## (Nerlove's transformation)
##
## Call:
## plm(formula = patents ~ merger + employ + return + stckpr + rnd +
## sales + sic, data = panel_patentes, model = "random", random.method = "nerlove")
##
## Unbalanced Panel: n = 226, T = 6-10, N = 2252
##
## Effects:
## var std.dev share
## idiosyncratic 363.72 19.07 0.046
## individual 7602.26 87.19 0.954
## theta:
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.9111 0.9310 0.9310 0.9309 0.9310 0.9310
##
## Residuals:
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## -455.9302 -2.9399 -1.6084 0.0032 0.6178 192.3469
##
## Coefficients:
## Estimate Std. Error z-value Pr(>|z|)
## (Intercept) 8.37943183 31.51076930 0.2659 0.7903
## merger 3.88283943 4.10699605 0.9454 0.3444
## employ 0.45875996 0.06225393 7.3692 1.717e-13 ***
## return -0.09373310 0.10828377 -0.8656 0.3867
## stckpr 0.04168538 0.03164395 1.3173 0.1877
## rnd -0.18140485 0.01406829 -12.8946 < 2.2e-16 ***
## sales -0.00340492 0.00040657 -8.3747 < 2.2e-16 ***
## sic 0.00452769 0.00928394 0.4877 0.6258
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Total Sum of Squares: 1138600
## Residual Sum of Squares: 885010
## R-Squared: 0.22271
## Adj. R-Squared: 0.22028
## Chisq: 642.996 on 7 DF, p-value: < 2.22e-16
#Comparar la r2 ajustada de los 3 métodos y elegir el que tenga el mayor r2 ese lo ponemos en la prueba de hausman
phtest(walhus_patentes, within_patentes)
##
## Hausman Test
##
## data: patents ~ merger + employ + return + stckpr + rnd + sales + sic
## chisq = 347.29, df = 6, p-value < 2.2e-16
## alternative hypothesis: one model is inconsistent
#Si el p-value en <0.05, usamos Efectos Fijos
#Por lo tat nos quedamos con el modelo de efectos fijos (within)
#Prieba de heterocedasticidad
bptest(within_patentes)
##
## studentized Breusch-Pagan test
##
## data: within_patentes
## BP = 1444.3, df = 7, p-value < 2.2e-16
#Si el p-value < 0.05, hay heterocedasticidad en los residuos (problema detectado)
#prueba de autocorrelación serial
pwartest(within_patentes)
##
## Wooldridge's test for serial correlation in FE panels
##
## data: within_patentes
## F = 104.18, df1 = 1, df2 = 2024, p-value < 2.2e-16
## alternative hypothesis: serial correlation
#Si el p-value < 0.05, hay autocorrelación serial en los errores (problema detectado)
#Modelo de Corrección de Errores Estandar Robustos
coeficientes_corregidos <- coeftest(within_patentes, vcov= vcovHC(within_patentes, type="HC0"))
solo_coeficientes <- coeficientes_corregidos[,1]
datos_de_prueba <- data.frame( merger=0, employ=10, return=6, stckpr=48, rnd=3, sales=344)
prediccion <- sum(solo_coeficientes*datos_de_prueba)
prediccion
## [1] -1.449341
En conclusion este ejercicio nos permitio generar pronosticos en bases de datos con panel, tomando en cuenta los tratamientos para distintos efectos en los datosy sus errores