I'm trying to use transformations of my outcomevar in a function that runs a few variants of models and stores the result in a list.
The runpanelsfunction first calls the prepare data function, which creates the lagged and differenced variables of the outcome variable specified as argument in the function. So after preparedata, model data contains outcomevar, doutcomevar and loutcomevar.
My problem is I now need to call/get these transformations of the outcomevar to subset the data such that loutcomevar and doutcomevar is not zero. And then i need to use doutcomevar and loutcomevar in the models.
set.seed(1)
df <- data.frame(firm=rep(LETTERS[1:5],each=10),
date=as.Date("2014-01-01")+1:10,
y1=sample(1:100,50),y2=sample(1:100,50),y3=sample(1:100,50),
x1=sample(1:100,50), x2=sample(1:100,50))
preparedata<-function(testData,outcomevar){
require(data.table)
DT <- as.data.table(testData)
setkey(DT,firm,date)
DT[,lag := c(NA,unlist(.SD)[-.N]), by=firm, .SDcols=outcomevar]
DT[,diff := c(NA,diff(unlist(.SD))), by=firm, .SDcols=outcomevar]
setnames(DT,c("lag","diff"),paste0(c("loutcomevar","doutcomevar")))
return(DT)
modeldata<-as.data.frame(DT)
}
runpanels <- function(testData,outcomevar) {
modeldata<-preparedata(testData,outcomevar)
modeldata<-subset(modeldata,loutcomevar!=0& doutcomevar!=0)
modellist<-list()
modellist$m1<-lm(log(outcomevar)~-1+x1+x2,data=modeldata)
modellist$m2<-lm(log(doutcomevar)~-1+x1+date,data=modeldata)
modellist$m3<-lm(log(outcomevar)~-1+log(loutcomevar)+x1+x2,data=modeldata)
return(modellist)
}
Example use: modelsID1<-runpanels(df,outcomevar="y1")
Unsurprisingly, I get the error when it gets to evaluating "loutcomevar!=0" : Error in eval(expr, envir, enclos) : object 'loutcomevar' not found Called from: eval(e, x, parent.frame())
So it does not find the lagged variable i created in the prepare data function in the environment of the run panels function.
How can I call those variables?
The below example solution from another question was using call which is similar to my problem but i also want to call transformations of my outcomevar which is an argument of the function. Any ideas how to tackle this will be much appreciated!
Example solution from other question that was kind of similar: air <- data(airquality) fm <- lm(Ozone ~ Solar.R, data=airquality)
myfun <- function(fm, name){
dn <- fm$call[['data']]
varname <- deparse(substitute(name))
get(as.character(dn),envir=.GlobalEnv)[varname]
}
Usage: myfun(fm, Temp)