为 R 中的数据面板创建一个 purtest 循环

Create a loop for purtest for data panel in R

for (i in 3:n )
{
  y<- data.frame(split(Panel_data2[,i], Panel_data2[,1])) 
  p<-purtest(y, data = Panel_data2, index = c("Country", "Year"), test = "levinlin", exo = "intercept",lags = "AIC", pmax = 5 )
    print(summary(p))
}

我的数据有列 country Year X1 X2 X3 X4 X5... 任何人都可以为数据中的每个 X 变量建议 R 中 purtest 函数的循环吗?我还需要为每个 i 将 pvalues 存储在数据框中。谁能告诉我如何存储 p.value ?

也许,问题需要更好地构建,提供更多信息和具体示例,包括使用的包等;以获得更广泛的响应。 通过一些假设和概括,下面的代码提供了一个解决方案,可以帮助为您的问题提供一个模式

> ################################## First a working example without a loop
> # install.packages("plm",dependencies = T)
> # library(plm)
> Panel_data2 <- data.frame(cbind("labels" = sort(rep(1:4,40)),"dollars"=rnorm(160,25,250), "Year" = sort(rep(2017:2020,40))))

> head(Panel_data2)
  labels   dollars Year
1      1  293.6016 2017
2      1  516.3135 2017
3      1  170.5544 2017
4      1  205.5305 2017
5      1  248.6401 2017
6      1 -188.5928 2017
> attach(Panel_data2)

> 
> y<- data.frame(split(Panel_data2$dollars, Panel_data2$labels)); head(y)
         X1        X2         X3         X4
1  293.6016   27.5932  -52.57139   78.55826
2  516.3135  355.9428  433.23976  -13.51502
3  170.5544 -262.3336  116.67886  108.29120
4  205.5305  148.8820  197.29201  144.51237
5  248.6401   91.8486 -286.50322 -440.58928
6 -188.5928 -177.1045  -58.59861 -204.75904
> p <-purtest(y, test = "levinlin", exo = "intercept", pmax = 5 )
> print(summary(p))
Levin-Lin-Chu Unit-Root Test 
Exogenous variables: Individual Intercepts 
Automatic selection of lags using SIC: 0 - 0 lags (max: 5)
statistic: -9.218 
p-value: 0 

   lags obs        rho      trho       p.trho sigma2ST sigma2LT
X1    0  39 -0.6857451 -4.497054 1.941968e-04 59414.60 17161.44
X2    0  39 -0.9768774 -6.092917 7.136768e-08 74860.31 13564.22
X3    0  39 -0.9343773 -5.742247 4.870200e-07 76580.26 17761.72
X4    0  39 -0.9961292 -6.028172 1.024591e-07 77668.78 18582.06
> ##############################  Next - the for loop in case you have $dollar1, $dollar2 .... variables in your data
> 
> Panel_data2 <- data.frame(cbind("labels" = sort(rep(1:4,40)),"dollars"=rnorm(160,25,250), "dollars1"=rnorm(160,35,250),"dollars2"=rnorm(160,45,250),"dollars3"=rnorm(160,55,250), "Year" = sort(rep(2017:2020,40))))
> head(Panel_data2)
  labels    dollars   dollars1   dollars2  dollars3 Year
1      1  183.33632 -109.76355  -58.30790  445.0653 2017
2      1  356.35553 -136.47802 -513.38200  494.9800 2017
3      1   78.89656  414.43767  310.95509   58.0294 2017
4      1 -141.81512   91.29213 -259.55993 -119.4564 2017
5      1  217.52874  -26.80482   28.13365 -189.5662 2017
6      1 -459.09774  443.19261  -38.53635 -353.5045 2017
> for (i in 2:5)
+ {
+   y<- data.frame(split(Panel_data2[,i], Panel_data2$labels)) 
+   p<-purtest(y,  test = "levinlin", exo = "intercept",lags = "AIC", pmax = 5 )
+   print(summary(p))
+ }
Levin-Lin-Chu Unit-Root Test 
Exogenous variables: Individual Intercepts 
Automatic selection of lags using AIC: 0 - 1 lags (max: 5)
statistic: -11.815 
p-value: 0 

   lags obs       rho      trho       p.trho sigma2ST sigma2LT
X1    0  39 -1.227784 -7.911474 9.068620e-13 55467.25 10902.43
X2    1  38 -1.041559 -4.881167 3.551036e-05 49193.85  9277.71
X3    1  38 -1.280364 -6.439673 9.770737e-09 68091.36 14546.49
X4    0  39 -1.242060 -7.692819 3.885717e-12 38042.81 14252.99
Levin-Lin-Chu Unit-Root Test 
Exogenous variables: Individual Intercepts 
Automatic selection of lags using AIC: 0 - 4 lags (max: 5)
statistic: -9.8 
p-value: 0 

   lags obs       rho      trho       p.trho sigma2ST  sigma2LT
X1    0  39 -1.081057 -6.800597 1.129773e-09 50890.60  9452.008
X2    4  35 -2.373316 -4.164677 7.539689e-04 51177.70 18085.713
X3    0  39 -1.007441 -7.178139 1.083001e-10 66201.79 33540.675
X4    0  39 -1.084036 -6.740021 1.632406e-09 79013.62 20016.329
Levin-Lin-Chu Unit-Root Test 
Exogenous variables: Individual Intercepts 
Automatic selection of lags using AIC: 0 - 0 lags (max: 5)
statistic: -11.383 
p-value: 0 

   lags obs       rho      trho       p.trho sigma2ST sigma2LT
X1    0  39 -1.097077 -6.871547 7.319843e-10 55618.32 10404.55
X2    0  39 -1.114177 -7.007343 3.161973e-10 56703.83 13260.77
X3    0  39 -1.039392 -6.014202 1.107282e-07 69945.04 18604.49
X4    0  39 -1.019252 -6.046148 9.270029e-08 61122.55 14540.71
Levin-Lin-Chu Unit-Root Test 
Exogenous variables: Individual Intercepts 
Automatic selection of lags using AIC: 0 - 1 lags (max: 5)
statistic: -9.446 
p-value: 0 

   lags obs        rho      trho       p.trho sigma2ST  sigma2LT
X1    0  39 -0.9517744 -6.056751 8.737652e-08 76139.52 20165.591
X2    0  39 -1.0317893 -6.410946 1.155769e-08 56768.27  9879.229
X3    0  39 -0.9278345 -5.800193 3.569186e-07 55170.62 12648.129
X4    1  38 -0.8104566 -4.445363 2.414433e-04 67342.52 21040.110
> #############################