您可以通过将模型指定为结构方程模型并添加约束在 R 中使用 Lavaan 来执行此操作。我不确定这是否是个好主意,但可以做到。
#load library and generate some data
library(lavaan)
d <- as.data.frame(matrix(rnorm(1:3000), ncol=3, dimnames=list(NULL, c("y", "x1", "x2"))))
使用 GLM 运行它:
> summary(glm(y ~ x1 + x2, data=d))
Call:
glm(formula = y ~ x1 + x2, data = d)
Deviance Residuals:
Min 1Q Median 3Q Max
-3.6385 -0.5899 -0.0224 0.6024 3.0131
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.01855 0.03021 -0.614 0.539
x1 0.01208 0.03049 0.396 0.692
x2 -0.03676 0.03021 -1.217 0.224
(Dispersion parameter for gaussian family taken to be 0.912437)
Null deviance: 911.2 on 999 degrees of freedom
Residual deviance: 909.7 on 997 degrees of freedom
AIC: 2751.2
然后用 lavaan 运行相同的模型,检查等价性:
> model1.syntax <- '
+ y ~ x1 + x2
+ '
> summary(sem(model1.syntax, data=d))
lavaan (0.5-14) converged normally after 1 iterations
Number of observations 1000
Estimator ML
Minimum Function Test Statistic 0.000
Degrees of freedom 0
P-value (Chi-square) 1.000
Parameter estimates:
Information Expected
Standard Errors Standard
Estimate Std.err Z-value P(>|z|)
Regressions:
y ~
x1 0.012 0.030 0.397 0.691
x2 -0.037 0.030 -1.219 0.223
Variances:
y 0.910 0.041
然后在 lavaan 中,通过命名参数并添加约束部分来添加约束:
> model2.syntax <- '
+ y ~ b1 * x1 + b2 * x2
+ '
>
> model2.constraints <-
+ '
+ b1 > 0
+ b2 > 0
+ '
>
> summary(sem(model=model2.syntax, constraints=model2.constraints, data=d))
lavaan (0.5-14) converged normally after 1 iterations
Number of observations 1000
Estimator ML
Minimum Function Test Statistic 1.484
Degrees of freedom 0
P-value (Chi-square) 0.000
Parameter estimates:
Information Observed
Standard Errors Standard
Estimate Std.err Z-value P(>|z|)
Regressions:
y ~
x1 (b1) 0.012 NA
x2 (b2) 0.000 NA
Variances:
y 0.911 0.041
Constraints: Slack (>=0)
b1 - 0 0.012
b2 - 0 0.000
b2 参数不是负数,而是固定为零。
请注意,您没有得到任何标准错误 - 如果您想要它们,您必须引导。(这在 lavaan 手册中有描述)。