0

glmer を使用して、R の一般化線形混合モデルから R^2 を計算する関数を作成しようとしています。どういうわけか、関数を使用しようとするとすぐにエラーが発生します:

Error in checkFormulaData(formula, data, checkLHS = control$check.formula.LHS ==  :  bad 'data': object 'input_data' not found

関数なしで同じコードを使用すると、すべて問題ありません。関数内で r.squaredGLMM に一般的な問題があるかどうかをテストするために、線形混合モデルの関数も作成しましたが、これは期待どおりに機能しています。

再現可能な例を次に示します。

library(lme4)
library(MuMIn)


#Generate some sample data
x <- rnorm(100)
y1 <- sample(c(0,1), 100, replace = TRUE)
y2 <- rnorm(100)
subject <- rep(1:10, 10)

df <- data.frame(x,y1,y2,subject)

#Calculate glmer and get the R^2
mod1_outside_function <- glmer(y1 ~ x + (1|subject), data = df,family="binomial")

#Works just fine
R2 <- r.squaredGLMM(mod1_outside_function)
print(R2)

#Create a function to get the R2
R2_glmer <- function(input_data)
{
  glmer_inside_function <- glmer(y1 ~ x + (1|subject), data = input_data,family="binomial")

  R2 <- r.squaredGLMM(glmer_inside_function)

  print(R2)

}

#I get the error running this:
R2_glmer(input_data = df)


#The same function works with lmer:
R2_lmer <- function(input_data)
{

  lmer_inside_function <- lmer(y2 ~ x + (1|subject), data = input_data)

  R2 <- r.squaredGLMM(lmer_inside_function)

  print(R2)

}

R2_lmer(input_data = df)
4

1 に答える 1