r - How to combine a column from a data table to panel data structure -
i have been working large panel data set. simple data set below.
+--------+------+------+-----+-----+--------+ | time | firm | land | lab | cap | gl | +--------+------+------+-----+-----+--------+ | jan-00 | | 25 | 261 | 13 | 161521 | | feb-00 | | 25 | 334 | 15 | 142452 | | mar-00 | | 25 | 156 | 14 | 365697 | | apr-00 | | 28 | 134 | 12 | 355789 | | may-00 | | 28 | 159 | 15 | 376843 | | jun-00 | | 28 | 119 | 12 | 258762 | | jul-00 | | 28 | 41 | 45 | 255447 | | aug-00 | | 28 | 247 | 75 | 188545 | | sep-00 | | 28 | 251 | 41 | 213663 | | oct-00 | | 30 | 62 | 12 | 273209 | | nov-00 | | 30 | 525 | 15 | 317468 | | dec-00 | | 30 | 217 | 16 | 238668 | | jan-01 | b | 42 | 298 | 42 | 241286 | | feb-01 | b | 42 | 109 | 45 | 135288 | | mar-01 | b | 42 | 7 | 24 | 363609 | | apr-01 | b | 42 | 12 | 56 | 318472 | | may-01 | b | 42 | 0 | 12 | 446279 | | jun-01 | b | 45 | 50 | 12 | 390230 | | jul-01 | b | 45 | 143 | 45 | 118945 | | aug-01 | b | 45 | 85 | 25 | 174887 | | sep-01 | b | 45 | 80 | 15 | 183770 | | oct-01 | b | 45 | 214 | 12 | 197832 | | nov-01 | b | 45 | 525 | 15 | 317468 | | dec-01 | b | 45 | 217 | 16 | 238668 | +--------+------+------+-----+-----+--------+
the above dataframe can accessed using following codes.
structure(list(time = structure(c(9l, 7l, 15l, 1l, 17l, 13l, 11l, 3l, 23l, 21l, 19l, 5l, 10l, 8l, 16l, 2l, 18l, 14l, 12l, 4l, 24l, 22l, 20l, 6l), .label = c("apr-00", "apr-01", "aug-00", "aug-01", "dec-00", "dec-01", "feb-00", "feb-01", "jan-00", "jan-01", "jul-00", "jul-01", "jun-00", "jun-01", "mar-00", "mar-01", "may-00", "may-01", "nov-00", "nov-01", "oct-00", "oct-01", "sep-00", "sep-01" ), class = "factor"), firm = structure(c(1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l), .label = c("a", "b"), class = "factor"), land = c(25l, 25l, 25l, 28l, 28l, 28l, 28l, 28l, 28l, 30l, 30l, 30l, 42l, 42l, 42l, 42l, 42l, 45l, 45l, 45l, 45l, 45l, 45l, 45l), lab = c(261l, 334l, 156l, 134l, 159l, 119l, 41l, 247l, 251l, 62l, 525l, 217l, 298l, 109l, 7l, 12l, 0l, 50l, 143l, 85l, 80l, 214l, 525l, 217l ), cap = c(13l, 15l, 14l, 12l, 15l, 12l, 45l, 75l, 41l, 12l, 15l, 16l, 42l, 45l, 24l, 56l, 12l, 12l, 45l, 25l, 15l, 12l, 15l, 16l), gl = c(161521l, 142452l, 365697l, 355789l, 376843l, 258762l, 255447l, 188545l, 213663l, 273209l, 317468l, 238668l, 241286l, 135288l, 363609l, 318472l, 446279l, 390230l, 118945l, 174887l, 183770l, 197832l, 317468l, 238668l)), .names = c("time", "firm", "land", "lab", "cap", "gl"), class = "data.frame", row.names = c(na, -24l))
i converted data panel structure below.
library(zoo) library(plm) sys.setlocale("lc_time", "english") dat["time1"] <- as.yearmon(dat$time,format="%b-%y") pdat <-pdata.frame(dat,index=c("firm","time1"))
original analysis
pdat$cap.lag.ln<-lag(log(pdat$cap), 1) pdat$cap.2lag.ln<-lag(log(pdat$cap), 2) pdat$lab.ln<-log(pdat$lab+1) pdat$lab.lag.ln<-lag(log(pdat$lab+1), 1) model1<- plm(log(land) ~ cap.lag.ln + cap.2lag.ln + lab.ln + lab.lag.ln, model = "within", data=pdat) summary(model1) fv_log <- data.table(model1$model[[1]] - model1$residuals)
just omit nas in pdat resulted lags combine fitted values
pdat2<-na.omit(pdat)
the problem
i want combine these fitted values pdat2 need retain panel structure is. if go following cbind panel structure disappeared.
pdat3 <-cbind(pdat2,fv_log)
even tried omitting old variables , following way.
pdat$cap<-null pdat$lab<-null pdat2 <- na.omit(pdat) pdat2$fv_log <- (model1$model[[1]] - model1$residuals) , run model2 using data=pdat2 following error message comes up. model2<-plm(log(gl)~ cap.lag.ln + cap.2lag.ln + lab.ln + lab.lag.ln + fv_log, data=pdat2, model = "within") **error in model.matrix.pformula(formula, data, rhs = 1, model = model, : na in individual index variable in addition: warning message: in `[.data.frame`(index, as.numeric(rownames(mf)), ) : nas introduced coercion**
can please assist me identify problem , run model 2 in plm structure. thank you
updating answer after op provided code needed replication.
i think using data.table create fv_log complicating factor.
i ran following codes after creation of pdat3 above.
# convert fv_log data.frame fv_log<-data.frame(fv_log) # bind fv_log pdat2 using use pdata.frame preserve structure. pdat3_fix<-pdata.frame(cbind(pdat2,fv_log),index=c("firm","time1"))
here pdat3_fix, identical pdat2 exception of new column:
note resulting data.frame object of class pdata.frame:
> class(pdat3_fix) [1] "pdata.frame" "data.frame"
Comments
Post a Comment