简体   繁体   English

我怎样才能让 lme4::glmer 运行

[英]How can I get a lme4::glmer to run

I have this csv file我有这个 csv 文件

Name名称 ID ID Year Tourist游客 Feed.bout反馈
Anubis阿努比斯 1 1个 2014 2014 TRUE真的 0:01:17 0:01:17
Athena雅典娜 2 2个 2014 2014 FALSE错误的 0:01:53 0:01:53

I am trying to run a general linear mixed model with lme4 but I am struggling to get it to run.我正在尝试使用 lme4 运行一般线性混合 model 但我正在努力让它运行。

My code is我的代码是

library(tidyverse)
library(lme4)
library(lattice)

Feedbout=read.csv("Av_Fe_Bout3.csv")
head(Feedbout)

Feedbout$Tourist=as.logical(Feedbout$Tourist)
Av_Fe_Bout3 <- glmer(Feed.bout ~ Year + Tourist + Year:Tourist + (1|ID), family = poisson, data = Feedbout)
summary(Av_Fe_Bout3)

and I keep getting我不断得到

Error in mkRespMod(fr, family = family) : 
  response must be numeric or factor
In addition: Warning message:
Some predictor variables are on very different scales: consider rescaling

I have tried it without a family and also with binomial.我在没有家庭的情况下尝试过,也尝试过二项式。

If I change anything else the Feed.bout changes to NA s and I don't know how to change it back.如果我更改任何其他内容, Feed.bout将更改为NA并且我不知道如何将其更改回来。

I think the problem is with Feed.bout but I'm not sure what to do.我认为问题出在Feed.bout但我不确定该怎么做。

I have also tried to run我也试过跑步

read.zoo(file = "Av_Fe_Bout3.csv", format = "%H:%M:%S")

to try and change the format of Feed.bout however it just says尝试更改Feed.bout的格式,但它只是说

Error in read.csv.zoo(file = "Av_Fe_Bout3.csv", format = "%H:%M:%S") : 
  could not find function "read.csv.zoo"

Its hard to emulate what you've done, but here I will provide a worked example using a binary dataset to show you what I imagine may be the issue.很难模仿您所做的事情,但在这里我将提供一个使用二进制数据集的工作示例,以向您展示我认为可能是问题所在。 First, I will load the requisite packages.首先,我将加载必要的包。 Then I will set a random seed so you can try what I have here (and hopefully on your own data).然后我会设置一个随机种子,这样你就可以尝试我在这里的东西(希望在你自己的数据上)。

#### Load Libraries and Random Seed ####
library(lmerTest)
library(tidyverse)
set.seed(123)

From here, there is no need to convert the outcome into a factor or numeric value for this specific dataset , but here I do so just to show you how to do it.从这里开始,无需将结果转换为该特定数据集的因子或数值,但我这样做只是为了向您展示如何操作。 I also create a simulated value called fake to add to lme4 's Verbagg data, as there are no other numeric predictors in this dataset.我还创建了一个名为fake的模拟值以添加到lme4的 Verbagg 数据中,因为此数据集中没有其他数字预测变量。 As there are 7585 observations, I simulate as many below.由于有 7585 个观测值,我在下面模拟了同样多的观测值。

#### Convert Variable to Factor ####
VerbAgg <- VerbAgg %>% 
  mutate(r2 = factor(r2),
         fake = rnorm(n=7584))

Then I fit the model as always.然后我一如既往地适合model。 In this case, I put the scale function around the two numeric predictors in the interaction term.The nAGQ argument is only run here to speed up the run, as this data is normally slow to fit.在这种情况下,我将scale function 放在交互项中的两个数字预测变量周围nAGQ参数仅在此处运行以加快运行速度,因为此数据通常拟合起来很慢。

#### Fit Data to Model ####
fit <- glmer(r2 
             ~ scale(Anger)*scale(fake)
             + (1|id) 
             + (1|item),
        family = binomial,
        data = VerbAgg,
        nAGQ=0L)

Finally from there you just summarize the model with summary(fit) , which gives you this:最后从那里你只需用summary(fit)总结 model ,它给你这个:

Generalized linear mixed model fit by maximum likelihood (Adaptive
  Gauss-Hermite Quadrature, nAGQ = 0) [glmerMod]
 Family: binomial  ( logit )
Formula: r2 ~ scale(Anger) * scale(fake) + (1 | id) + (1 | item)
   Data: VerbAgg

     AIC      BIC   logLik deviance df.resid 
  8203.1   8244.7  -4095.5   8191.1     7578 

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-5.8761 -0.6234 -0.1787  0.6240 14.0824 

Random effects:
 Groups Name        Variance Std.Dev.
 id     (Intercept) 1.815    1.347   
 item   (Intercept) 1.275    1.129   
Number of obs: 7584, groups:  id, 316; item, 24

Fixed effects:
                          Estimate Std. Error z value Pr(>|z|)   
(Intercept)              -0.155244   0.244350  -0.635  0.52521   
scale(Anger)              0.261487   0.081500   3.208  0.00133 **
scale(fake)               0.001112   0.028515   0.039  0.96890   
scale(Anger):scale(fake)  0.026135   0.029000   0.901  0.36747   
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Correlation of Fixed Effects:
            (Intr) scl(A) scl(f)
scale(Angr)  0.000              
scale(fake)  0.002 -0.003       
scl(Ang):() -0.001  0.011  0.001

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在lme4 :: glmer模型的新数据上使用predict()? - How do I use predict() on new data for lme4::glmer model? lme4 :: glmer.nb函数产生“$ family系列中的错误:$ S4未定义此类的运算符”,具体取决于我运行模型的顺序 - lme4::glmer.nb function produces “Error in family$family : $ operator not defined for this S4 class” depending on the order I run models 解释lme4中的glmer置信区间 - Interpreting glmer confidence interval in lme4 excel 的等效项 如果 r 中的 lme4 glmer 出现错误 - An equivalent for the excel If error for lme4 glmer in r 如何在嵌套的logit glmer模型(使用lme4)中获取每个系数的p值? - How to obtain the p-values for each coefficient in a nested logit glmer model (using lme4)? lme4 ::: profile.merMod()是否应该与glmer模型一起使用? - Is lme4:::profile.merMod() supposed to work with glmer models? lme4 :: glmer与Stata的melogit命令 - lme4::glmer vs. Stata's melogit command Lme4:循环 glmer 时的随机效应错误 - Lme4: error on random effect when looping glmer lme4 :: glmer中的错误消息:“&#39;什么&#39;必须是字符串或函数” - Error message in lme4::glmer: “ 'what' must be a character string or a function” glmer混合模型lme4更新之间不一致 - glmer mixed models inconsistent between lme4 updates
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM