Undefined columns selected in mediate R - r

I'm currently facing a problem estimating the mediation effect with function mediate from R package mediation.
I'm running three panel regressions with plm from R package plm:
TERR1_us <- plm(log(POLSTAB+1) ~ log(US+1) + RDURAB + DEMOC + log(POP+1) +MUS, data = US, index = c('NUMBER', "YEAR"), model = 'within', effect = "individual")
TERR2_us<- plm(log(TERROR+1) ~ log(US+1) + RDURAB + DEMOC +log(POP+1)+ MUS, data = US, index = c('NUMBER', "YEAR"), model = 'within', effect = "individual")
TERR3_us <- plm(log(POLSTAB+1) ~ log(US+1) + RDURAB + DEMOC +log(POP+1)+MUS +log(TERROR+1), data = US, index = c('NUMBER', "YEAR"), model = 'within', effect = "individual")
To test the mediations significancy I run a bootstrapping test.
results_terr_us <- mediate(TERR2_us, TERR_us, treat="US", mediator="TERR",
boot=TRUE, sims=100)
Unfortunately, I always get the error message Error in [.data.frame(x, , "US") : undefined columns selected
Both models have the same number of observations. I don't think there are any commas that I set wrong.

Related

ADD INTERCEPT TO GMM MODEL IN R (function: pgmm; package: plm)

When I launch the pgmm function using the plm package for my dynamic panel data model I can see the output that shows the results for every variable I inserted in the model (including the lags) but I cannot see the results for the intercept.
Here is my code:
z1.AB <- pgmm(cpi ~ lag(cpi, 1:1)+lag(gdppc, 2:2) + lag(expense, 3:3) + lag(freedom, 2:2) | lag(cpi, 1:10) + lag(gdppc, 1:9) + lag(expense, 1:2) + lag(freedom, 1:8) + lag(democracy, 4:10) + lag(stability, 1:10),
data = model,
effect = "twoways",
model = "twosteps",
collapse = TRUE,
subset = sample == 1)
mtest(z1.AB, order = 1L)
mtest(z1.AB, order = 2L, vcov = vcovHC)
summary(z1.AB, robust = FALSE)
sargan(z1.AB)
Here is my output:
Please help me to put also the intercept in order to let it show with other variables in the output :(
I wanted to get my intercept together with other variables but it does not show.

Comparison between configural and metric model (MI) using semTools

I try to analyze for measurement invariance with multi groups (8 groups; group = "stort"). Below you can see my syntax. The wb variables are all ordinal, ranging from 1-5. There are 8 different groups: n1 =233, n2= 832 n3=67 n4=68 n5=530 n6=169 n7=139 n8=108
The following error occurs: If I try to compare the configural and the metric model using semTools (see syntax) I get an error message: Error in A %*% P.inv : requires numeric/complex matrix/vector arguments (also if I use lavTestLRT()). Other model comparisons run without any errors.
My question is, how can I fix this? I appreciate all comments :)
Here, you can find the dataset: https://drive.google.com/drive/folders/1h9hpFoRhz-zphJ3NSfXtDxbWoad3CmQI?usp=sharing
This is my model:
modelwb.1<- 'kaw =~ wb1.1 + wb2.1 + wb3.1invers + wb4.1invers
paw =~ wb5.1 + wb6.1 + wb7.1invers + wb8.1invers
saw =~ wb9.1 + wb10.1 + wb11.1invers + wb12.1invers
kaw ~ paw + saw
paw ~ saw
kaw ~~ paw + saw
paw ~~ saw
'
fitwb.1 <- cfa(modelwb.1, data=df_wide, estimator = "WLSMV")
summary(fitwb.1, fit.measures = TRUE, standardized = TRUE)
#### Configural model
fitwbcon.1 <- cfa(modelwb.1, data = df_wide, group = "stort", estimator = "WLSMV", missing = "pairwise")
summary(fitwbcon.1, fit.measures = TRUE, standardized = TRUE)
#### Metric model
fitwbmet.1 <- cfa(modelwb.1, data = df_wide, group = "stort", estimator = "WLSMV", missing = "pairwise", group.equal = "loadings")
summary(fitwbmet.1, fit.measures = TRUE, standardized = TRUE)
##### Model comparison: configural & metric
x1<- semTools::compareFit(fitwbcon.1, fitwbmet.1)
summary(x1)
##or:
lavTestLRT(fitwbcon.1, fitwbmet.1)

I cannot get the gam.check function to work

I used the model below to predict marine survival in both SAS and R. The R code is below.
gam_mod<-gam(recruits/smolt ~ s(max, k = 6) + s(medB, k = 10),
weights = smolt, data = GAMdata, method = "REML", family = binomial("logit"))
However when I use gam.check(gam_mod) I get the error message below:
Error in dm[, i] <- sort(residuals(object, type = type)) :
number of items to replace is not a multiple of replacement length

How to cluster standard error in clubSandwich's vcovCR()?

I'm trying to specify a cluster variable after plm using vcovCR() in clubSandwich package for my simulated data (which I use for power simulation), but I get the following error message:
"Error in [.data.frame(eval(mf$data, envir), , index_names) : undefined columns selected"
I'm not sure if this is specific to vcovCR() or something general about R, but could anyone tell me what's wrong with my code? (I saw a related post here How to cluster standard errors of plm at different level rather than id or time?, but it didn't solve my problem).
My code:
N <- 100;id <- 1:N;id <- c(id,id);gid <- 1:(N/2);
gid <- c(gid,gid,gid,gid);T <- rep(0,N);T = c(T,T+1)
a <- qnorm(runif(N),mean=0,sd=0.005)
gp <- qnorm(runif(N/2),mean=0,sd=0.0005)
u <- qnorm(runif(N*2),mean=0,sd=0.05)
a <- c(a,a);gp = c(gp,gp,gp,gp)
Ylatent <- -0.05*T + a + u
Data <- data.frame(
Y = ifelse(Ylatent > 0, 1, 0),
id = id,gid = gid,T = T
)
library(clubSandwich)
library(plm)
fe.fit <- plm(formula = Y ~ T, data = Data, model = "within", index = "id",effect = "individual", singular.ok = FALSE)
vcovCR(fe.fit,cluster=Data$id,type = "CR2") # doesn't work, but I can run this by not specifying cluster as in the next line
vcovCR(fe.fit,type = "CR2")
vcovCR(fe.fit,cluster=Data$gid,type = "CR2") # I ultimately want to run this
Make your data a pdata.frame first. This is safer, especially if you want to have the time index created automatically (seems to be the case looking at your code).
Continuing what you have:
pData <- pdata.frame(Data, index = "id") # time index is created automatically
fe.fit2 <- plm(formula = Y ~ T, data = pData, model = "within", effect = "individual")
vcovCR(fe.fit2, cluster=Data$id,type = "CR2")
vcovCR(fe.fit2, type = "CR2")
vcovCR(fe.fit2,cluster=Data$gid,type = "CR2")
Your example does not work due to a bug in clubSandwich's data extraction function get_index_order (from version 0.3.3) for plm objects. It assumes both index variables are in the original data but this is not the case in your example where the time index is created automatically by only specifying the individual dimension by the index argument.

Error in 1 - diaghat : non-numeric argument to binary operator

I wanto to run a two way fixed effects model and then correct standard errors.
mod3 = plm(Dir ~ Dist +Trade + GDP_sim +GDP +
GDP_capita_constant +Openess + GDP_g + Endowments +
US_PTA + Japan_PTA + FDI + Population+ Democracy+Rule_of_Law+Values+sim+
WTO, data= dfPTA, index = c("Country", "Year"),
model = "within",
effect = "twoway")
this works very well.
However, when running this line
m.2wfe.scc <- coeftest(mod3, vcov = vcovSCC(mod3, type = "HC3", cluster = "group"))
I get the following error message
Error in 1 - diaghat : non-numeric argument to binary operator
All my variables are either numeric or integers (except Country).
Interestingly, I can run
mod4 = plm(Dir ~ Dist + Trade + GDP_sim +GDP +
GDP_capita_constant+Population+FDI, data= dfPTA, index = c("Country", "Year"),
model = "within",
effect = "twoway")
m.2wfe.scc <- coeftest(mod4, vcov = vcovSCC(mod4, type = "HC3", cluster = "group"))
and it works. Alltough I dont see a difference between the subset variables and the full model.
Any ideas?

Resources