Error in rep(" ", spaces1) : invalid 'times' argument - r

I'm trying to carry out covariate balancing using the ebal package. The basic code is:
W1 <- weightit(Conformidad ~ SexoCon + DurPetFiscPrisión1 +
Edad + HojaHistPen + NacionCon + AnteVivos +
TipoAbog + Reincidencia + Habitualidad + Delitos,
data = Suspension1,
method = "ebal", estimand = "ATT")
I then want to check the balance using the summary function:
summary(W1)
This originally worked fine but now I get the error message:
Error in rep(" ", spaces1) : invalid 'times' argument
It's the same dataset and same code, except I changed some of the covariates. But now even when I go back to original covariates I get the same error. Any ideas would be very appreciated!!

I'm the author of WeightIt. That looks like a bug. I'll take a look at it. Are you using the most updated version of WeightIt?
Also, summary() doesn't assess balance. To do that, you need to use cobalt::bal.tab(). summary() summarizes the distribution of the weights, which is less critical than examining balance. bal.tab() displays the effect sample size as well, which is probably the most important statistic produced by summary().

I encountered the same error message. This happens when the treatment variable is coded as factor or character, but not as numeric in weightit.
To make summary() work, you need to use 1 and 0.

Related

glmer warning: parameters or bounds appear to have different scalings

I get the following warning from glmer:
m <- glmer(cbind(Y, N) ~ c1 + c2 + c3 + (1|g1:Year) + (1 + c1 + c2|g1) + (1|g1:Site),
family = binomial, data = data,
control = glmerControl(optimizer ='optimx', optCtrl=list(method='nlminb')))
# Warning in optimx.check(par, optcfg$ufn, optcfg$ugr, optcfg$uhess, lower, :
# Parameters or bounds appear to have different scalings.
# This can cause poor performance in optimization.
# It is important for derivative free methods like BOBYQA, UOBYQA, NEWUOA.
This is interesting, since all my covariates are scaled (c1: mean = 5.410769e-16, sd = 1), (c2: mean = -2.411114e-16, sd = 1), (c3: mean = 7.602661e-18, sd = 1).
What does this is warning actually mean? All my covariates are scaled, see above. Scaling them again won't fix it.
Shall I have to be concerned about this warning in the sense my model could return unreliable estimates? I got no other warnings or errors.
Thank you!
PS: note - the warning seems to be kinda non-deterministic, on certain data sets in different runs I've observed it sometimes is present and sometimes isn't.
I am rather late with this but since you haven't got other answers maybe someone can still use it. The source of this message is the optimx package. You use optimx as the non-linear optimiser.
The message is the result of a scale check (see the scalecheck() function in the manual). It raises suspicion when the parameter space could be set too narrow. However, this function can also throw misleading warnings. John Nash himself wrote in the manual: "It is, however, an imperfect and heuristic tool, and could be improved." If you get good results, you are probably okay.
Hope this help,
Jan

Trying to run this generalised linear model to find the effect on mass of crickets by multiple random effects and I am getting this following error

I am trying to run a glm model to check the variation in the mass of cricket which could be affected by age(numeric), altitude(High or low), temperature(two different temperatures) and the incubators(4 different incubators) they are kept it.
I have tried the glm model which seems to be fine theoretically. The data on excel is all checked as well. I am assuming I have to convert some of the data into binary or some sorts.
glm(crick$Mass ~ crick$Altitude*crick$Age + crick$Altitude*crick$suare(Altitude) + 1/crick$Nymph.ID + 1/crick$Population + crick$Temperature*crick$Altitude + 1/crick$Incubator)
This is the code I am trying to run.
Error in eval(predvars, data, env) : attempt to apply non-function this is the error message.
You need to properly specify the formula (e.g. see examples in ?glm). Try
glm(Mass ~ Altitude*Age + 1/Nymph.ID + 1/Population + Temperature*Altitude + 1/Incubator, data = cricks)
Note that I excluded Altitude*suare(Altitude) which is invalid R syntax for formulas. You'll have to explain more what you're trying to go with here.

Propensity score matching in R (problem with code)

I am a complete beginner at R and don't have much time to complete this analysis.
I need to run propensity score matching. I am using RStudio and have
Uploaded my dataset which is called 'R' and was saved on my desktop
Installed and loaded package Matchit
My dataset has the following headings:
BA (my grouping variable of which someone is either on BA or not, 0=off, 1=on),
Then age, sex, timesincediagnosis, TVS, and tscore which are my matching variables.
I have adapted the following code which I have found online
m.nn <- matchit(ba ~ age + sex + timesincediagnosis + TVS + tscore,
data = R, method= " nearest", ratio = 1)
summary(m.nn)
I am getting the following errors:
Error in summary(m.nn) : object 'm.nn' not found
Error in matchit(ba ~ age + sex + timesincediagnosis + TVS + tscore,
data = R, : nearestnot supported.
I would really appreciate any help with why I am getting the errors or how I can change my code.
Thank you!
Credit goes to #MrFlick for noticing this, but the problem is that " nearest" is not an acceptable value to be passed to method. What you want is "nearest" (without the leading space in the string). (Note that the default method is nearest neighbor matching, so you can omit the method argument entirely if this is what you want to do.)
The error print first (Error in summary(m.nn) : object 'm.nn' not found) occurs because R didn't create the m.nn object because of the other error.

coxph : "**Error in if (any(infs)) warning(paste("Loglik converged before variable ", : missing value where TRUE/FALSE needed"**

I am doing a Multi-touch Attribution problem using coxph() function. Its a large dataset with around 1 million data but currently I am running a subset of ~100000.
I have removed all the missing values from my data. I am getting an error
Error in if (any(infs)) warning(paste("Loglik converged before variable ",
:missing value where TRUE/FALSE needed
Here is the Cox Function :
SurvObj <- Surv(Final_Data$NormalizedStartTime,Final_Data$NormalizedEndTime,event = Final_Data$Converted)
model2 <- coxph(SurvObj ~ Clicks + RFR + Impressions + Other + `Site-ID` + `Creative-ID`, data = Final_Data1)
Thanks in Advance for the help :)
The Error and the Summary of Final_Data
The line above, "Loglik" and so on, is meant to give information about a dubious test, where loglik converges beforehandedly.
The full line, when produced correctly, should be something akin to the following:
"Loglik converged before variable 100; beta may be infinite."
And it is produced by the following code in the agreg.Rnw
https://r-forge.r-project.org/scm/viewvc.php/pkg/survival/noweb/agreg.Rnw?diff_format=c&sortdir=down&sortby=author&revision=11529&root=survival&view=markup
if (any(infs))
warning(paste("Loglik converged before variable ",
paste((1:nvar)[infs],collapse=","),
"; beta may be infinite. "))
From here we can see that the any() would expect the infs to be a number. If infs is NaN, the function will not work.
The inner part functions like this:
paste("Loglik converged before variable ",
paste((1:1)[NaN],collapse=","),
"; beta may be infinite. ")
[1] "Loglik converged before variable NA ; beta may be infinite. "
so this part of the function would run, if it could get to the inner part. But it does not, since evaluating
infs <- NaN
if (any(infs))
warning(paste("Loglik converged before variable ",
paste((1:nvar)[infs],collapse=","),
"; beta may be infinite. "))
Error in if (any(infs)) warning(paste("Loglik converged before variable ", :
missing value where TRUE/FALSE needed
the exact error you had. The infs variable is generated before, via infs <- abs(agfit$u %*% var). And the agfit is produced via .Call(Cagfit4.....), so the problem is in the underlying C code for the function.
For some of my data, both the agfit$u and agfit$imat are NaNs. The $u and $imat are generated from
u2 = SET_VECTOR_ELT(rlist, 1, allocVector(REALSXP, nvar));
u = REAL(u2);
and
PROTECT(imat2 = allocVector(REALSXP, nvar*nvar));
nprotect =1;
if (NAMED(covar2)>0) {
PROTECT(covar2 = duplicate(covar2));
nprotect++;
}
covar= dmatrix(REAL(covar2), nused, nvar);
imat = dmatrix(REAL(imat2), nvar, nvar);
respectively, in the agfit4 C code. I am not that good in C so I cannot say what the problem is in the C code area. It could be a bug or then the Cox function is not usable for your data or both. Nevertheless, something should be done to this, since I've seen others asking about this error too. But unfortunately I am not skilled enough to fix this, I only can point to the problem and holler "hey! somebody else pls take care of this" :-).
My possible solutions would be:
1) check if your data is usable with the Cox function at all (e.g. if you have 2000 cases of 0 and 2 cases of 1, the Cox function may not be suitable anyway, and the error is suggesting you find another way for the analysis :-) )
2) modify the code to do the any(infs) evaluation via removing the NAs, resulting in FALSE and skipping the error via the following:
if (any(infs, na.rm=T)) (could screw up the code, tho)
3) fix the C code so that the agfit4 does not produce NaNs in the output object. (only for the skilled, not me)
I had this problem too. Turns out one of my variables had infinite values in it. Problem solved once I replaced these values

Subsetting data breaks GLM

I have a GLM Logit regression that works correctly, but when I add a subset argument to the GLM command, I get the following error:
invalid type (list) for variable '(weights)'.
So, the following command works:
glm(formula = A ~ B + C,family = "binomial",data = Data)
But the following command yield the error:
glm(formula = A ~ B + C,family = "binomial",data = Data,subset(Data,D<10))
(I realize that it may be difficult to answer this without seeing my data, but any general help on what may be causing my problem would be greatly appreciated)
Try subset=D<10 instead (you don't need to specify Data again, it is implicitly used as the environment for the subset argument). Because you haven't named the argument, R is interpreting it as the weights argument (which is the next argument after data).

Resources