[英]Standard error and confidence interval for nonlinear function of least squares regression coefficients
我在R中運行OLS回歸,從中得到幾個系數。 這是代碼的一部分:
Attacks <- Treat.Terr.Dataset$Attacks[2:30]
Attackslag <- Treat.Terr.Dataset$Attacks[1:29]
TreatmentEffect <- Treat.Terr.Dataset$TreatmentEffect[2:30]
TreatmentEffectlag <- Treat.Terr.Dataset$TreatmentEffect[1:29]
olsreg <- lm(TreatmentEffect ~ TreatmentEffectlag + Attacks + Attackslag)
coeffs<-olsreg$coefficients
然后我需要計算:( (Attacks + Attackslag) / (1 - TreatmentEffectlag)
。 問題是我可以通過使用(coeffs[3] + coeffs[4]) / (1 - coeffs[2])
在R上做到這一點,但是結果是沒有任何p值或置信區間的固定數字,就像計算器會告訴我。
有沒有人知道我是否可以使用任何函數來計算這個置信區間?
編者注
如果目標數量是回歸系數的線性函數,那么問題就會減少到可能進行精確推理的一般線性假設檢驗。
## variance-covariance of relevant coefficients
V <- vcov(olsreg)[2:4, 2:4]
## point estimate (mean) of relevant coefficients
mu <- coef(olsreg)[2:4]
## From theory of OLS, coefficients are normally distributed: `N(mu, V)`
## We now draw 2000 samples from this multivariate distribution
beta <- MASS::mvrnorm(n = 2000, mu, V)
## With those 2000 samples, you can get 2000 samples for your target quantity
z <- (beta[, 2] + beta[, 3]) / (1 - beta[, 1])
## You can get Monte Carlo standard error, and Monte Carlo Confidence Interval
mean(z)
sd(z)
quantile(z, prob = c(0.025, 0.975))
## You can of course increase sample size from 2000 to 5000
這是一個使用'car'包中的delta方法的自包含示例:
# Simulate data
dat <- data.frame(Attacks = rnorm(30), Trt=rnorm(30))
dat <- transform(dat, AttacksLag = lag(Attacks), TrtLag = lag(Trt))
dat <- dat[2:30,]
# Fit linear model
m1 <- lm(Trt ~ TrtLag + Attacks + AttacksLag, data=dat)
# Use delta method
require("car")
del1 <- deltaMethod(m1, "(Attacks + AttacksLag) / (1 - TrtLag)")
# Simple Wald-type conf int
del1$Est + c(-1,1) * del1$SE * qt(1-.1/2, nrow(dat)-length(coef(m1)))
# [1] -0.2921529 0.6723991
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.