简体   繁体   English

如何在lm中指定参数估计之间的关系?

[英]How can I specify a relationship between parameter estimates in lm?

Using lm, I would like to fit the model: y = b0 + b1*x1 + b2*x2 + b1*b2*x1*x2 使用lm,我想拟合模型: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? 我的问题是:如何指定交互作用的系数应等于主要作用系数的乘积?

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. 我已经看到将系数设置为特定值,可以使用offset()和I(),但是我不知道如何指定系数之间的关系。

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)

To fit Model 1: y = b0 + b1*x1 + b2*x2 + b3*x1*x2, I would use: 为了适合模型1:y = b0 + b1 * x1 + b2 * x2 + b3 * x1 * x2,我将使用:

summary(lm(y~ x1 + x2 + x1:x2))

But how do I fit Model 2: y = b0 + b1*x1 + b2*x2 + b1*b2*x1*x2? 但是如何拟合模型2:y = b0 + b1 * x1 + b2 * x2 + b1 * b2 * x1 * x2?

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). 在模型1中,我们估计了4个参数:b0(截距),b1(变量1的斜率),b2(变量2的斜率)和b3(变量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) 在模型2中,我们估计3个参数:b0(截距),b1(变量1的斜率和变量1和2之间相互作用的斜率的一部分)和b2(变量2的斜率和变量2的斜率的一部分)。变量1和2之间的交互)

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. 我要这样做的原因是,在调查模型2的x1和x2之间是否存在显着相互作用时,y = b0 + b1 * x1 + b2 * x2 + b1 * b2 * x1 * x2会更好。空模型比y = b0 + b1 * x1 + b2 * x2。

Many thanks! 非常感谢!

Marie 玛丽

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. 由于您施加在系数上的约束,因此您指定的模型不是线性模型,因此无法使用lm进行拟合。 You would need to use a non-linear regression, such as nls . 您将需要使用非线性回归,例如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

You can really see that the model is non-linear when you re-write it as 当您将模型重写为时,您真的可以看到模型是非线性的

> 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

Brian provides a way to fit the constrained model you specify but if you're interested in if the unconstrained model fits better than your constrained model you use the delta method to test that hypothesis. Brian提供了一种适合您指定的约束模型的方法,但是如果您对无约束模型是否比约束模型更合适感兴趣,可以使用delta方法来检验该假设。

# Let's make some fake data where the constrained model is true
n <- 100
b0 <- 2
b1 <- .2
b2 <- -1.3
b3 <- b1 * b2
sigma <- 1

x1 <- rnorm(n)
# make x1 and x2 correlated for giggles
x2 <- x1 + rnorm(n) 
# Generate data according to the model
y <- b0 + b1*x1 + b2*x2 + b3*x1*x2 + rnorm(n, 0, sigma)

# Fit full model y = b0 + b1*x1 + b2*x3 + b3*x1*x2 + error
o <- lm(y ~ x1 + x2 + x1:x2)

# If we want to do a hypothesis test of Ho: b3 = b1*b2
# this is the same as Ho: b3 - b1*b2 = 0
library(msm)
# Get estimate of the difference specified in the null
est <- unname(coef(o)["x1:x2"] - coef(o)["x1"] * coef(o)["x2"])
# Use the delta method to get a standard error for
# this difference
standerr <- deltamethod(~ x4 - x3*x2, coef(o), vcov(o))

# Calculate a test statistic.  We're relying on asymptotic
# arguments here so hopefully we have a decent sample size
z <- est/standerr
# Calculate p-value
pval <- 2 * pnorm(-abs(z))
pval

I explain what the delta method is used for and more on how to use it in R in this blog post . 这篇博客文章中,我将解释delta方法的用途,以及如何在R中使用它的更多信息

Expanding on Brian's answer you could alternatively do this by comparing the full model to the constrained model - however you have to use nls to fit the full model to be able to easily compare the models. 扩展Brian的答案,您可以选择通过将完整模型与受限模型进行比较来进行此操作-但是,您必须使用nls来拟合完整模型,才能轻松比较模型。

o2 <- nls(y ~ b0 + b1*x1 + b2*x2 + b1*b2*x1*x2, start=list(b0=0, b1=1, b2=1))
o3 <- nls(y ~ b0 + b1*x1 + b2*x2 + b3*x1*x2, start = list(b0 = 0, b1 = 1, b2 = 1, b3 = 1))
anova(o2, o3)

There's no way to do what you're asking for in lm and there's no reason for it to be able to do it. 无法完成lm您要的操作,也没有理由使其能够执行。 You run lm to get estimates of of your coefficients. 您运行lm以获取系数的估计值。 If you don't want to estimate the coefficient then don't include the predictor in the model. 如果您不想估计系数,则不要在模型中包括预测变量。 You can use coef to extract the coefficients you want and multiply them out afterwards. 您可以使用coef提取所需的系数,然后将它们相乘。

Note that leaving the interaction out is a different model and will produce a different b1 and b2. 请注意,忽略交互是一个不同的模型,并且将产生不同的b1和b2。 You could alternatively leave I(x1 * x2) in and not use the coefficient. 您也可以保留I(x1 * x2) ,而不使用系数。

As for why you want to do this, there's not good a priori justification that your constrained model actually fits better than the simple additive model. 至于为什么要这样做,没有先验理由证明约束模型实际上比简单加性模型更适合。 Having more free parameters necessarily means a model fits better but you haven't added that, you've added a constraint that, in the real world, could make it fit worse. 拥有更多自由参数必然意味着模型更适合,但您还没有添加,而是添加了一个约束,在现实世界中,约束可能会使模型变得更糟。 In that case would you consider it a better "baseline" for comparison to the model including the interaction? 在那种情况下,您认为与包括交互作用的模型相比,它是更好的“基准”吗?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM