mkshaw / r2mlm

r2mlm: R Package for Calculating R-Squared in Multilevel Models
27 stars 1 forks source link

Error in `$<-.data.frame`(`*tmp*`... #29

Open jweir48 opened 4 years ago

jweir48 commented 4 years ago

I have run a random intercepts model successfully: m1 <- lmer(SBP~1+Segment+(1 |Subject),data=Mido_1, REML=FALSE), however when I try to run r2mlm I get this error

r2mlm(m1) Error in $<-.data.frame(*tmp*, ".fitted", value = c(1 = 92.7182482102599, : replacement has 280 rows, data has 288

mkshaw commented 4 years ago

I think missing data causes this -- are there 8 NA's somewhere in your data? If so, what happens if you run the following:

Mido_1_na <- na.omit(Mido_1)
m1 <- lmer(SBP ~ 1 + Segment + (1 | Subject), data=Mido_1_na, REML=FALSE)
r2mlm(m1)

Here, I'm using na.omit() to listwise delete the missing values from your dataframe, storing that cleaned dataset in Mido_1_na, and then re-running your model using Mido_1_na as the data. Let me know how that goes, if it runs successfully or if another (or the same) error pops up.

I'll note that lme4 already does listwise deletion, which is why you can successfully run your model, but r2mlm currently doesn't which is why this error is happening. I'm in the process of incorporating listwise deletion to handle this error.

jweir48 commented 4 years ago

Aha, thanks. I’ll give that a try. Much appreciated.

From: Mairead notifications@github.com Sent: Wednesday, October 28, 2020 2:47 PM To: mkshaw/r2mlm r2mlm@noreply.github.com Cc: Weir, Joseph joseph.weir@ku.edu; Author author@noreply.github.com Subject: Re: [mkshaw/r2mlm] Error in $<-.data.frame(*tmp*... (#29)

I think missing data causes this -- are there 8 NA's somewhere in your data? If so, what happens if you run the following:

Mido_1_na <- na.omit(Mido_1)

m1 <- lmer(SBP ~ 1 + Segment + (1 | Subject), data=Mido_1_na, REML=FALSE)

r2mlm(m1)

Here, I'm using na.omit() to listwise delete the missing values from your dataframe, storing that cleaned dataset in Mido_1_na, and then re-running your model using Mido_1_na as the data. Let me know how that goes, if it runs successfully or if another (or the same) error pops up.

I'll note that lme4 already does listwise deletion, which is why you can successfully run your model, but r2mlm currently doesn't which is why this error is happening. I'm in the process of incorporating listwise deletion to handle this error.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fmkshaw%2Fr2mlm%2Fissues%2F29%23issuecomment-718168244&data=04%7C01%7Cjoseph.weir%40ku.edu%7C0ca36fc7ea73484bb47a08d87b7a2f2f%7C3c176536afe643f5b96636feabbe3c1a%7C0%7C1%7C637395111991810536%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=AKfYO3UQcRKa75bisKM4vQUl79bWEMUC7b3AFtMnRoE%3D&reserved=0, or unsubscribehttps://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FARO5MRDET2SBJVXFZO2XB7TSNBYJ3ANCNFSM4S2MAWSA&data=04%7C01%7Cjoseph.weir%40ku.edu%7C0ca36fc7ea73484bb47a08d87b7a2f2f%7C3c176536afe643f5b96636feabbe3c1a%7C0%7C1%7C637395111991820531%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=Z7dYkDgey28i%2BRM%2Fc9Qt2bhPqd3LLQ1DTLnhhOCjMJw%3D&reserved=0.

mkshaw commented 4 years ago

Let me know how it goes!

Christy-Y-Wong commented 4 years ago

I have similar issue as well and I am doing intention-to-treat analysis so I can't take the listwise deletion approach. I hope to hear update from this thread soon. Thanks a lot!

mkshaw commented 4 years ago

@Christy-Y-Wong How are you running your lme4 or nlme code? I believe they already do listwise deletion as a default.

Christy-Y-Wong commented 4 years ago

I ran my model with lme4 and it included all the available observations in the model. All of my study participants took the measures at baseline but not all did so at follow-ups. I ran the lmer model directly but with REML set to be TRUE. Could I not delete any of my observations and go ahead with the R-squared measures?

jweir48 commented 4 years ago

Well, I seem to have a new error:

Error in r2mlm_lmer(model) : Your data must be numeric. Only the cluster variable can be a factor.

Here is the lme4 call:

m1_na <- lmer(SBP~1+Segment+(1 |Subject),data=Mido_1_na, REML=FALSE)

so it doesn’t seem to like that Segment is a categorical variable?

From: Mairead notifications@github.com Sent: Wednesday, October 28, 2020 5:22 PM To: mkshaw/r2mlm r2mlm@noreply.github.com Cc: Weir, Joseph joseph.weir@ku.edu; Author author@noreply.github.com Subject: Re: [mkshaw/r2mlm] Error in $<-.data.frame(*tmp*... (#29)

Let me know how it goes!

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fmkshaw%2Fr2mlm%2Fissues%2F29%23issuecomment-718242454&data=04%7C01%7Cjoseph.weir%40ku.edu%7C54b73d1bd25f4f7390c508d87b8feb78%7C3c176536afe643f5b96636feabbe3c1a%7C0%7C0%7C637395205355279136%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=O6iw%2BEjf5LgOfR6DGc0TDMxyBcQzdYzUk3SmRFov%2BQA%3D&reserved=0, or unsubscribehttps://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FARO5MRBWUGXJQU557X2HVF3SNCKRLANCNFSM4S2MAWSA&data=04%7C01%7Cjoseph.weir%40ku.edu%7C54b73d1bd25f4f7390c508d87b8feb78%7C3c176536afe643f5b96636feabbe3c1a%7C0%7C0%7C637395205355291230%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=3n4N8J%2F2DuVhH69Cbf33eEM0ZIWj9o4hgO8K854xzeQ%3D&reserved=0.

mkshaw commented 4 years ago

@jweir48 That's correct; segment must be numeric.

mkshaw commented 4 years ago

@Christy-Y-Wong I believe lme4 already does listwise deletion behind the scenes. See this explanation on ResearchGate and this one on StackOverflow.

bhui commented 3 years ago

hi there, thanks for creating this package, I believe it will be very useful to researchers. but I also encountered the same problem.

Here are my codes: m1<-lmer(pos~ beep+(beep|username),REML=F, data=dataset, na.action=na.omit) summary(m1) ### I could have the results r2mlm(m1) ###no luck, below is the error message

"Error in $<-.data.frame(*tmp*, ".fitted", value = c(1 = 1.36402264157347, : replacement has 27528 rows, data has 28182"

I also tried your suggestion to remove the missing data:

dataset_na <- na.omit(dataset) m2<-lmer(pos~ beep+(beep|username),REML=F, data=dataset_na) summary(m2) ### but this time, I could not have the results, the error message is below:

"Error in lme4::lFormula(formula = pos ~ beep + (beep | : 0 (non-NA) cases"

any ideas?

mkshaw commented 3 years ago

@bhui I haven't seen that error before. Are you able to send me some reproducible data and code?