Search code examples
rloopsplm

Loop in R to print the plm regression results


I have the following for loop to run the same regression for 8 datasets:

for (i in 1:length(PData_EE)) {
  FEwithin_EE[[i]] <- plm(PData_EE[[i]]$domegaACF ~ dplyr::lag(PData_EE[[i]]$domegaACF, 1) + dplyr::lag(PData_EE[[i]]$domegaACF, 2) 
                          + PData_EE[[i]]$ddebt + lag(PData_EE[[i]]$ff2) + PData_EE[[i]]$ddebt:lag(PData_EE[[i]]$ff2) 
                          + log(PData_EE[[i]]$Age) + PData_EE[[i]]$ta + PData_EE[[i]]$dsales + PData_EE[[i]]$INVr, 
                          effect = "twoways", model ="within", index = c("ID", "Year"), data = PData_EE[[i]])
}

The results of the plm regressions are stored in the FEwithin_EE list.

Now< I am trying to print the results of each of the regressions by using

for (i in 1:length(FEwithin_EE)) {
  n <- names(EE_list)
  cat("Results for dataset ", n[i],"\n", summary(FEwithin_EE[[i]]),"\n")
}

so to get something similar to:

Results for dataset EE_AB 
Twoways effects Within Model

Call:
plm(formula = PData_EE[[i]]$domegaACF ~ dplyr::lag(PData_EE[[i]]$domegaACF, 
    1) + dplyr::lag(PData_EE[[i]]$domegaACF, 2) + PData_EE[[i]]$ddebt + 
    lag(PData_EE[[i]]$ff2) + PData_EE[[i]]$ddebt:lag(PData_EE[[i]]$ff2) + 
    log(PData_EE[[i]]$Age) + PData_EE[[i]]$ta + PData_EE[[i]]$dsales + 
    PData_EE[[i]]$INVr, data = PData_EE[[i]], effect = "twoways", 
    model = "within", index = c("ID", "Year"))

Unbalanced Panel: n = 1444, T = 1-8, N = 6871

Residuals:
    Min.  1st Qu.   Median  3rd Qu.     Max. 
-1.58936 -0.18466  0.00000  0.17945  1.82167 

Coefficients:
                                             Estimate Std. Error  t-value  Pr(>|t|)    
dplyr::lag(PData_EE[[i]]$domegaACF, 1)     -0.3480423  0.0124119 -28.0410 < 2.2e-16 ***
dplyr::lag(PData_EE[[i]]$domegaACF, 2)     -0.1775470  0.0122021 -14.5505 < 2.2e-16 ***
PData_EE[[i]]$ddebt                        -0.0481358  0.0120689  -3.9884  6.74e-05 ***
lag(PData_EE[[i]]$ff2)                     -0.0016440  0.0044048  -0.3732  0.708984    
PData_EE[[i]]$ta                           -0.0046349  0.0171719  -0.2699  0.787238    
PData_EE[[i]]$dsales                        0.4013406  0.0122319  32.8111 < 2.2e-16 ***
PData_EE[[i]]$INVr                         -0.0317337  0.0116622  -2.7211  0.006528 ** 
PData_EE[[i]]$ddebt:lag(PData_EE[[i]]$ff2)  0.0100436  0.0044968   2.2335  0.025557 *  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Total Sum of Squares:    1284.6
Residual Sum of Squares: 895.64
R-Squared:      0.30279
Adj. R-Squared: 0.11496
F-statistic: 293.8 on 8 and 5412 DF, p-value: < 2.22e-16


Results for dataset EE_C
Twoways effects Within Model

Call:
plm(formula = PData_EE[[i]]$domegaACF ~ dplyr::lag(PData_EE[[i]]$domegaACF, 
    1) + dplyr::lag(PData_EE[[i]]$domegaACF, 2) + PData_EE[[i]]$ddebt + 
    lag(PData_EE[[i]]$ff2) + PData_EE[[i]]$ddebt:lag(PData_EE[[i]]$ff2) + 
    log(PData_EE[[i]]$Age) + PData_EE[[i]]$ta + PData_EE[[i]]$dsales + 
    PData_EE[[i]]$INVr, data = PData_EE[[i]], effect = "twoways", 
    model = "within", index = c("ID", "Year"))

Unbalanced Panel: n = 2621, T = 1-8, N = 12617

Residuals:
     Min.   1st Qu.    Median   3rd Qu.      Max. 
-0.979984 -0.047097  0.000000  0.048005  0.855447 

Coefficients:
                                             Estimate Std. Error  t-value  Pr(>|t|)    
dplyr::lag(PData_EE[[i]]$domegaACF, 1)     -0.1635905  0.0057632 -28.3856 < 2.2e-16 ***
dplyr::lag(PData_EE[[i]]$domegaACF, 2)     -0.0756078  0.0055546 -13.6118 < 2.2e-16 ***
PData_EE[[i]]$ddebt                        -0.0201306  0.0027124  -7.4216 1.251e-13 ***
lag(PData_EE[[i]]$ff2)                     -0.0043745  0.0013689  -3.1956   0.00140 ** 
PData_EE[[i]]$ta                            0.0084570  0.0043092   1.9625   0.04973 *  
PData_EE[[i]]$dsales                        0.7218502  0.0049261 146.5358 < 2.2e-16 ***
PData_EE[[i]]$INVr                         -0.0509146  0.0023479 -21.6855 < 2.2e-16 ***
PData_EE[[i]]$ddebt:lag(PData_EE[[i]]$ff2)  0.0017058  0.0013150   1.2971   0.19461    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Total Sum of Squares:    677.73
Residual Sum of Squares: 182.12
R-Squared:      0.73128
Adj. R-Squared: 0.66034
F-statistic: 3395.26 on 8 and 9981 DF, p-value: < 2.22e-16

However, I am getting an error:

 Error in cat("Results for dataset ", n[i], "\n", summary(FEwithin_EE[[1]]),  : 
  argument 4 (type 'list') cannot be handled by 'cat'

Would really much appreciate your responses!

With two lines as Helix suggested I receive this error:

Results for dataset  EE_AB 
Twoways effects Within Model

Call:
plm(formula = PData_EE[[i]]$domegaACF ~ dplyr::lag(PData_EE[[i]]$domegaACF, 
    1) + dplyr::lag(PData_EE[[i]]$domegaACF, 2) + PData_EE[[i]]$ddebt + 
    lag(PData_EE[[i]]$ff2) + PData_EE[[i]]$ddebt:lag(PData_EE[[i]]$ff2) + 
    log(PData_EE[[i]]$Age) + PData_EE[[i]]$ta + PData_EE[[i]]$dsales + 
    PData_EE[[i]]$INVr, data = PData_EE[[i]], effect = "twoways", 
    model = "within", index = c("ID", "Year"))

Unbalanced Panel: n = 1444, T = 1-8, N = 6871

Residuals:
Error in options(digits = digits) : 
  invalid 'digits' parameter, allowed 0...22

Solution

  • The problem is pretty clear based on the error you reported. The error is occurring because you are trying to pass a list through cat. If you look at the cat documentation it only works with atomic vectors. Try unlist(summary(FEwithin_EE[[i]]) in your cat argument and you should solve that problem.