ycroissant / plm

Panel Data Econometrics with R
GNU General Public License v2.0
50 stars 13 forks source link

pldv issues with upper treshold #60

Open Tanguy-Richard opened 3 weeks ago

Tanguy-Richard commented 3 weeks ago

Hi,

To put it simply, I want to estimate a random effects model with censored data. I am trying to use the pldv function to do this.

However, I have not been able to implement double threshold censoring. Every time I try to use right censorship, I'm faced with: "Error in maxOptim(fn = fn, grad = grad, hess = hess, start = start, method = "BFGS", : NA in initial gradient"

Here is a toy example to replicate the problems: (it more or less corresponds to the lab experiment pilot data I tried to use)

Data generation:

library(plm)

# Number of individuals
Nb_simulation = 20

# Individual random effects (temporal invariant)
effect_indivs <- rnorm(Nb_simulation,0,3.5)
# Treatments effects (
effect_temps <- c( 1.5 , 3.8 , 4.0 , 3.7 , 5.4 )
# Dataset creation
data_sim <- data.frame(
  temps1 = effect_indivs + effect_temps[1],
  temps2 = effect_indivs + effect_temps[2],
  temps3 = effect_indivs + effect_temps[3],
  temps4 = effect_indivs + effect_temps[4],
  temps5 = effect_indivs + effect_temps[5]
) 
# Independant error term generation
data_sim <- data_sim + rnorm( Nb_simulation*5 , 0 , 3.5 )

# Both side censorship 
data_sim[data_sim<0] <- 0
data_sim[data_sim>10] <- 10

# Dataset reshaping
Tob_value <- data.frame(
  valeurs_pl = c(
    data_sim$temps1,
    data_sim$temps2,
    data_sim$temps3,
    data_sim$temps4,
    data_sim$temps5
  ),
  temps = as.factor(
    c(
    rep(1,Nb_simulation),
    rep(2,Nb_simulation),
    rep(3,Nb_simulation),
    rep(4,Nb_simulation),
    rep(5,Nb_simulation)
  )
  ),
  inds = as.factor(rep(1:Nb_simulation,5))
)

# Use of pdata.frame
Tob_panel <- pdata.frame(Tob_value, index = c("inds","temps"))

One side censorship

Lower treshold

Tob_mod_inf <- pldv(valeurs_pl ~ 1 + temps, data = Tob_panel, model = "random", R = 128, lower = 0, sample = "cens", method = "bfgs")
summary(Tob_mod_inf)

Everything seems to be working fine.

Upper treshold

Tob_mod_sup <- pldv(valeurs_pl ~ 1 + temps, data = Tob_panel, model = "random", R = 128, upper = 10, sample = "cens", method = "bfgs")
summary(Tob_mod_sup) # /!\ error

Which gives an error

Two sides censorship


Tob_mod <- pldv(valeurs_pl ~ 1 + temps, data = Tob_panel, model = "random", R = 128, lower = 0, upper = 10, sample = "cens", method = "bfgs")
summary(Tob_mod) # /!\ error

Same error

Thank you in advance for your help,

Tanguy