使用R中的plm fom model.matrix估计固定效应模型

时间:2019-03-11 15:20:35

标签: r panel-data plm

我有以下数据集:

df <- structure(list(b034 = c(372014, 33392282, 963697, 132599, 20096344, 
                              16763954, 111482, 114488, 12865350, 202375, 6488677, 271387, 
                              359313, 104523, 128172, 143762, 495689, 56584, 604622, 783067
), b094 = c(1980549, 10900551, 753705, 12752, 6644908, 13870084, 
            12117, 12388, 7459061, 150948, 4990358, 357850, 726953, 525585, 
            311363, 436576, 731903, 511628, 1628231, 1963048), b113 = c(141744, 
                                                                        43833152, 1819238, 58946, 44092348, 41732042, 61339, 188222, 
                                                                        30245811, 276182, 14643226, 401380, 1219790, 567831, 211055, 
                                                                        182373, 434946, 215029, 1424440, 2771243), b119 = c(17575, 9075581, 
                                                                                                                            772848, 0, 9217552, 6662922, 0, 0, 1928984, 0, 332170, 0, 153062, 
                                                                                                                            154130, 0, 0, 38676, 0, 259485, 430704), b123 = c(41091, 2237667, 
                                                                                                                                                                              43434, 7347, 511429, 3129586, 12547, 21112, 754672, 93001, 1598352, 
                                                                                                                                                                              123397, 104485, 78760, 124238, 125754, 15748, 164960, 340055, 
                                                                                                                                                                              138427), b136 = c(1552, 1386816, 7328, 295, 687958, 612780, 399, 
                                                                                                                                                                                                817, 337370, 614, 120176, 588, 57510, 31628, 520, 319, 0, 183, 
                                                                                                                                                                                                33515, 203), b147 = c(48951, 1715659, 58832, 13877, 1692717, 
                                                                                                                                                                                                                      1410295, -11509, 37019, 1040208, 56377, 713912, 11328, 48846, 
                                                                                                                                                                                                                      38869, -14615, 41025, 4169, -48078, 272145, 160588), b369 = c(1699865, 
                                                                                                                                                                                                                                                                                    40886032, 138723, 0, 39128816, 0, 3773, 0, 0, 0, 0, 37997, 0, 
                                                                                                                                                                                                                                                                                    0, 0, 4548, 0, 0, 44070, 29267), reportyear = c(2017L, 2017L, 
                                                                                                                                                                                                                                                                                                                                    2017L, 2016L, 2016L, 2015L, 2015L, 2014L, 2014L, 2013L, 2013L, 
                                                                                                                                                                                                                                                                                                                                    2012L, 2012L, 2011L, 2011L, 2010L, 2010L, 2009L, 2003L, 2002L
                                                                                                                                                                                                                                                                                    ), subjectid = c("04670647", "02649624", "00224871", "00224871", 
                                                                                                                                                                                                                                                                                                     "02649624", "02649624", "00224871", "00224871", "02649624", "00224871", 
                                                                                                                                                                                                                                                                                                     "02649624", "00224871", "02649624", "02649624", "00224871", "00224871", 
                                                                                                                                                                                                                                                                                                     "02649624", "00224871", "00224871", "00224871")), row.names = c(1L, 
                                                                                                                                                                                                                                                                                                                                                                     12L, 18L, 164996L, 229101L, 306280L, 333053L, 386608L, 415725L, 
                                                                                                                                                                                                                                                                                                                                                                     515731L, 533006L, 603723L, 650160L, 694574L, 729140L, 773246L, 
                                                                                                                                                                                                                                                                                                                                                                     780649L, 908163L, 1420682L, 1480284L), class = "data.frame")

数据被组织为面板数据。我想估算固定效果,但是由于某些原因,我想从model.matrix对象估算它。首先,我制作pdataframe对象:

# convert data to panel data
library(plm)
panelData <- pdata.frame(df, index = c("subjectid", "reportyear"), drop.index = FALSE)

然后我创建模型矩阵:

# make matrix from formula
# form <- input$formula
formulaInput <- "b369 ~ b113 + I(b147+b123) + I(b147+b123+b119+b136) + I(b034/b094)"
pform  <- pFormula(as.formula(formulaInput))
mf <- model.frame(pform, data = panelData)
modmat <- model.matrix(pform, data = mf, model = "within")
modmat

如何使用modmat对象估算固定效果?我知道如何直接通过plm函数执行此操作,但是我想使用modmat对象。我的尝试:

fe_model <- plm(modmat[, 1] ~ modmat[, -1])

返回错误:

Error in row.names(data) : argument "data" is missing, with no default

1 个答案:

答案 0 :(得分:1)

plm函数需要一个data.frame或一个pdata.frame。一旦有了模型矩阵,数据就已经转换了(固定效果被清除了)。因此,您可以运行lm()而不是plm()

fe_model <- lm(modmat[, 1] ~ modmat[, -1])