Dear all-
I am have trouble in using the model="ht" option in function plm from
the plm library. I am using
Package: plm Version: 1.1-1; R version 2.8.1 (2008-12-22) running on a
FC-8 linux machine.
Here is what I am trying to do:
##----------------------------------------------------------------------------
R> ###Prob 6 Chapter 3 Use R! Applied Econometrics with R (Kleiber &
Zeileis)
R> ## hlp(PSID1982) => cross section data for 1982 only Need panel data
I guess
R> ## found full set on STATA web site
R> ## http://www.stata-press.com/data/r10/psidextract.dta
R> ## STATA results in Sec 2 of: folk.uio.no/erikbi/ECON5120_H07_Note19.pdf
R> library("foreign")
R> fulldat <- read.dta("~/Desktop/psidextract.dta")
R> library("plm")
R>
R> fulldat.plm = plm.data(fulldat,index=c("id","t"))
R>
R> earn_plm <- plm(lwage~ occ+ south+ smsa+ ind+ exp+ exp2+ wks+
+ ms+ union+ fem+ blk+ ed | exp+ exp2+ wks+ ms+ union+ ed,
+ data = fulldat.plm,model="ht")
Error in names(result) <- nf : attempt to set an attribute on NULL
I have tried several variations and some other data sets (not so easily
reproducible for others as this one) but have yet to obtain an error
free result.
Thanks in advance for any help
Ron
--
R. R. Burns
Retired
Oceanside, CA
On Sat, 25 Apr 2009, Ron Burns wrote:> Dear all- > > I am have trouble in using the model="ht" option in function plm from the plm > library. I am using > Package: plm Version: 1.1-1; R version 2.8.1 (2008-12-22) running on a FC-8 > linux machine. > > Here is what I am trying to do: > > ##---------------------------------------------------------------------------- > R> ###Prob 6 Chapter 3 Use R! Applied Econometrics with R (Kleiber & > Zeileis) > R> ## hlp(PSID1982) => cross section data for 1982 only Need panel data I > guess > R> ## found full set on STATA web site > R> ## http://www.stata-press.com/data/r10/psidextract.dta > R> ## STATA results in Sec 2 of: folk.uio.no/erikbi/ECON5120_H07_Note19.pdf > R> library("foreign") > R> fulldat <- read.dta("~/Desktop/psidextract.dta") > R> library("plm") > R> > R> fulldat.plm = plm.data(fulldat,index=c("id","t")) > R> > R> earn_plm <- plm(lwage~ occ+ south+ smsa+ ind+ exp+ exp2+ wks+ > + ms+ union+ fem+ blk+ ed | exp+ exp2+ wks+ ms+ union+ ed, > + data = fulldat.plm,model="ht") > Error in names(result) <- nf : attempt to set an attribute on NULL > > I have tried several variations and some other data sets (not so easily > reproducible for others as this one) but have yet to obtain an error free > result.This seems to be a bug in "plm". traceback() shows: R> traceback() 4: lev2var(data[, -c(K - 1, K)]) 3: plm.ht(formula, data) 2: switch(model, within = plm.within(formula, data, effect), between = plm.between(formula, data, effect), pooling = plm.pooling(formula, data), random = plm.random(formula, data, effect, random.method, inst.method), ht = plm.ht(formula, data), fd = plm.fd(formula, data)) 1: plm(lwage ~ occ + south + smsa + ind + exp + exp2 + wks + ms + union + fem + blk + ed | exp + exp2 + wks + ms + union + ed, data = fulldat.plm, model = "ht") lev2var() is a function internal to "plm" and it apparently fails if there are no factors in the data. (The result is NULL and lev2var() tries to assign names to it; these are character(0) but still this fails with the error message you have quoted above.) Maybe Yves can clarify this. A workaround for your example is to code the dummy variables as factors (which might also be more useful in other situations). For example: R> for(i in c(3:9, 11)) fulldat[[i]] <- factor(fulldat[[i]]) R> fulldat.plm = plm.data(fulldat,index=c("id","t")) R> earn_plm <- plm(lwage~ occ+ south + smsa+ ind + exp + exp2 + wks + + ms + union + fem + blk + ed | exp + exp2 + wks + ms + union + ed, + data = fulldat.plm, model = "ht") Of course, more meaningful factor levels would be desirable but at least this works. It gives similar results to those that you refer to above, but they are not identical. Maybe Yves can something more about the differences to Stata. hth, Z> Thanks in advance for any help > Ron > -- > R. R. Burns > Retired > Oceanside, CA > > >
The problem arises because your data.frame contains
no factors and plm:::lev2var implicitly assumes that its
input data.frame, x, contains both factor and non-factor
columns. That is because, e.g., the output of
sapply(zeroLongInput, func)
has class NULL, not the class of the output of func()
(since func() was never called it doesn't know what that
class would be). Similarly, the output of unlist(zeroLongList)
and names(zeroLongVector) have class NULL. It might be
nice if names(NULL)<-character(0) worked, since it would
not corrupt NULL, but it doesn't. It also might be nice if sapply
had an option that let you declare the output type of func
so sapply's output always had the expected type, but that
wouldn't solve the whole problem.
A possible change to plm:::lev2var is to pass the output
of the some of these calls through as.character() so the NULL
is changed to character(0), as in
plm:::lev2var <- function (x, ...)
{
is.fact <- sapply(x, is.factor)
not.fact <- names(x)[!is.fact]
names(not.fact) <- not.fact
x <- x[is.fact]
wl <- lapply(x, levels)
nl <- sapply(wl, length)
nf <- as.character(rep(names(nl), nl)) # as.character in case of
0-long result
result <- as.character(unlist(wl)) # ditto
names(result) <- nf
result <- paste(names(result), result, sep = "")
names(nf) <- result
c(nf, not.fact)
}
I used trace(plm:::lev2var, edit=T) to test this change -- the real fix
involves changing the source code and recompiling the package.
Bill Dunlap
TIBCO Software Inc - Spotfire Division
wdunlap tibco.com
[R] plm Hausman-Taylor model
Ron Burns rrburns at cox.net
Sat Apr 25 19:47:24 CEST 2009
Dear all-
I am have trouble in using the model="ht" option in function plm from
the plm library. I am using
Package: plm Version: 1.1-1; R version 2.8.1 (2008-12-22) running on a
FC-8 linux machine.
Here is what I am trying to do:
##----------------------------------------------------------------------
------
R> ###Prob 6 Chapter 3 Use R! Applied Econometrics with R (Kleiber &
Zeileis)
R> ## hlp(PSID1982) => cross section data for 1982 only Need panel data
I guess
R> ## found full set on STATA web site
R> ## http://www.stata-press.com/data/r10/psidextract.dta
R> ## STATA results in Sec 2 of:
folk.uio.no/erikbi/ECON5120_H07_Note19.pdf
R> library("foreign")
R> fulldat <- read.dta("~/Desktop/psidextract.dta")
R> library("plm")
R>
R> fulldat.plm = plm.data(fulldat,index=c("id","t"))
R>
R> earn_plm <- plm(lwage~ occ+ south+ smsa+ ind+ exp+ exp2+ wks+
+ ms+ union+ fem+ blk+ ed | exp+ exp2+ wks+ ms+ union+
ed,
+ data = fulldat.plm,model="ht")
Error in names(result) <- nf : attempt to set an attribute on NULL
I have tried several variations and some other data sets (not so easily
reproducible for others as this one) but have yet to obtain an error
free result.
Thanks in advance for any help
Ron
--
R. R. Burns
Retired
Oceanside, CA