Error for predict() with lmer - r

I'm using a leave-one-out method to evaluate how well a model with one datapoint excluded predicts that datapoint (rotating through all datapoints). The code below has successfully run on essentially the same data with a slightly different DV, so I'm stumped as to why I'm getting the error that I'm getting. Here's the relevant chunk of code:
dataPennTrim.lmer <- lmer(logDur.PENN~cNewNounDen*ContextCode+
Vowel.Contrasts+BlockCode+
(1|subject)+(0+ cNewNounDen +ContextCode|subject)+
(1|word)+(0+ContextCode|word),
data=pennTrim,
control = lmerControl(optimizer = "bobyqa"),REML=FALSE)
pennPred <- predict(dataPennTrim.lmer, newdata = dataFull2)
dataFull2 has the same columns as pennTrim, it just has more rows. Pretty standard use of the predict() function. I get this error:
Error in t(.Call(Csparse_dense_crossprod, y, x)) :
error in evaluating the argument 'x' in selecting a
method for function 't': Error: Cholmod error 'X and/or Y
have wrong dimensions' at file ../MatrixOps/cholmod_sdmult.c, line 90
Any thoughts about what might be causing this error? I can use essentially the same code with the same dataframes swapping out logDur.PENN for logDur.Manual (measurements from a different source) and the code gives no errors.

Related

tryCatch() function to save warning message AND error message in r

my previous question was:
I am simulating multilevel data and fitting it to various Multilevel models.
I want to make a function that if there is any error (such as "failed to converge" or "singular fit"), I want to save it.
For example, my model is
lmer(y~ x1 + x2 + (1|pid), data=sim_data).
and here are many conditions so various data will be fitted into this model.
How can I save the error or warning message as a whole in the dataframe or list?
(like, first dataset -> no error, second dataset -> converge error...etc)
and some people gave me good answer to it.
However, the answers to this question were about error message, but not warning message.
for( i in 1:10){
catch$error[i]<-tryCatch((y~ x1 + x2 + (1|pid), data=sim_data),
error=function(e) e$message,
warning=function(w) w$message)
}
with this code, can I extract warning and error message both?
and it seems like generating following error
Error in results$MLM0warning1[i] <- tryCatch(model_null(ind_sim_data), :
incompatible types (from S4 to character) in subassignment type fix

Calibration plot error - Non-Numeric argument to binary operator

I am unable to produce a calibration plot for my logistic classification model, and I am unsure of why I am getting this error / how to fix it.
For the purposes of reproducibility, here is a made up example:
library(tidyverse)
library(classifierplots)
test.df <- as_tibble(data_frame(y = factor(c(0,0,0,0,1,1,0,1,0,1)),pred = c(0.1,0.15,0.2,0.05,0.6,0.7,0.2,0.85,0.1,0.75)))
calibration_plot(test.y = test.df$y,pred.prob = test.df$pred)
When I run this, I get the following error:
Error in alpha * 255 : non-numeric argument to binary operator
Similarly, when I run the code with my actual model, I receive the same error. Any ideas?

error r: invalid subscript type "closure" in a simple regression

unfortunately i am a beginner in r. I d like to run a simple linear regression model in r with the comand lm, but every time i try the following error occurs:
Error in xj[i] : invalid subscript type 'closure'
The regression model ist just as follows:
REG1 <- lm(flowpercent~ret+tna+fundage+number_shr_cl,data = reg, na.omit)
#-flowpercent is a calculated variable:
reg$flowpercent <- reg$flow_dollar/lag(reg$tna, n=1)
#-fundage is also calculated:
reg$fundage <- as.numeric(difftime(ref_date,reg$InceptionDate, units = "days")/365.25)
ret, tna, number_shr_cl are variables from a database
hopefully some can help me to solve my problem.
Many thanks in advance.
Your third argument is na.omit. You probably saw someone writing something like na.action = na.omit. However, if you look up the help for lm by typing ?lm, you will see:
Usage:
lm(formula, data, subset, weights, na.action, ... # etc
which tells you that the third argument to lm is subset. So, you are passing the object called na.omit to the subset argument, which lm tries to use to subset your data. Unfortunately, na.omit is an R function (aka a "closure"). Not surprisingly, R does not know how to use this function to subset your data. Hence the error.

Why do I get "Error in as.character.default(<S4 object of class "glmerMod">)" when using tryCatch?

I am applying a linear model over a dataset, and using tryCatch to detect possible warnings, here's the script:
glm.m <- tryCatch(
glmer(y ~ gene + (1|x) + (1|z), data =a, family="binomial"),
warning= function(w){paste("warning",glmer(y ~ gene + (1|x) + (1|z), data =a, family="binomial"))})
I keep on getting this error message:
Error in as.character.default() : no
method for coercing this S4 class to a vector
which does disappear if I remove tryCatch and simply run the model. Unfortunately I need to use tryCatch to detect the rows which will give me warnings.
Did anyone already encounter the same error in similar conditions? I did see other threads with similar errors, but nothing was helpful for my case.

R segmented regression predict gives error: "subscript out of bounds"

I'm building a segmented regression model using R's Segmented package.
I was able to create the model but have trouble using the predict.segmented function. It always throws an error saying "subscript out of bounds"
This is the exact error message:
Error in newdata[[nameZ[i]]] : subscript out of bounds
Traceback just gives this:
1: predict.segmented(seg_model, xtest)
I created a simple case that gives the same error:
require(segmented)
x = c(1:90, 991:1000)
y = c((x[1:10]/2), (x[11:100]*2))
lm_model = lm(y~x)
seg_model = segmented(lm_model, seg.Z=~x, psi=list(x=NA),
control=seg.control(display=FALSE, K=1, random=TRUE))
xtest = c(1:1000)
predict.segmented(seg_model, xtest)
I am starting to think this could be a bug. I'm new to R and not sure how to debug this either. Any help is appreciated!
You are using predict.segemented incorrectly. Like nearly all the predict() functions, your newdata parameter should be a data.frame, not a vector. Also, it needs to have names that match the variables used in your regression. Try
predict.segmented(seg_model, data.frame(x=xtest))
instead. When using a function for the first time, be sure the read the help page (?predict.segmented) to know what the function expects for each of the parameters.

Resources