问题
This is a follow-up question from
Automate regression with specific dependent and independent variables
This code works a treat with plm function from plm package. However, I wanted to estimate the same model with felm function from lfe package to obtain standard errors clustered by space (in my case county).
Let this be the data set:
regDf <- data.frame(year = rep(seq(1966,2015,1), 8),
county = c(rep('first', 50), rep('second', 50), rep('third', 50), rep('fourth', 50),
rep('fifth', 50), rep('sixth', 50), rep('seventh', 50), rep('eighth', 50)),
crime1 = runif(400), crime2 = runif(400), crime3 = runif(400),
uvar1 = runif(400), uvar2 = runif(400), uvar3 = runif(400),
var1 = runif(400), var2 = runif(400), var3 = runif(400), var4 = runif(400), var5 = runif(400))
This code works
set.seed(123)
#a helper function to create "log(var)" from "var"
fn_appendLog = function(x) {
paste0("log(",x,")")
}
modelList = lapply(1:3,function(x) {
indepVars2 = Reduce(function(x,y) paste(x,y,sep="+"),lapply(colnames(regDF)[grepl("^v",colnames(regDF))],fn_appendLog))
#> indepVars2
#[1] "log(var1)+log(var2)+log(var3)+log(var4)+log(var5)"
indepVars1 = fn_appendLog(paste0("uvar",x))
depVar = fn_appendLog(paste0("crime",x))
formulaVar = formula(paste0(depVar, " ~ ",indepVars1,"+", indepVars2))
#> formulaVar
#log(crime1) ~ log(uvar1) + log(var1) + log(var2) + log(var3) + log(var4) + log(var5)
modelObj = plm(formulaVar, model = 'within', effect = 'twoways', data = regDf)
})
The purpose of this code was to estimate n different models with n different unique independent variables and x common independent variables.
The new code differs only in this line:
modelObj = felm(formulaVar | county + year | 0 | county, data =regDf)
The syntax of the felm function is the following:
The first part consists of ordinary covariates, the second part consists of factors to be projected out. The third part is an IV-specification. The fourth part is a cluster specification for the standard errors.
To be concrete, these means I have a model (formulaVar) to be estimated, with county and year fixed effects, no IV (0) and clustered s.e. by county.
The following error occurs:
Error in terms(formula(as.Formula(formula), rhs = 1), specials = "G") : object 'county' not found
N.B., when estimating outside of lapply function, such as this:
felm <- felm(log(crime2) ~ log(uvar2) + log(var1) + log(var2) + log(var3) | county + year | 0 | county , data = regDf)
the function works perfectly. More on the felm function from lfe package:
https://cran.r-project.org/web/packages/lfe/lfe.pdf
来源:https://stackoverflow.com/questions/43494631/felm-function-doesnt-work-inside-lapply