如何在R中约束混合效应模型中的参数?

发布于 2025-01-11 05:12:55 字数 2550 浏览 0 评论 0原文

我正在尝试使用受约束的参数来拟合混合效应模型,并且正在努力使其发挥作用。增加一点复杂性的是其中一项应该是多项式。

本质上我正在寻找的是类似下面的内容,其中 var 1 固定为某个值。

mod1 <- lmer(outcome ~ var1 + poly(var2,2) + (1 | Study), df)

似乎可以使用 lmer 和 Nelder-Mead 选项来完成,但我我可以完全思考如何让它发挥作用。

我也尝试过使用 lavaan 包,但我以前从未使用过它,并且在某个地方挂断了。这是一个例子...

library(lavaan)

reprex_df <- structure(list(outcome = c(0.54, 5.06, 15.35, 5.4, 5.3, 1.57, 
                                        2.11, 2.71, 9.09, 7.96, 28.8, 4.4, 3.38, 15.43, 4.05), var1 = c(0.55, 
                                                                                                        3.42, 2.24, 2.24, 3.44, 1.82, 1.82, 2.23, 5.41, 2.61, 6.94, 3.98, 
                                                                                                        2.23, 5.29, 3.28), var2 = c(111, 235, 60, 197, 369, 342.78, 240.99, 
                                                                                                                                    406.5, 264, 263.8, 76, 679, 338, 116, 683), study = c("Study 1", 
                                                                                                                                                                                   "Study 2",  "Study 2",  "Study 2", "Study 3", 
                                                                                                                                                                                   "Study 4", "Study 4", "Study 6", "Study 5", 
                                                                                                                                                                                   "Study 7", "Study 2", "Study 7", "Study 6", 
                                                                                                                                                                                   "Study 5", "Study 2")), row.names = c(NA, -15L), class = c("tbl_df", "tbl", "data.frame"))

我想我可以制作一个基本模型(没有多项式)

reprex_df

test.model <- '  outcome  ~ var1 + var2 + study'

test.model <- sem(test.model, 
                    data = reprex_df, cluster = "study")
coef(test.model)

但是当我尝试将 var1 限制为特定值时我收到错误

test.model.constr <- '  outcome    ~ var1 + var2 + study

var1 == 4.87
'

test.model.constr <- sem(test.model.constr, 
                  data = reprex_df, cluster = "study")

限制参数的任何帮助(使用 lmer 或 lavaan)和/或在 lavaan 中添加多项式项将非常感激。

I'm trying to fit a mixed effect model with a constrained parameter, and am struggling to make it work. Adding a small bit of complexity, is that one of the terms should be a polynomial.

Essentially what I'm looking for is something like the following, where var 1 is fixed at a certain value.

mod1 <- lmer(outcome ~ var1 + poly(var2,2) + (1 | Study), df)

It seems like it can be done using lmer with the Nelder-Mead option, but I can quite wrap my head around how to make it work.

I've also tried using the lavaan package, but I've never used it before and am getting hung up somewhere. Here is an example...

library(lavaan)

reprex_df <- structure(list(outcome = c(0.54, 5.06, 15.35, 5.4, 5.3, 1.57, 
                                        2.11, 2.71, 9.09, 7.96, 28.8, 4.4, 3.38, 15.43, 4.05), var1 = c(0.55, 
                                                                                                        3.42, 2.24, 2.24, 3.44, 1.82, 1.82, 2.23, 5.41, 2.61, 6.94, 3.98, 
                                                                                                        2.23, 5.29, 3.28), var2 = c(111, 235, 60, 197, 369, 342.78, 240.99, 
                                                                                                                                    406.5, 264, 263.8, 76, 679, 338, 116, 683), study = c("Study 1", 
                                                                                                                                                                                   "Study 2",  "Study 2",  "Study 2", "Study 3", 
                                                                                                                                                                                   "Study 4", "Study 4", "Study 6", "Study 5", 
                                                                                                                                                                                   "Study 7", "Study 2", "Study 7", "Study 6", 
                                                                                                                                                                                   "Study 5", "Study 2")), row.names = c(NA, -15L), class = c("tbl_df", "tbl", "data.frame"))

I think I can make a basic model (without the polynomial)

reprex_df

test.model <- '  outcome  ~ var1 + var2 + study'

test.model <- sem(test.model, 
                    data = reprex_df, cluster = "study")
coef(test.model)

But when I try and constrain var1 to a specific value I'm getting an error

test.model.constr <- '  outcome    ~ var1 + var2 + study

var1 == 4.87
'

test.model.constr <- sem(test.model.constr, 
                  data = reprex_df, cluster = "study")

Any help in constraining the parameter (using either lmer or lavaan) and/or adding a polynomial term in lavaan would be very much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

○闲身 2025-01-18 05:12:56

这很棘手,因为lmer“分析”了固定效应参数,即它们没有明确地作为非线性优化步骤的一部分进行拟合。

假设 var1 是数值/连续的,我们想设置一个系数 b,怎么样

mod1 <- lmer(outcome ~ 1 + offset(b*var1) + poly(var2,2) + (1 | Study), df)

?这会将项 b*var1 直接添加到模型中。

glmmTMB 包有一个 map 参数,允许用户将任何参数显式固定为特定值(或限制多个参数具有公共值)。

It's tricky because lmer "profiles out" the fixed-effect parameters, i.e. they're not explicitly fitted as part of the nonlinear optimization step.

Assuming var1 is numeric/continuous and we want to set a coefficient of b, how about

mod1 <- lmer(outcome ~ 1 + offset(b*var1) + poly(var2,2) + (1 | Study), df)

? This adds the term b*var1 directly to the model.

The glmmTMB package has a map argument that allows the user to fix any of the parameters explicitly to a particular value (or to constrain several parameters to have a common value).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文