簡體   English   中英

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

[英]Estimate fixed effects model using plm fom model.matrix in R

我有以下數據集:

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

plm函數需要一個data.framepdata.frame 一旦有了模型矩陣,數據就已經轉換了(固定效果被清除了)。 因此,您可以運行lm()而不是plm()

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

暫無
暫無

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

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