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.
I am new to mirt models and tried to estimate a graded response model with the mirt-package in Rstudio using the mirt command:
x <- pid[,c(items.25,items.1,items.13)]
#x= dataframe with 1228 observations of 24 variables
# define bifactor model
#model syntax
mirtsyn <- paste0("G = ",paste0(colnames(x), collapse = ","),"\n",
"F1 = ",paste0(items.25,collapse = ","),"\n",
"F2 = ",paste0(items.1,collapse = ","),"\n",
"F3 = ",paste0(items.13,collapse = ","))
mirtmodel <- mirt.model(mirtsyn, itemnames=colnames(x))
# estimate model
fit <- mirt(x,model = mirtmodel,itemtype = "graded", SE = FALSE, method = "MHRM")
#itemtype = graded means its using a graded response model,
#method= MHRM (Metropolis-Hastings Robbins-Monro) means it uses stochastic methods for estimation'
I got this error message:
Error in draw.thetas(theta0 = gtheta0[[g]], pars = pars[[g]], fulldata = Data$fulldata[[g]], :
MH sampler failed. Model is likely unstable or may need better starting valuesFALSE
all-
First-time poster, here, so please be forbearing if I've violated some of the conventions for asking questions (like, for example, providing a replicable example).
I'm trying to estimate a Generalized Additive Mixed Model using the "gamm" function with this code:
fit1.1 = gamm(opioidNonFatalOD ~ s(mandatoryReg.l2, k = 3, fx = TRUE,
bs = "cr") +
s(coalitionActive.l2, k = 3, fx = TRUE, bs = "cr") +
monthsSinceJan2011 +
everFunded +
ICD10 +
spoke5 +
hub +
s(monthly2, bs = "cc", fx = FALSE, k = 4) +
s(county2, bs = "re"),
#+ offset(log(population / 100000)),
correlation = corAR1(form = ~ monthsSinceJan2011 | county2),
data = tsData,
family = quasipoisson, offset = log(population / 100000),
niterPQL = 20,
verbosePQL = TRUE)
For some reason, it looks like the "offset" argument isn't getting passed to gammPQL. I get this error:
iteration 1
Quitting from lines 201-220 (pfs_model_experiments_041520.Rmd)
Error in lme(fixed = fixed, random = random, data = data, correlation = correlation, :
unused argument (offset = log(population/1e+05))
Calls: <Anonymous> ... withVisible -> eval -> eval -> gamm -> eval -> eval -> gammPQL
Execution halted
Here're the traceback messages:
Error in lme(fixed = fixed, random = random, data = data, correlation = correlation, : unused argument (offset = log(population/1e+05))
4.
gammPQL(y ~ X - 1, random = rand, data = strip.offset(mf), family = family, correlation = correlation, control = control, weights = weights, niter = niterPQL, verbose = verbosePQL, mustart = mustart, etastart = etastart, ...) at <text>#1
3.
eval(parse(text = paste("ret$lme<-gammPQL(", deparse(fixed.formula), ",random=rand,data=strip.offset(mf),family=family,", "correlation=correlation,control=control,", "weights=weights,niter=niterPQL,verbose=verbosePQL,mustart=mustart,etastart=etastart,...)", sep = "")))
2.
eval(parse(text = paste("ret$lme<-gammPQL(", deparse(fixed.formula), ",random=rand,data=strip.offset(mf),family=family,", "correlation=correlation,control=control,", "weights=weights,niter=niterPQL,verbose=verbosePQL,mustart=mustart,etastart=etastart,...)", sep = "")))
1.
gamm(opioidNonFatalOD ~ s(mandatoryReg.l2, k = 3, fx = TRUE, bs = "cr") + s(coalitionActive.l2, k = 3, fx = TRUE, bs = "cr") + monthsSinceJan2011 + everFunded + ICD10 + spoke5 + hub + s(monthly2, bs = "cc", fx = FALSE, k = 4) + s(county2, bs = "re"), ...
I've tried using the offset as a term in the model (see commented-out code), but get a similar error.
Just be inspecting the code, does anyone have an idea of what I'm doing wrong?
Thanks,
David
tl;dr;
Create the offset outside the gamm function and then pass it to the formula using ...+offset().
In your example then use:
tsData$off = log(tsData$population/100000)
gamm(opioidNonFatalOD ~ <other variables> + s(county2, bs = "re") + offset(off),
<other stuffs>)
The general syntax for gams to add an offset is to include it in the formula, like y ~ ... + x + offset(offset_variable). However, as seen in the examples below it seems as if gammis struggling to parse functions (i.e. the log or division) within the offset function.
Some examples:
library(mgcv)
# create some data
set.seed(1)
dat <- gamSim(6,n=200,scale=.2,dist="poisson")
# create an offset
dat$off1 = (dat$y+1)*sample(2:10, 200, TRUE)
Attempt 1: finds off1 but errors likely due to the large values in off1 (and we really would like the log transfromed, or whichever link function was used)
m1 <- gamm(y~s(x0)+s(x1)+s(x2) + offset(off1),
family=poisson,data=dat,random=list(fac=~1))
Maximum number of PQL iterations: 20
iteration 1
iteration 2
Show Traceback
Rerun with Debug
Error in na.fail.default(list(Xr.1 = c(-0.00679246534326368, -0.0381904761033802,
:missing values in object
Attempt 2: can't seem to find off1 after log transform within offset function
m2 <- gamm(y~s(x0)+s(x1)+s(x2) + offset(log(off1)),
family=poisson, data=dat,random=list(fac=~1))
Maximum number of PQL iterations: 20
iteration 1
Show Traceback
Rerun with Debug
Error in eval(predvars, data, env) : object 'off1' not found
Attempt 3: define offset term outside offset function
# Success
dat$off2 = log(dat$off1)
m3 <- gamm(y~s(x0)+s(x1)+s(x2) + offset(off2),
family=poisson, data=dat, random=list(fac=~1))
So create the offset variable outside then pass it to the gamm formula.
Trying to run a Cross validation of a zero-inflated poisson model using cv.zipath from the mpath package.
Fitting the LASSO
fit.lasso = zipath(estimation_sample_nomiss ~ .| .,
data = missings,
nlambda = 100,
family = "poisson",
link = "logit")
Cross validation
n <- dim(docvisits)[1]
K <- 10
set.seed(197)
foldid <- split(sample(1:n), rep(1:K, length = n))
fitcv <- cv.zipath(F_time_unemployed~ . | .,
data = estimation_sample_nomiss, family = "poisson",
nlambda = 100, lambda.count = fit.lasso$lambda.count[1:30],
lambda.zero = fit.lasso$lambda.zero[1:30], maxit.em = 300,
maxit.theta = 1, theta.fixed = FALSE, penalty = "enet",
rescale = FALSE, foldid = foldid)
I encounter the following error:
Error in model.frame.default(formula = F_time_unemployed ~ . + ., data = list(: variable lengths differ (found for '(weights)')
I have cleaned the sample of all NA's but still encounter the error message.
The solution turns out to be that the cv.zipath() command does not accept tibble data formats - at least in this instance. (No guarantee as to how this statement can be generalised). Having used dplyr commands, one needs to convert back to data frame. Thus, the solution is as simple as as.dataframe().
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