Error message in lme4::glmer: " 'what' must be a character string or a function" - r

I am running a multi-level model. I use the following commands with validatedRS6 as the outcome, random as the predictor and clustno as the random effects variable.
new<-as.data.frame(read.delim("BABEX.dat", header=TRUE))
install.packages("lme4")
library(lme4)
model1<- glmer(validatedRS6 ~ random + (1|clustno), data=new, family=binomial("logit"), nAGQ = 1L)
However, I get the following error
Error in do.call(new, c(list(Class = "glmResp", family = family), ll[setdiff(names(ll), :
'what' must be a character string or a function
I have absolutely no idea what has gone wrong and have searched the internet. I am sorry but I cannot provide the data as it is from an intervention which has yet to be published.

(expanded from comment).
Congratulations, you found a bug in lme4! This is fixed now:
https://github.com/lme4/lme4/commit/9c12f002821f9567d5454e2ce3b78076dabffb54
It is caused by having a variable called new in the global environment (deep in the guts of the code, lme4 uses do.call(new,...) and finds your variable new rather than the built-in function new).
You can install a patched version from Github using devtools::install_github() (but you'll need compilation tools etc.). Alternately, there is a very simple workaround -- just call your variable anything other than new (you can't just copy it, i.e. new2 <- new -- you also have to make sure the old version is removed (rm("new"))).

Related

truthTable in Rstudio Error: incorrect outcome specification

I was trying to do a QCA analysis of sufficeny in RStudio 4.1.3. using truthTable command. By running the code i get Error: wrong outcome specification. The outcome is written correctly. When I do an analysis of necessity with superSubset code everything works fine. Everything is coded numeric but it does not work with bivariate callibration either.
> ttCO21 <- truthTable( data = mydata, outcome = "CO21",
+ conditions = "GENDG1, FDI1, GDP1",
+ sort.by="incl, n", show.cases = TRUE, complete = TRUE)
**Error: Incorrect outcome specification.**
I had the same issue today and the solution was to delete any unnecessary columns (or create a new dataframe). I left only the calibrated conditions and outcome, because when I found this error, the raw data was in the same df, even if was not using it in the truthTable() command.
Because of the 3 hours i wasted on this problem i'll share my situation and solution. I had the exact same problem. creating a new df did not help, nor did removing unnecessary colums (wouldn't make sense anyway). Completely reinstalling en reloading the QCA package did work for me. Hope this helps someone.
you can try "comma" instead of "dot" (in “decimal") when you import file

Check for Null Values in R tis

I rarely use R, but need for a helper function that somebody has given me. I have the following line:
league_model_19 <- gam(SHOT_MADE_FLAG ~ ti(LOC_X) + ti(LOC_Y) + ti(LOC_X, LOC_Y), data = shots)
But, when I run it, the following error message is displayed:
Error in ti.default(LOC_X) :
'tif' and 'freq' cannot both be NULL if 'x' is not a ti
I saw online somebody write code along the lines of:
if(is.null(tif)){
if(is.null(freq))
stop("'tif' and 'freq' cannot both be NULL if 'x' is not a ti")
else tif <- freq2tif(freq)
}
But I don't know how to integrate this into my current line, or if there is a more efficient way to do this. Does anybody know how to get rid of this error message so I can proceed with my code? Let me know if more details are needed.
Edit: Added minimal dataframe to reproduce error (player_id and player_name aren't used here, just left in there). This is the data that is in 'shots' as called in the line of code I shared:
PLAYER_ID,PLAYER_NAME,LOC_X,LOC_Y,SHOT_MADE_FLAG
1627759,Jaylen Brown,-20,19,0
1627759,Jaylen Brown,-29,168,1
1627759,Jaylen Brown,7,30,1
1627759,Jaylen Brown,9,6,0
1627759,Jaylen Brown,-176,186,0
Sounds like you either don't have the mgcv package loaded, or you have the tis package loaded after mgcv and the former is masking the latter.
If you didn't load mgcv, add library('mgcv') to your script before fitting the gam().
If you have mgcv loaded but also loaded the tis package, both packages provide a ti() function and it seems the tis one is being found first on the search path. Your best option is to use the fully qualified name for the ti() so that you inform R as to which ti() you want to use.
Try:
league_model_19 <- gam(SHOT_MADE_FLAG ~ mgcv::ti(LOC_X) +
mgcv::ti(LOC_Y) +
mgcv::ti(LOC_X, LOC_Y),
data = shots)
You could also change the script to load mgcv after tis but this might cause later problems when you use the ti() from the tis package; in which case you could use tis:ti() in those places.

How to fix lmer error: "Error in as(value, fieldClass, strict=FALSE) :"?

I'm getting a strange error when I run an lmer function in r.
I've tried changing the variable types (all of them are numeric or factor) and removing the NA before analysis, but nothing seems to work.
model_1 <- lmer(Q14 ~ gender * time + (1|OMID), data=data)
summary(model_1)
Specifically, my error message reads:
Error in as(value, fieldClass, strict = FALSE) :
internal problem in as(): “labelled” is(object, "numeric") is TRUE, but the metadata asserts that the 'is' relation is FALSE
Not sure why this is happening, but I can't seem to find any answers for it. Any help would be appreciated.
Thanks!
I think lmer has a problem with 'labelled' data. If you un-label the predictors it should work fine.
I had the same error: the code for the lme-formula worked perfectly fine and one day I encountered that error. The solution in my case was simply to restart the R session, reload the data - in my case from SPSS via library("haven")::read.sps and after that load library("lme4") and execute the lme-formula.
So, if the formula worked before without any error, maybe just clean the project environment and re-run the most crucial code without any additional packages loaded. Maybe it's just some "cross-contamination" between packages or an unwanted effect of any package on the dataframe.

R - Matchit - Propensity Score Matching - Discard function not working

I am using the MatchIt package on the LaLonde data-set and the discard argument is generating two types of errors. (The code works if I do not use the discard argument). In both cases, it is not clear how to resolve the problems....
The first issue is when I try discard = "hull.control"
m.opt1 <- matchit(treat ~ inc.re74 + inc.re75 + education + nonwhite +
age + nodegree, data = cps_controls, method = "optimal", ratio=1,
discard="hull.control")
This error message is produced....
Loading required namespace: WhatIf
Preprocessing data ...
Performing convex hull test ...
Error in mclapply(1:m, in_ch, mc.cores = mc.cores) :
'mc.cores' > 1 is not supported on Windows
The second issue is when I try discard = "control"
Error in d[i, ] <- abs(d1[i] - d0) :
number of items to replace is not a multiple of replacement length
Is there a way to address either of these? Thanks!!
Your issue seems to be kinda bug in MatchIt package as noted on SO here and here. I've submitted a ticket on GitHub.
Regarding the discard = "hull.control" issue:
Download the source code of MatchIt from here and edit discard.R. Add to the calls of WhatIf::whatif the argument mc.cores = 1. This should hard-code the number of cores used to 1 and thus eliminate the issue.
Uninstall the MatchIt package and build the new one by opening command line and type R CMD build C:\path\to\MatchIt-master. This should create a .tar.gz file. In R Studio, click on Tools -> Install packages... and select the local package.
You may need to restart R Studio if the library was loaded previously.
Enjoy.

Error with src() command in R

Yesterday I posted this question on Stats Exchange and based on the response I got, I decided to do some analysis using R's src() function. It's part of the "sensitivity" package.
I installed the package with no trouble, and then tried the following command:
sens <- src(seminars, REV, rank=TRUE, nboot=100)
sens is a new variable to store the results of the test
seminars is a data frame that I imported from a CSV file using the read.csv() command
REV is the name of a variable/column in seminars and my desired response variable
When I ran the command, I got the following error:
Error in data.frame(Y = y, X) : object 'REV' not found
Any thoughts?
From the documentation of src
y: a vector containing the responses corresponding to the design
of experiments (model output variables).
The input needs to be a vector (apparently) and you're attempting to pass in a name (and not even quoting the name at that). Since REV isn't defined (I'm guessing due to the error message) in the global environment it doesn't know what to do.
From reading the documentation it sounds like what you want to do is pass sensitivity[,-which(colnames(sensitivity) == "REV")] (just the design matrix - you don't want to include the responses) in as x and sensitivity[,"REV"] in as y.
This error is linked to the fact that the data.frame X=seminars include factors with 0 value, which produce an error while constructing the regression coefficient. You can first remove them as they don't contribute to the variance of the output.

Resources