简体   繁体   English

R 中的 Function 计算线性回归的异方差稳健置信区间

[英]Function in R that computes heteroskedasticity-robust confidence intervals for a linear regression

Good afternoon, I have a question regarding my function down below.下午好,我对下面的 function 有疑问。 The task is to develop a function in R that computes heteroskecasticity-robust confidence intervals for the results of the betas of a linear regression.任务是在 R 中开发一个 function,用于计算线性回归的 beta 结果的异方差稳健置信区间。

As I have tried to do so, my function does not return any output.正如我试图这样做的那样,我的 function 没有返回任何 output。 The console simply doesn´t do anything after trying to get some results from it.控制台在尝试从中获得一些结果后根本不做任何事情。 I really argue why especially if I compute it manually by the last two rows of my code it works out all fine.我真的很争论为什么特别是如果我通过代码的最后两行手动计算它,它工作得很好。 Even though you dont have the necessary data.frames, perhaps you can take a look at my code and tell me what is wrong about it or propose an alternative way to solve my problem:)即使您没有必要的 data.frames,也许您可以查看我的代码并告诉我它有什么问题或提出解决我的问题的替代方法:)

For clarity: the original numerous values (using all 200 data points each) of the coefficients are c(463.2121, 139.5762), the stdHC are c(74.705054, 5.548689) as given by the lm model and for HC-robust standard errors I use the package sandwich.为了清楚起见:系数的原始众多值(每个使用所有 200 个数据点)是 c(463.2121, 139.5762),stdHC 是 c(74.705054, 5.548689),由 lm model 给出,对于 HC-robust 标准错误我使用package 三明治。

my_CI <- function (mod, level = 0.95)
{
  `%>%` <- magrittr::`%>%`
  standard_deviation <- stderrorHC
  Margin_Error <- abs(qnorm((1-0.95)/2))*standard_deviation 
  df_out <- data.frame(stderrorHC, mod,Margin_Error=Margin_Error,
                       'CI lower limit'=(mod - Margin_Error),
                       'CI Upper limit'=(mod + Margin_Error)) %>%
    return(df_out)
}

my_CI(mod, level = 0.95) #retrieving does not return any results for me

Definitions:
women <- read.table("women.txt")
men <- read.table("men.txt")
converged <- merge(women, men, all = TRUE)
level <- c(0.95, 0.975)
modell <- lm(formula = loan ~ education, data = converged)
mod <- modell$coefficients
vcov <- vcovHC(modell, type = "HC1")
stderrorHC <- sqrt(diag(vcov))

mod - abs(qnorm((1-level[1])/2))*stderrorHC 
mod + abs(qnorm((1-level[1])/2))*stderrorHC

Addition: Here is some data from the original dataset.补充:这是原始数据集中的一些数据。 I included just ten data points so we would need to construct the confidence interval upon the t-distributon in this case.我只包括了十个数据点,因此在这种情况下,我们需要在 t 分布上构建置信区间。

dataMenEductaion <- c(12, 17, 16, 11, 20, 20 , 11, 19, 15, 16)
dataMenLoan <- c(2404.72, 3075.313, 2769.543, 2009.295, 3105.121, 4269.216
                   2213.730, 4025.136, 2605.191, 2760.186)
dataWomenEducation <- c(12, 14, 16, 19 , 12, 19, 20, 17, 16, 10)
dataWomenLoan <- c(1920.667, 2278.255, 2296.804, 2977.048, 1915.740, 3557.991, 
                   3336.683, 2923.040, 2628.351, 1918.218)

I believe that the following provides you with the desired output.相信下面为您提供了想要的output。

# install.packages('sandwich')
library(sandwich) # contains vcovHC()

# data
df <- data.frame(education = c(12, 17, 16, 11, 20, 20, 11, 19, 15, 16,
                              12, 14, 16, 19 , 12, 19, 20, 17, 16, 10),
                loan = c(2404.72, 3075.313, 2769.543, 2009.295, 3105.121, 4269.216,
                         2213.730, 4025.136, 2605.191, 2760.186,
                         1920.667, 2278.255, 2296.804, 2977.048, 1915.740, 3557.991, 
                         3336.683, 2923.040, 2628.351, 1918.218))
df$sex <- factor(gl(2, nrow(df)/2, labels = c('males', 'females')))

# linear model
fit <- lm(loan ~ education + sex, data = df)
coefs <- fit$coefficients
vcov <- vcovHC(fit, type = "HC1")
stderrorHC <- sqrt(diag(vcov))

# function to compute robust SEs
my_CIs <- function (coefs, level = 0.95) {
  standard_deviation <- stderrorHC
  Margin_Error <- abs( qnorm( (1-level)/ 2) ) * standard_deviation 
  df_out <- data.frame(stderrorHC, coefs, Margin_Error = Margin_Error,
                       'CI lower limit' = (coefs - Margin_Error),
                       'CI Upper limit' = (coefs + Margin_Error))
  return(df_out)
}

Output Output

> my_CIs(coefs = coefs)
stderrorHC     coefs Margin_Error CI.lower.limit CI.Upper.limit
(Intercept)  295.86900  160.3716    579.89259      -419.5210      740.26416
education     23.64313  176.0111     46.33968       129.6714      222.35073
sexfemales   132.07169 -313.2632    258.85576      -572.1189      -54.40743

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

相关问题 如何在R中使用线性回归和置信区间? - How to work with linear regression and confidence intervals in R? R 用稳健的线性回归模型 (rlm) 绘制置信区间线 - R plot confidence interval lines with a robust linear regression model (rlm) 如何解决 R 中多元线性回归中的异方差 - How to resolve heteroskedasticity in Multiple Linear Regression in R 如何引导线性回归并估计 R 中的置信区间? - How to bootstrap a linear regression and estimate confidence intervals in R? R:具有置信度和预测间隔的三相线性模型的鲁棒非线性最小二乘拟合 - R : Robust nonlinear least squares fitting of three-phase linear model with confidence & prediction intervals 贝叶斯线性回归模型预测的置信区间 - Confidence intervals on predictions for a Bayesian linear regression model 导出包括置信区间的线性回归结果 - Exporting Linear Regression Results Including Confidence Intervals 使用rq函数计算R中分位数回归的95%置信区间 - Calculating 95% confidence intervals in quantile regression in R using rq function "获得稳健回归系数的置信区间 (MASS::rlm)" - Getting confidence intervals for robust regression coefficient (MASS::rlm) 提取 R 中的稳健标准误差后如何获得置信区间? - How to get confidence intervals after extracting robust standard errors in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM