简体   繁体   English

包括 plm 中固定效应 model 中的非线性

[英]including non linearity in fixed effects model in plm

I am trying to build a fixed effects regression with the plm package in R.我正在尝试使用 R 中的 plm package 构建固定效应回归。 I am using country level panel data with year and country fixed effects.我正在使用具有年份和国家固定效应的国家级面板数据。 My problem concerns 2 explanatory variables.我的问题涉及 2 个解释变量。 One is an interaction term of two varibels and one is a squared term of one of the variables.一个是两个变量的交互项,一个是其中一个变量的平方项。

model is basically: y = x1 + x1^2+ x3 + x1*x3+...+xn, with the variables all being in log form model 基本上是:y = x1 + x1^2+ x3 + x1*x3+...+xn,所有变量都是对数形式

It is central to the model to include the squared term, but when I run the regression it always gets excluded because of "singularities", as x1 and x1^2 are obviously correlated. model 的核心是包含平方项,但是当我运行回归时,由于“奇点”,它总是被排除在外,因为 x1 和 x1^2 显然是相关的。 Meaning the regression works and I get estimates for my variables, just not for x1^2 and x1*x2.意味着回归有效,我得到了我的变量的估计值,而不是 x1^2 和 x1*x2。 How do I circumvent this?我该如何规避这个?

library(plm)
fe_reg<- plm(log(y) ~ log(x1)+log(x2)+log(x2^2)+log(x1*x2)+dummy,
                    data = df,
                    index = c("country", "year"), 
                    model = "within",
             effect = "twoways")
summary(fe_reg)  
  ´´´

#I have tried defining the interaction and squared terms as vectors, which helped with the #interaction term but not the squared term. 

df1.pd<- df1 %>% mutate_at(c('x1'), ~(scale(.) %>% as.vector))
df1.pd<- df1 %>% mutate_at(c('x2'), ~(scale(.) %>% as.vector))
 ´´´
I am pretty new to R, so apologies if this not a very well structured question.

You just found a property of the logarithm function:您刚刚找到了对数 function 的一个属性:

log(x^2) = 2 * log(x)日志(x^2) = 2 * 日志(x)

Then, obviously, log(x) is collinear with 2*log(x) and one of the two collinear variables is dropped from the estimation.然后,很明显,log(x) 与 2*log(x) 共线,并且从估计中删除了两个共线变量之一。

So, the model you want to estimate is not estimable by linear regression methods.因此,您要估计的 model 无法通过线性回归方法进行估计。 You might want to take different data transformations than log into account or the original variables.您可能希望采用与登录或原始变量不同的数据转换。

See also the reproducible example below.另请参阅下面的可重现示例。 Linear dependence can be detected, eg, via function detect.lindep from package plm (see also below).例如,可以通过来自 package plm的 function detect.lindep检测线性相关性(也参见下文)。 Dropping of coefficients from estimation also hints at collinear columns in the model estimation matrix.从估计中删除系数也暗示 model 估计矩阵中的共线列。 At times, linear dependence appears only after data transformations invovled in the estimation functions, see for an example of the within transformation the help page ?detect.lindep in the Example section).有时,仅在估计函数中涉及的数据转换之后才会出现线性相关性,请参阅示例部分中的帮助页面?detect.lindep中的内部转换示例)。

library(plm)
data("Grunfeld")
pGrun <- pdata.frame(Grunfeld)
pGrun$lvalue  <- log(pGrun$value)   # log(x)
pGrun$lvalue2 <- log(pGrun$value^2) # log(x^2) == 2 * log(x)

mod  <- plm(inv ~ lvalue + lvalue2 + capital, data = pGrun, model = "within")
summary(mod)
#> Oneway (individual) effect Within Model
#> 
#> Call:
#> plm(formula = inv ~ lvalue + lvalue2 + capital, data = pGrun, 
#>     model = "within")
#> 
#> Balanced Panel: n = 10, T = 20, N = 200
#> 
#> Residuals:
#>       Min.    1st Qu.     Median    3rd Qu.       Max. 
#> -186.62916  -20.56311   -0.17669   20.66673  300.87714 
#> 
#> Coefficients: (1 dropped because of singularities)
#>          Estimate Std. Error t-value Pr(>|t|)    
#> lvalue  30.979345  17.592730  1.7609  0.07988 .  
#> capital  0.360764   0.020078 17.9678  < 2e-16 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Total Sum of Squares:    2244400
#> Residual Sum of Squares: 751290
#> R-Squared:      0.66525
#> Adj. R-Squared: 0.64567
#> F-statistic: 186.81 on 2 and 188 DF, p-value: < 2.22e-16

detect.lindep(mod) # run on the model 
#> [1] "Suspicious column number(s): 1, 2"
#> [1] "Suspicious column name(s):   lvalue, lvalue2"
detect.lindep(pGrun) # run on the data
#> [1] "Suspicious column number(s): 6, 7"
#> [1] "Suspicious column name(s):   lvalue, lvalue2"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM