psychbruce / bruceR

📦 BRoadly Useful Convenient and Efficient R functions that BRing Users Concise and Elegant R data analyses.
https://psychbruce.github.io/bruceR/
GNU General Public License v3.0
167 stars 36 forks source link

An error occurs when using function HLM_summary() #12

Closed HPDell closed 2 years ago

HPDell commented 2 years ago

Platform: Windows x64
R Version: 4.1.1 (x64)
bruceR Version: latest (installed via install_github())


When I tried to generate tidy report of an object returned from function lmer(), the following error occurred:

HLM_summary(model.hlm3)

# Error in formula[[2]] : object of type 'symbol' is not subsettable
HPDell commented 2 years ago

But everything goes well when the function is called in this way:

HLM_summary(formula = model.hlm.formula, data = mydata)
psychbruce commented 2 years ago

Hello. Please provide all code relevant to this issue. It seems like something invalid in the R formula when building the model using lmer().

psychbruce commented 2 years ago

Please see this example:

library(bruceR)
library(lmerTest)

m1=lmer(Reaction ~ Days + (Days | Subject), data=sleepstudy)
HLM_summary(m1)  # OK

formula=Reaction ~ Days + (Days | Subject)
m2=lmer(formula, data=sleepstudy)
HLM_summary(m2)  # Error
# Error in formula[[2]] : object of type 'symbol' is not subsettable

HLM_summary(formula=formula, data=sleepstudy)  # OK

To use a variable like model.hlm.formula to define a formula and pass it to lmer() (e.g., lmer(model.hlm.formula, data=mydata)), please use the argument formula of HLM_summary(). By default, HLM_summary() extracts formula=model[["call"]][["formula"]] and its second element formula[[2]] is the outcome variable. If you use a variable rather than a formula in lmer(), it definitely extracts nothing.

psychbruce commented 2 years ago

The latest commit to GitHub has addressed this issue (now the function extracts the name of outcome variable from the data frame of the model rather than from its formula call). I close this issue.

HPDell commented 2 years ago

Thanks. I wrote my code in the way that is almost exactly shown in your example. However, once I found the possible cause, I've reformated my code to get over this problem. Sorry that I forgot to provide more information when opening this issue.

P.S. A brilliant package! I like it very much.