Warning Messages : confint command for lmer model - r

I am doing a simulation study for sample size calculation for multilevel modeling. I am using R for both simulation and estimation . As posted in this post , confint function was not working and that was due to model mis-specification . As, I have one independent variable (X) at individual level and one independent variable (Z) at group level and since the simulation study assumes a zero correlation between the intercept deviations and X effect deviations across grouping factors , the model need to be specified as :
fit <- lmer(Y~X+Z+X:Z+(X||group),data=sim_data)
Now the confint.merMod function works . But another problem arises . I am checking the effect of sample size for various combination of parameters . But for small group size , I found warning message more than 50 (though it produces a result also) . Some of those are :
Warning messages:
1: In optwrap(optimizer, par = thopt, fn = mkdevfun(rho, ... :
convergence code 3 from bobyqa: bobyqa -- a trust region step failed to reduce q
4: In zeta(shiftpar, start = opt[seqpar1][-w]) :
slightly lower deviances (diff=-1.42109e-14) detected
5: In nextpar(mat, cc, i, delta, lowcut, upcut) :
Last two rows have identical or NA .zeta values: using minstep
7: In zetafun(np, ns) : slightly lower deviances (diff=-1.42109e-14) detected
8: In FUN(X[[i]], ...) : non-monotonic profile
24: In nextpar(mat, cc, i, delta, lowcut, upcut) :
unexpected decrease in profile: using minstep
25: In profile.merMod(object, which = parm, signames = oldNames, ... :
non-monotonic profile for (Intercept)
But there are no warning message , if I increase the number of group size .
In this case , should I worry of the warnings ?

Related

Error when trying to fit Hierarchical GAMs (Model GS or S) using mgcv

I have a large dataset (~100k observations) of presence/absence data that I am trying to fit a Hierarchical GAM with individual effects that have a Shared penalty (e.g. 'S' in Pedersen et al. 2019). The data consists of temp as numeric, region (5 groups) as a factor.
Here is a simple version of the model that I am trying to fit.
modS1 <- gam(occurrence ~ s(temp, region), family = binomial,
data = df, method = "REML")
modS2 <- gam(occurrence ~ s(temp, region, k= c(10,4), family = binomial,
data = df, method = "REML")
In the first case I received the following error:
Which I assumed it because k was set too high for region given there are only 5 different regions in the data set.
Error in smooth.construct.tp.smooth.spec(object, dk$data, dk$knots) :
NA/NaN/Inf in foreign function call (arg 1)
In addition: Warning messages:
1: In mean.default(xx) : argument is not numeric or logical: returning NA
2: In Ops.factor(xx, shift[i]) : ‘-’ not meaningful for factors
In the second case I attempt to lower k for region and receive this error:
Error in if (k < M + 1) { : the condition has length > 1
In addition: Warning messages:
1: In mean.default(xx) : argument is not numeric or logical: returning NA
2: In Ops.factor(xx, shift[i]) : ‘-’ not meaningful for factors
I can fit Models G and GI and I from Pedersen et al. 2019 with no issues. It is models GS and S where I run into issues.
If anyone has any insights I would really appreciate it!
The bs = "fs" argument in the code you're using as a guide is important. If we start at the ?s help page and click on the link to the ?smooth.terms help page, we see:
Factor smooth interactions
bs="fs" Smooth factor interactions are often produced using by variables (see gam.models), but a special smoother class (see factor.smooth.interaction) is available for the case in which a smooth is required at each of a large number of factor levels (for example a smooth for each patient in a study), and each smooth should have the same smoothing parameter. The "fs" smoothers are set up to be efficient when used with gamm, and have penalties on each null space component (i.e. they are fully ‘random effects’).
You need to use a smoothing basis appropriate for factors.
Notably, if you take your source code and remove the bs = "fs" argument and attempt to run gam(log(uptake) ∼ s(log(conc), Plant_uo, k=5, m=2), data=CO2, method="REML"), it will produce the same error that you got.

mnps function of R twang package shows warning errors for Absolute Standardized Difference plot

I have a dataset with 3465 observation and 5 variables as follows (named: dataset)
Variables
$ Treat --> factor(3 level: "1", "2", "3")
$ Age --> integer(eg. 24,54,etc.)
$ Mortality --> integer(0=no,1= yes)
$ LDL --> integer(eg. 200,120,143, etc.)
$ Smoking --> integer(0=no, 1= yes)
I used mnps function in twang package to weight my observations according to treat
mnps code
mnps.dataset <- mnps(treat ~ Age + LDL + Smoking, data = dataset, estimand = "ATE", stop.method = c("es.mean", "ks.mean"), n.trees=3000)
```
### Plot ###
Then I used plot function to assess the balance of the data after weighting.
There was no problem for plots 1 and 2, (balanced of treatment per iterations, and propensity score distribution of treatments.)
Plot code
plot(mnps.dataset, plots = 1)
plot(mnps.dataset, plots = 2)
But when i wanted to run plot 3 for Absolute Standardized difference, it made an error with some warnings and an empty plot:
Warning Error
plot(mnps.dataset, plots = 3)
Warning messages:
1: In order(as.numeric(x)) : NAs introduced by coercion
2: In split(as.numeric(x) [ord], gg[ord]) : NAs introduced by coercion
3: ...(same as error 1)
4: ... (same as error 2)
5: ... (same as error 1)
6: ... (same as error 2)
7: ... (same as error 1)
8: ... (same as error 2)
9: In panel.superpose(x, y, type = type, groups = groups, ... : NAs introduced by coercion
10: ...
11: ...
12: ...
___________________
Note : "..." means repeated previous error
Image of empty plot generated by mnps plot=3
This is a bug in twang (I was able to replicate it on my own dataset). There isn't anything you can do about it.
You can instead use the cobalt package, which facilitates balance checking and is compatible with objects from twang. A plot that conveys the same information as the type 3 plot is a Love plot, which can be called using love.plot() in cobalt.
If you run love.plot(mnps.dataset, which.treat = .all) you can see standardized mean differences for all pairwise comparisons. See the section in the cobalt vignette on multi-category treatments for more information on how to use it.

Resuming an uncoverged ergm run with ergm package in R

I am using the ergm package in R to explore exponential random graph models for network data. Here's the network:
gn
Network attributes:
vertices = 678
directed = TRUE
hyper = FALSE
loops = FALSE
multiple = FALSE
bipartite = FALSE
total edges= 1663
missing edges= 0
non-missing edges= 1663
Vertex attribute names:
indegree membership num_tweets vertex.names
Edge attribute names not shown
After fitting simpler models, one with a term for edges and mutual and one with edges, mutual, and nodefactor("membership", base = 4) (where membership is a factor with four levels), I fit a model with nodematch("membership", diff = T) added, as follows:
model1 <- ergm(gn ~ edges +
mutual +
nodefactor("membership", base = 4) +
nodematch("membership", diff = T)
)
While the previous models converged, this model led to this message:
MCMLE estimation did not converge after 20 iterations. The estimated coefficients may not be accurate. Estimation may be resumed by passing the coefficients as initial values; see 'init' under ?control.ergm for details.
As instructed, I took a look at ?control.ergm: Passing control.ergm(init=coef(prev.fit)) can be used to “resume” an uncoverged ergm run, but see enformulate.curved.
OK, this sounds good, I searched for how control.ergm is used, and found an example from this page, and I tested that this worked by passing control = control.ergm(MCMLE.maxit = 50) as an argument, which increased the number of times the parameters for the MCMC should be updated by maximizing the MCMC likelihood from 20 to 50. I then did the following:
model1a <- ergm(gn ~ edges +
mutual +
nodefactor("membership", base = 4) +
nodematch("membership", diff = T),
control = control.ergm(init=coef(prev.fit))
)
But, this message is returned: Error in coef(prev.fit) : object 'prev.fit' not found.
I also tried to pass the model object (i.e., model1a) instead of prev.fit, but that led to this not-too-productive error:
Error in if (drop) { : argument is not interpretable as logical
In addition: Warning message:
In if (drop) { :
the condition has length > 1 and only the first element will be used
So, how can I "resume" an unconverged model by passing control.ergm(init = coef(prev.fit)) - or via a different approach?

Difficulty to construct profile confidence interval in lmer model

When I fit lmer model with my data , there is no warning message. But when I tried to construct confidence interval by confint , it shows the following warning message :
Warning messages:
1: In FUN(X[[i]], ...) : non-monotonic profile
2: In nextpar(mat, cc, i, delta, lowcut, upcut) :
Last two rows have identical or NA .zeta values: using minstep
3: In FUN(X[[i]], ...) : non-monotonic profile
Is there any way to overcome the warning message , that is, is there any argument(option) which can remove the problem ? I have used the argument control=lmerControl(optCtrl=list(maxfun=20000)) :
fit <- lmer(Y~X+Z+X:Z+(X||group),data=dat,control=lmerControl(optCtrl=list(maxfun=20000)))

Neural network in R using the caret package [duplicate]

This question already has answers here:
Using neuralnet with caret train and adjusting the parameters
(2 answers)
Closed 8 years ago.
So I've read a paper that had used neural networks to model out a dataset which is similar to a dataset I'm currently using. I have 160 descriptor variables that I want to model out for 160 cases (regression modelling). The paper I read used the following parameters:-
'For each split, a model was developed for each of the 10 individual train-test folds. A three layer back-propagation net with 33 input neurons and 16 hidden neurons was used with online weight updates, 0.25 learning rate, and 0.9 momentum. For each fold, learning was conducted from a total of 50 different random initial weight starting points and the network was allowed to iterate through learning epochs until the mean absolute error (MAE) for the validation set reached a minimum. '
Now they used a specialist software called Emergent in order to do this, which is a very specialised neuronal network model software. However, as I've done previous models before in R, I have to keep to it. So I'm using the caret train function in order to do 10 cross fold validation, 10 times with the neuralnet package. I did the following:-
cadets.nn <- train(RT..seconds.~., data = cadet, method = "neuralnet", algorithm = 'backprop', learningrate = 0.25, hidden = 3, trControl = ctrl, linout = TRUE)
I did this to try and tune the parameters as closely to the ones used in the paper, however I get the following error message:-
layer1 layer2 layer3 RMSE Rsquared RMSESD RsquaredSD
1 1 0 0 NaN NaN NA NA
2 3 0 0 NaN NaN NA NA
3 5 0 0 NaN NaN NA NA
Error in train.default(x, y, weights = w, ...) :
final tuning parameters could not be determined
In addition: There were 50 or more warnings (use warnings() to see the first 50)
Do you know what I'm doing wrong? It works when I do nnet, but I can't tune the parameters for that to make it similar to the ones used in the paper I'm trying to mimic.
This is what I get in the warnings() fifty times:-
1: In eval(expr, envir, enclos) :
model fit failed for Fold01.Rep01: layer1=1, layer2=0, layer3=0 Error in neuralnet(form, data = data, hidden = nodes, ...) :
formal argument "hidden" matched by multiple actual arguments
2: In data.frame(..., check.names = FALSE) :
row names were found from a short variable and have been discarded
3: In eval(expr, envir, enclos) :
model fit failed for Fold01.Rep01: layer1=3, layer2=0, layer3=0 Error in neuralnet(form, data = data, hidden = nodes, ...) :
formal argument "hidden" matched by multiple actual arguments
4: In data.frame(..., check.names = FALSE) :
row names were found from a short variable and have been discarded
5: In eval(expr, envir, enclos) :
model fit failed for Fold01.Rep01: layer1=5, layer2=0, layer3=0 Error in neuralnet(form, data = data, hidden = nodes, ...) :
formal argument "hidden" matched by multiple actual arguments
Thanks!
From the error message, the 'hidden' parameter is not being properly matched. Looking at the documentation, there are only three training parameters for method = "neuralnet", layer1, layer2, layer3. Take a look at the link and use a different a method where you can specify the desired parameters.

Resources