如何在lm中指定参数估计之间的关系? [英] How can I specify a relationship between parameter estimates in lm?
问题描述
使用lm,我想拟合模型: y = b0 + b1 * x1 + b2 * x2 + b1 * b2 * x1 * x2
Using lm, I would like to fit the model: y = b0 + b1*x1 + b2*x2 + b1*b2*x1*x2
我的问题是: 如何指定交互作用的系数应等于主要作用系数的乘积?
My question is: How can I specify that the coefficient of the interaction should equal the multiplication of the coefficients the main effects?
我已经看到了将系数设置为特定值的方法,可以使用offset()和I(),但是我不知道如何指定系数之间的关系.
I've seen that to set the coefficient to a specific value you can use offset() and I() but I don't know how to specify a relationship between coefficient.
这是一个简单的模拟数据集:
Here is a simple simulated dataset:
n <- 50 # Sample size
x1 <- rnorm(n, 1:n, 0.5) # Independent variable 1
x2 <- rnorm(n, 1:n, 0.5) # Independent variable 2
b0 <- 1
b1 <- 0.5
b2 <- 0.2
y <- b0 + b1*x1 + b2*x2 + b1*b2*x1*x2 + rnorm(n,0,0.1)
要拟合模型1:y = b0 + b1 * x1 + b2 * x2 + b3 * x1 * x2,我会使用:
To fit Model 1: y = b0 + b1*x1 + b2*x2 + b3*x1*x2, I would use:
summary(lm(y~ x1 + x2 + x1:x2))
但是我如何拟合模型2:y = b0 + b1 * x1 + b2 * x2 + b1 * b2 * x1 * x2?
But how do I fit Model 2: y = b0 + b1*x1 + b2*x2 + b1*b2*x1*x2?
两个模型之间的主要区别之一是要估计的参数数量.在模型1中,我们估计4个参数:b0(截距),b1(变量1的斜率),b2(变量2的斜率)和b3(变量1和2之间相互作用的斜率).在模型2中,我们估计了3个参数:b0(截距),b1(变量1的斜率和变量1和2之间相互作用的斜率的一部分)和b2(变量2的斜率和变量2的斜率).变量1和2之间相互作用的斜率
One of the main differences between the two models is the number of parameters to estimate. In Model 1, we estimate 4 parameters: b0 (intercept), b1 (slope of var. 1), b2 (slope of var. 2), and b3 (slope for the interaction between vars. 1 & 2). In Model 2, we estimate 3 parameters: b0 (intercept), b1 (slope of var. 1 & part of slope of the interaction between vars. 1 & 2), and b2 (slope of var. 2 & part of slope of the interaction between vars. 1 & 2)
我要执行此操作的原因是,在调查x1和& amp; amp;之间是否存在明显的交互作用时, x2,模型2,y = b0 + b1 * x1 + b2 * x2 + b1 * b2 * x1 * x2,可能是比y = b0 + b1 * x1 + b2 * x2更好的空模型.
The reason why I want to do this is that when investigating whether there is a significant interaction between x1 & x2, model 2, y = b0 + b1*x1 + b2*x2 + b1*b2*x1*x2, can be a better null model than y = b0 + b1*x1 + b2*x2.
非常感谢!
玛丽
推荐答案
由于您在系数上施加了约束,因此指定的模型不是线性模型,因此不能使用lm
进行拟合.您将需要使用非线性回归,例如nls
.
Because of the constraint that you impose on the coefficients, the model you specify is not a linear model and so lm
can not be used to fit it. You would need to use a non-linear regression, such as nls
.
> summary(nls(y ~ b0 + b1*x1 + b2*x2 + b1*b2*x1*x2, start=list(b0=0, b1=1, b2=1)))
Formula: y ~ b0 + b1 * x1 + b2 * x2 + b1 * b2 * x1 * x2
Parameters:
Estimate Std. Error t value Pr(>|t|)
b0 0.987203 0.049713 19.86 <2e-16 ***
b1 0.494438 0.007803 63.37 <2e-16 ***
b2 0.202396 0.003359 60.25 <2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.1121 on 47 degrees of freedom
Number of iterations to convergence: 5
Achieved convergence tolerance: 2.545e-06
当您将模型重写为
> summary(nls(y ~ b0+(1+b1*x1)*(1+b2*x2)-1, start=list(b0=0, b1=1, b2=1)))
Formula: y ~ b0 + (1 + b1 * x1) * (1 + b2 * x2) - 1
Parameters:
Estimate Std. Error t value Pr(>|t|)
b0 0.987203 0.049713 19.86 <2e-16 ***
b1 0.494438 0.007803 63.37 <2e-16 ***
b2 0.202396 0.003359 60.25 <2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.1121 on 47 degrees of freedom
Number of iterations to convergence: 5
Achieved convergence tolerance: 2.25e-06
这篇关于如何在lm中指定参数估计之间的关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!