[英]How can I get a lme4::glmer to run
I have this csv file我有这个 csv 文件
Name![]() |
ID ![]() |
Year![]() |
Tourist![]() |
Feed.bout![]() |
---|---|---|---|---|
Anubis![]() |
1 ![]() |
2014 ![]() |
TRUE![]() |
0:01:17 ![]() |
Athena![]() |
2 ![]() |
2014 ![]() |
FALSE![]() |
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.