[英]'Factors are not allowed' error when running bam() in mgcv() - R
I have a dataset as follows:我有一个数据集如下:
str(templates)
tibble [2,179 x 8] (S3: grouped_df/tbl_df/tbl/data.frame)
$ Speaker : Factor w/ 5 levels "Alex","Lily",..: 1 1 1 1 1 1 1 1 1 1 ...
$ session_ordinal: num [1:2179] 1 1 1 2 2 2 3 3 3 4 ...
$ structure : Factor w/ 32 levels "C","CCV","CCVC",..: 5 20 26 5 6 5 5 6 20 4 ...
$ structurePC : num [1:2179] 0.55 0.15 0.1 0.3636 0.0303 ...
$ structure.ord : Ord.factor w/ 32 levels "C"<"CCV"<"CCVC"<..: 5 20 26 5 6 5 5 6 20 4 ...
- attr(*, "groups")= tibble [220 x 4] (S3: tbl_df/tbl/data.frame)
..$ Speaker : chr [1:220] "Alex" "Alex" "Alex" "Alex" ...
..$ session_ordinal: num [1:220] 1 2 2 3 4 4 5 5 6 6 ...
..$ .rows :List of 220
I'm attempting to run a GAMM model on the data using bam()
in the mgcv
package.我正在尝试使用 mgcv package 中的
bam()
对数据运行mgcv
model。 I can run a simple model successfully:我可以成功运行一个简单的 model:
templates.gam.simple <- bam(structurePC ~
s(session_ordinal, k = 18) +
s(session_ordinal, Speaker, bs = "fs", m = 1, k = 5),
data=subset(templates, structurePC >= .1 & session_ordinal < 19), method="ML")
But as soon as I add additional factors (which I need to do in order to run my analyses) I start to get error messages.但是,一旦我添加了其他因素(我需要这样做才能运行我的分析),我就会开始收到错误消息。 If I add another smooth (
s(Speaker, k = 5)
) to the model, as in:如果我向 model 添加另一个平滑(
s(Speaker, k = 5)
),如下所示:
templates.gam.1 <- bam(structurePC ~
s(session_ordinal, k = 18) +
s(Speaker, k = 5) +
s(session_ordinal, Speaker, bs = "fs", m = 1, k = 5),
data=subset(templates, structurePC >= .1 & session_ordinal < 19), method="ML")
I get the error message:我收到错误消息:
Error in smooth.construct.tp.smooth.spec(object, dk$data, dk$knots) :
NA/NaN/Inf in foreign function call (arg 1)
In addition: Warning messages:
1: In mean.default(xx) : argument is not numeric or logical: returning NA
2: In Ops.factor(xx, shift[i]) : ‘-’ not meaningful for factors
If I try to add an interaction to the initial model ( ti(session_ordinal, Speaker, k = c(18, 5)) +
):如果我尝试向初始 model (
ti(session_ordinal, Speaker, k = c(18, 5)) +
)添加交互:
templates.gam.2 <- bam(structurePC ~
s(session_ordinal, k = 18) +
ti(session_ordinal, Speaker, k = c(18, 5)) +
s(session_ordinal, Speaker, bs = "fs", m = 1, k = 5),
data=subset(templates, structurePC >= .1 & session_ordinal < 19), method="ML")
I get:我得到:
Error in quantile.default(xu, seq(0, 1, length = nk)) :
factors are not allowed
You can't make a standard spline from a factor variable.您不能从因子变量制作标准样条曲线。 When you use the
"fs"
basis, you pass a continuous variable ( x
say) and a factor variable ( f
say):当您使用
"fs"
基础时,您传递一个连续变量( x
说)和一个因子变量( f
说):
s(x, f, bs = 'fs')
What this is doing is setting up splines in x
for each level of f
where the splines share the same smoothness penalty.这样做是在
x
中为每个f
水平设置样条线,其中样条线共享相同的平滑度损失。 These terms also include a random intercept for group means of f
.这些项还包括
f
的组均值的随机截距。
This is a special basis construction.这是一个特殊的基础结构。
If you want to include random intercepts for other factor variables, then you need to use another special basis type, the random effect or "re"
basis:如果要包括其他因子变量的随机截距,则需要使用另一种特殊的基类型,即随机效应或
"re"
基:
s(Speaker, bs = 're')
I'm not sure what you mean by including the ti()
term;我不确定包含
ti()
术语是什么意思; the "fs"
smooth is already an interaction between session_ordinal
and Speaker
. "fs"
平滑已经是session_ordinal
和Speaker
之间的交互。 Looking at session_ordinal
suggests this might not be a factor but you may have coded it via integers 1
, 2
,..., n
?查看
session_ordinal
表明这可能不是一个因素,但您可能已经通过整数1
、 2
、...、 n
对其进行了编码? If you explain what model you want (what effects you want) I can suggest solutions...如果你解释你想要什么 model (你想要什么效果)我可以建议解决方案......
...but in general you can include a factor in a tensor product smooth if you tell mgcv to use a random effect basis for the factor marginal term: ...但一般来说,如果您告诉mgcv对因子边际项使用随机效应基础,您可以在张量积中包含一个因子:
te(x, f, bs = c('cr', 're'))
where cr
means cubic regression spline and is the default basis for tensor products.其中
cr
表示三次回归样条曲线,是张量积的默认基础。 You can do this with t2()
and ti()
too.你也可以用
t2()
和ti()
来做到这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.