-2

I'm trying to use the pglm function from pglm package to obtain a random effects panel estimation of an ordered logistic model.

When testing the standalone function pglm it gives me the desired result. Here's my specifications:

pglm::pglm(as.numeric(y)~x1+x2+x3, df,family = pglm::ordinal('logit'), 
           model = "random", method = "bfgs", print.level = 3, R = 5, index = 'Year')

where: 1. all explanatory variable {x1,x2,x3} are numerical doubles 2. y is an ordered categorical variable ranging from 1 to 22

The table also includes a "Year" variable ranging from 1996 to 2014, that will be used to build the panel data.

When trying to use the pglm function in another function:

pglm_fun <- function(df){

  df <- data.frame(df)
  pglm::pglm(as.numeric(y)~x1+x2+x3, data =  df,family = pglm::ordinal('logit'), 
               model = "random", method = "bfgs", print.level = 3, R = 5, index = 'Year')

}

I get an error message occurring when calculating

pdata.frame(data, index) 

Error in x[, !na.check] : object of type 'closure' is not subsettable.

When trying to run the code in the console, I do not have such an error and the pdata.frame() function works.

Example of data frame:

df = data.frame(y    = sort(rep(1:4,20)),
       x1   = rnorm(80), 
       x2   = rnorm(80), 
       x3   = rnorm(80),
       Year = rep(sample(1995:1998, replace = FALSE),20))
mike.dl
  • 67
  • 8
  • 1
    What is `index` in your code? There is also an `index()` function in `plm`, which is probably causing the confusion. Also what is `data` ? How does it relate to `df` ? In short: you should post a reproducible example http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Aurèle Jan 11 '17 at 13:24
  • I set the variable `'Year'` as index, and data is simply the data frame I give to the function `pglm_fun`as argument. I'll edit the post with an example of data frame df – mike.dl Jan 11 '17 at 15:08
  • What about `pdata.frame(data, index = "Year")` then? – Aurèle Jan 11 '17 at 15:15
  • Non reproducible for me, running `pglm_fun(df)` works for me. – Helix123 Jan 11 '17 at 16:18
  • Ok, my RStudio compiler is just very unstable. Sometimes it works, sometimes it doesn't, giving me different sorts of errors under this data frame. I'm actually testing a bigger and more complex data frame having about 2k rows, is it possible to share it somehow? – mike.dl Jan 11 '17 at 16:36
  • Maybe you need to declare a variable for the individuals too (and use the `index` argument to declare the variables for individuals as well as times). – Helix123 Jan 11 '17 at 16:43
  • I tried pasting everything into a formula and use R's `as.formula`, but it didn't work either. Moreover, I can append at most one individual and one time index. – mike.dl Jan 12 '17 at 07:50

1 Answers1

0

Ok, I figured it out. In this case there are environments problems in R, since the funciton I was using was making copies of variables instead of using global variables directly. The problem is solved by declaring individual names in the function and leaving the data frame as a global variable:

pglm_fun1 <- function("y","x1","x2","x3","Year"){
  require("pglm")
  pglm(as.numeric(y)~x1+x2+x3, data = df, family = ordinal('logit'), 
          model = "random", method = "bfgs", print.level = 3, R = 5, index = 'Year')

}
mike.dl
  • 67
  • 8