所以我试图复制我在《计量经济学原理》中看到的一个统计函数,作者是 Hill、Griffiths 和 Lim。我要复制的功能在 stata 中是这样的;
lincom _cons + b_1 * [arbitrary value] - c
这是针对原假设 H0:B0 + B1*X = C
我可以在没有常数的情况下测试假设,但我想在测试参数的线性组合时添加常数。我浏览了包文档,glht()
但它只有一个示例,他们从中取出了常量。我复制了这个例子,保持不变,但是当你有一个矩阵 K 和一个常数时,我不确定如何测试线性组合。作为参考,这里是他们的例子;
### multiple linear model, swiss data
lmod <- lm(Fertility ~ ., data = swiss)
### test of H_0: all regression coefficients are zero
### (ignore intercept)
### define coefficients of linear function directly
K <- diag(length(coef(lmod)))[-1,]
rownames(K) <- names(coef(lmod))[-1]
K
### set up general linear hypothesis
glht(lmod, linfct = K)
我不太擅长创建假数据集,但这是我的尝试。
library(multcomp)
test.data = data.frame(test.y = seq(200,20000,1000),
test.x = seq(10,1000,10))
test.data$test.y = sort(test.data$test.y + rnorm(100, mean = 10000, sd = 100)) -
rnorm(100, mean = 5733, sd = 77)
test.lm = lm(test.y ~ test.x, data = test.data)
# to view the name of the coefficients
coef(test.lm)
# this produces an error. How can I add this intercept?
glht(test.lm,
linfct = c("(Intercept) + test.x = 20"))
根据文档,似乎有两种方法可以解决这个问题。我可以使用函数 diag() 来构造一个矩阵,然后我可以在linfct =
参数中使用它,或者我可以使用一个字符串。这种方法的问题是,我不太清楚如何使用 diag() 方法,同时还包括常数(等式的右侧);在字符串方法的情况下,我不确定如何添加拦截。
任何和所有的帮助将不胜感激。
这是我正在使用的数据。这最初是在一个 .dta 文件中,所以我为可怕的格式道歉。根据我上面提到的书,这是 food.dta 文件。
structure(list(food_exp = structure(c(115.22, 135.98, 119.34,
114.96, 187.05, 243.92, 267.43, 238.71, 295.94, 317.78, 216,
240.35, 386.57, 261.53, 249.34, 309.87, 345.89, 165.54, 196.98,
395.26, 406.34, 171.92, 303.23, 377.04, 194.35, 213.48, 293.87,
259.61, 323.71, 275.02, 109.71, 359.19, 201.51, 460.36, 447.76,
482.55, 438.29, 587.66, 257.95, 375.73), label = "household food expenditure per week", format.stata = "%10.0g"),
income = structure(c(3.69, 4.39, 4.75, 6.03, 12.47, 12.98,
14.2, 14.76, 15.32, 16.39, 17.35, 17.77, 17.93, 18.43, 18.55,
18.8, 18.81, 19.04, 19.22, 19.93, 20.13, 20.33, 20.37, 20.43,
21.45, 22.52, 22.55, 22.86, 24.2, 24.39, 24.42, 25.2, 25.5,
26.61, 26.7, 27.14, 27.16, 28.62, 29.4, 33.4), label = "weekly household income", format.stata = "%10.0g")), .Names = c("food_exp","income"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -40L))