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.
Related
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'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.
I'm quite new to R and I think my problem is quite simple but I cannot seem to work it out. I've looked at similar problems on here but I can't seem to get a solution to work for my specific problem.
I'm using the Wage data set that comes as part of the ISLR package to try and model wage as a function of age of differing polynomial degrees.
library(ISLR)
attach(Wage)
I'm performing regression onto wage with age up to degree 10 and then I want to apply the anova test to each model and investigate the results. The closest I have got is this;
for (i in 1:10) {
fit[[i]] <- lm(wage~poly(age, i) , data = Wage)
result[[i]] <- aov(as.formula(paste(fit[i], "~ wage")))
}
which results in this error;
Error in model.frame.default(formula = as.formula(paste(fit[i], "~ wage")), :
invalid type (list) for variable 'list(coefficients = c((Intercept) = 111.703608201744, poly(age, i) = 447.067852758315), residuals = c(231655 = -19.3925481434428, 86582 = -28.2033380861585, 161300 = 17.4500251413464, 155159 = 42.5676926169455, 11443 = -42.0253778623369, 376662 = 7.21810821763668, 450601 = 56.7036617292801, 377954 = 8.79783605559716, 228963 = 8.18131081863092, 81404 = 10.197404483506, 302778 = 3.61466467869064, 305706 = -24.4688637359983, 8690 = -16.9669134309657, 153561 = 25.4168784550536, 449654 = 14.807739524331, 447660 = -27.2938944517631, 160191 = -25.1943075097583, 230312 = 95.773820436023, 301585 = 7.84450555272621, 153682 = -9.27460094600634, 158226 = 91.9620415895517, 11141 = -59.5891117312741, 448410 = -49.3664897185768, 305116 = 50.6467028157233, 233002 = -14.5085059894098, 8684 = 161.240161560035, 229379 = -28.0716427246922, 86064 = -40.6412633049063, 378472 = -5.75413931818888, `1
Any help would be greatly appriciate and apologises for being such a R noob.
Thanks!!
To answer your question, you can do:
library(ISLR)
attach(Wage)
fit <- vector("list",10)
result <- vector("list",10)
for (i in 1:10) {
thisForm <- paste0("wage~poly(age, ",i,")")
fit[[i]] <- lm(thisForm , data = Wage)
result[[i]] <- aov(fit[[i]])
}
Above I created the formula so that you can see it in the fit / aov object, instead of having i.
Note that the lm fit is already in the aov object, meaning, you can get coefficients, predict and do things you need with lm directly on the aov object. You don't need to store the lm fit separately:
coefficients(result[[1]]) ; coefficients(fit[[1]])
(Intercept) poly(age, i)
111.7036 447.0679
(Intercept) poly(age, i)
111.7036 447.0679
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.
Im trying to fit a mixed effect model to asses for effects upon the rate of germinated polen grains. I started with a binomial distribution with a model structure like this:
glmer(cbind(NGG,NGNG) ~ RH3*Altitude + AbH + Date3 + (1 | Receptor/Code/Plant) +
(1 | Mountain/Community), data=database, family="binomial",
control = glmerControl(optimizer="bobyqa"))
Where NGG is the number of successes (germinated grains per stigma, can vary from 0 to e.g. 55), NGNG the number of failures (non-germinated grains 0 to e.g. 80). The issue is, after seeing the results, data seems to be over-dispersed, as indicated by the function (found in http://rstudio-pubs-static.s3.amazonaws.com/263877_d811720e434d47fb8430b8f0bb7f7da4.html):
overdisp_fun <- function(model) {
vpars <- function(m) {
nrow(m)*(nrow(m)+1)/2
}
model.df <- sum(sapply(VarCorr(model), vpars)) + length(fixef(model))
rdf <- nrow(model.frame(model))-model.df
rp <- residuals(model, type = "pearson") # computes pearson residuals
Pearson.chisq <- sum(rp^2)
prat <- Pearson.chisq/rdf
pval <- pchisq(Pearson.chisq, df = rdf, lower.tail = FALSE)
c(chisq = Pearson.chisq, ratio = prat, rdf = rdf, p = pval)
}
The output was:
chisq = 1.334567e+04, ratio = 1.656201e+00, rdf = 8.058000e+03, p = 3.845911e-268
So I decided to try a beta-binomial in glmmTMB as follows (its important to keep this hierarchical structure):
glmmTMB(cbind(NGG,NGNG) ~ RH3*Altitude + AbH + Date3 + (1 | Receptor/Code/Plant) +
(1 | Mountain/Community), data=database,
family=betabinomial(link = "logit"), na.action = na.omit, weights=NGT)
When I run it.. says:
Error in nlminb(start = par, objective = fn, gradient = gr, control = control$optCtrl) : (converted from warning) NA/NaN function evaluation
Is there something wrong in the model writing? I already checked for posible issues in (http://rstudio-pubs-static.s3.amazonaws.com/263877_d811720e434d47fb8430b8f0bb7f7da4.html) but did not find any solution yet.
thanks