簡體   English   中英

在 x 和 y 變量列表上循環面板數據回歸

[英]Loop paneldata regression over list of x and y variables

我正在使用 paneldata 並希望從變量列表中進行一系列雙變量固定效應回歸。

我的數據集的一小部分如下所示:

library(plm)
library(dplyr)

structure(list(v2x_libdem = c(0.876, 0.88, 0.763, 0.779), v2x_partipdem = c(0.679, 
0.68, 0.647, 0.652), v2x_frassoc_thick = c(0.937, 0.937, 0.9, 
0.899), country_name = c("Sweden", "Sweden", "Hungary", "Hungary"
), year = c(2000, 2008, 2000, 2008)), row.names = c(NA, -4L), class = c("tbl_df", 
"tbl", "data.frame"))

# A tibble: 4 × 5
  v2x_libdem v2x_partipdem v2x_frassoc_thick country_name  year
       <dbl>         <dbl>             <dbl> <chr>        <dbl>
1      0.876         0.679             0.937 Sweden        2000
2      0.88          0.68              0.937 Sweden        2008
3      0.763         0.647             0.9   Hungary       2000
4      0.779         0.652             0.899 Hungary       2008

我的變量列表看起來像這樣:

vars <- c("v2x_libdem", "v2x_partipdem", "v2x_frassoc_thick")

這些變量應該同時是 x 和 y,因此作為 x 和 y 的變量的每個組合都應該在回歸中。

我寫了一個 function 進行面板數據建模:

paneldata_function <- function (y, x) {     #this function will do a PLM 
  plm(paste(y, "~", x), 
      data = Vdem_west,
      model = "within",
      index = c("year", "country_name"))   #for a given x & y variable
}

這是 function 我需要循環遍歷 x 和 y 的每個組合。 最好,我希望 output 是四個值向量,我可以將其轉換為數據集; 一種用於系數,一種用於 x 變量,一種用於 y 變量,一種用於 p 值。

如果我做一個單一的 model,我可以很容易地訪問這些:

e <- paneldata_function("v2x_libdem", "v2x_partipdem")

p_value <- e[["fstatistic"]][["p.value"]]

x_var <- names(e[["model"]])[2]

y_var <- names(e[["model"]])[1]

我如何編寫循環才能將這些向量作為 output?

非常感謝任何幫助,我希望我盡可能清楚地表達我的問題。

要遍歷可能的字段,首先,您需要一些可以為您提供該信息的東西。 無論您的數據集大小如何,此 function 都可以使用,但您必須調整要保留的字段。 (比如這里需要排除最后兩個字段,所以我 select 1:3 做這個列表的方法有很多,但是很多都是做重復的(或者兩個字段一樣,但是一次是左右下一個是右-左)。此方法使用2 RcppAlgos您想要多少個變量。F 或FALSE僅呈現返回唯一對。

ndf = names(Vdem_west)   
cbo = RcppAlgos::comboGeneral(ndf[1:3], 2, F) # make a list of possible combinations
#      [,1]            [,2]               
# [1,] "v2x_libdem"    "v2x_partipdem"    
# [2,] "v2x_libdem"    "v2x_frassoc_thick"
# [3,] "v2x_partipdem" "v2x_frassoc_thick" 

然后使用您現在的 function,我在這個map電話中調用它。 maptidyverse中。

# run all the models; store results in list of lists
res = map(1:nrow(cbo),
          ~paneldata_function(cbo[.x, 1], cbo[.x, 2]))

# name the models
names(res) <- paste0(cbo[, 1], "_", cbo[, 2])
names(res) # check it
# [1] "v2x_libdem_v2x_partipdem"        "v2x_libdem_v2x_frassoc_thick"   
# [3] "v2x_partipdem_v2x_frassoc_thick" 

對於系數數據,我將 output 寫入數據框。 你沒有要求 model 名稱,但我也添加了它。

map_dfr(1:length(res),
        .f = function(x){
          estP = summary(res[[x]])$coefficients[c(1, 4)]
          coef = dimnames(summary(res[[x]])$coefficients)[1]
          model = names(res)[x]
          c(Coefficient = coef, Est = round(estP[1], 4), 
            PV = round(estP[2], 4), Model = model)
        })
# # A tibble: 3 × 4
#   Coefficient         Est     PV Model                          
#   <chr>             <dbl>  <dbl> <chr>                          
# 1 v2x_partipdem     3.56  0.0067 v2x_libdem_v2x_partipdem       
# 2 v2x_frassoc_thick 2.85  0.0441 v2x_libdem_v2x_frassoc_thick   
# 3 v2x_frassoc_thick 0.799 0.0509 v2x_partipdem_v2x_frassoc_thick


我還整理了一個電話,以便您也可以查看 model 的性能。 這可以與之前的調用相結合,但您沒有要求這部分。 (實際上我先做了這部分,然后意識到我沒有回答你的問題。)

map_dfr(1:length(res),
        .f = function(x){
          R2 = summary(res[[x]])[["r.squared"]][["rsq"]] %>% round(4)
          Adj.R2 = summary(res[[x]])[["r.squared"]][["adjrsq"]] %>% round(4)
          PV = summary(res[[x]])[["fstatistic"]][["p.value"]] %>% round(4)
          Model = names(res)[x]
          c(R2 = R2, Adj.R2 = Adj.R2, PV = PV, Model = Model)
        })
# # A tibble: 3 × 4
#   R2     Adj.R2 PV.F   Model                          
#   <chr>  <chr>  <chr>  <chr>                          
# 1 0.9999 0.9997 0.0067 v2x_libdem_v2x_partipdem       
# 2 0.9952 0.9856 0.0441 v2x_libdem_v2x_frassoc_thick   
# 3 0.9936 0.9809 0.0509 v2x_partipdem_v2x_frassoc_thick 

您可以使用相同的方法來測試您的 model 並查看假設。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM