Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中划分不同回归系数时计算标准误差_R - Fatal编程技术网

在R中划分不同回归系数时计算标准误差

在R中划分不同回归系数时计算标准误差,r,R,考虑来自同一数据集mtcars的以下两个回归 #load the data data(mtcars) # Run the regression model1<-lm(mpg~cyl+gear+drat, data = mtcars) model2<-lm(wt~cyl+gear+drat, data = mtcars) summary(model1) summary(model2) #Calculate ratio of coefficients g<-model1$c

考虑来自同一数据集mtcars的以下两个回归

#load the data
data(mtcars)


# Run the regression
model1<-lm(mpg~cyl+gear+drat, data = mtcars)
model2<-lm(wt~cyl+gear+drat, data = mtcars)

summary(model1)
summary(model2)

#Calculate ratio of coefficients
g<-model1$coefficients[2] / model2$coefficients[2]

#calculate clustered standard errors
vcov<-cluster.vcov(model1, mtcars$vs)
coeftest(model1, vcov)
vcov<-cluster.vcov(model2, mtcars$vs)
coeftest(model2, vcov)
#加载数据
数据(mtcars)
#运行回归

模型1正如@MDEWITT所建议的,你可以在文章中使用delta方法,尽管我认为你可能需要以不同的方式估计模型-你可能需要一个单一的多元模型而不是两个独立的回归模型,因为你需要两个系数的协方差,除非估计关节模型,否则不存在:

library(msm)
data(mtcars)
# Run the regression
model<-lm(cbind(mpg, wt)~cyl+gear+drat, data = mtcars)
b <- c(coef(model))
v <- vcov(model)
## calcualte se
est <- b[2]/b[6]
se <- deltamethod(g = ~ x2/x6, b, vcov(model))
> est 
# [1] -8.160363
> se
# [1] 1.770336
库(msm)
数据(mtcars)
#运行回归

模型您可以使用delta方法来计算。看见
## write a function to calculate the statistic of interest
boot.fun <- function(data, inds){
  m <- lm(cbind(mpg, wt) ~ cyl + gear + drat, data=data[inds, ])
  # return the appropraite ratio
  coef(m)[2,1]/coef(m)[2,2]
}

library(boot)
## bootstrap the function
out <- boot(mtcars, statistic=boot.fun, R=5000)
## calculate confidence intervals
boot.ci(out, type=c("perc", "bca"))
# BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
# Based on 5000 bootstrap replicates
# 
# CALL : 
#   boot.ci(boot.out = out, type = c("perc", "bca"))
# 
# Intervals : 
#   Level     Percentile            BCa          
# 95%   (-12.993,  -5.152 )   (-12.330,  -4.838 )  
# Calculations and Intervals on Original Scale
## draw coefficients from implied sampling distributions
B <- MASS::mvrnorm(5000, c(coef(model)), vcov(model))
## calculate the ratio for each draw
rat <- B[,2]/B[,6]
## calculate the confidence itnerval
round(c(mean(rat), quantile(rat, c(.025,.975))), 3)
#           2.5%   97.5% 
# -8.559 -14.418  -5.554 
q1 <- est + qt(c(.025,.975), df=model$df.residual)*se
q2 <- boot.ci(out, type="perc")$percent[1,4:5]
q3 <- quantile(rat, c(.025,.975))
cis <- rbind(q1, q2, q3)
colnames(cis) <- c("lwr", "upr")
rownames(cis) <- c("Delta Method", "BS (non-parametric)", "BS (parametric)")
cis
#                           lwr       upr
# Delta Method        -11.78673 -4.533994
# BS (non-parametric) -12.99338 -5.151545
# BS (parametric)     -14.41812 -5.553773
par(mfrow=c(1,2))
hist(out$t[which(out$t > quantile(out$t, .005) & out$t < quantile(out$t, .995))], main="Non-parametric Bootstrap", xlab="ratio")
hist(rat[which(rat < quantile(rat, .995) & rat > quantile(rat, .005))], main="Parametric Boostratp", xlab="ratio")