2 Heteroskedasticity

本文通过模拟数据集展示了当误差项的方差随解释变量变化时,即存在异方差性的情况下,普通最小二乘法的局限性。并通过加权最小二乘法提供了一种解决方案,该方法能够有效地减少参数估计的标准误差。

Suppose the noise variance is itself variable.

Figure 2: Scatter-plot of n = 150 data points from the above model. (Here X isGaussian with mean 0 and variance 9.) Grey: True regression line. Dashed: ordinaryleast squares regression line.


 n=150
      x=rnorm(n,0,3)
      y = 3-2*x + rnorm(n,0,sapply(x,function(x){1+0.5*x^2}))
      
      # Plot the data
      plot(x,y)
      # Plot the true regression line
      abline(a=3,b=-2,col="grey")
      # Fit by ordinary least squares
      fit.ols = lm(y~x)
      # Plot that line
      abline(fit.ols,lty="dashed")

      





par(mfrow=c(1,2))
plot(x,residuals(fit.ols))
plot(x,(residuals(fit.ols))^2)

par(mfrow=c(1,1))




Figure 3: Residuals (left) and squared residuals (right) of the ordinary least squares
regression as a function of x. Note the much greater range of the residuals at large
absolute values of x than towards the center; this changing dispersion is a sign of

heteroskedasticity.



# Generate more random samples from the same model and the same x values,
      # but different y values
      # Inputs: number of samples to generate
      # Presumes: x exists and is defined outside this function
      # Outputs: errors in linear regression estimates
      ols.heterosked.example = function(n) {
        y = 3-2*x + rnorm(n,0,sapply(x,function(x){1+0.5*x^2}))
        fit.ols = lm(y~x)
        # Return the errors
        return(fit.ols$coefficients - c(3,-2))
      }
      # Calculate average-case errors in linear regression estimates (SD of
      # slope and intercept)
      # Inputs: number of samples per replication, number of replications (defaults
      # to 10,000)
      # Calls: ols.heterosked.example
      # Outputs: standard deviation of intercept and slope
      ols.heterosked.error.stats = function(n,m=10000) {
        ols.errors.raw = t(replicate(m,ols.heterosked.example(n)))
        # transpose gives us a matrix with named columns
        intercept.sd = sd(ols.errors.raw[,"(Intercept)"])
        slope.sd = sd(ols.errors.raw[,"x"])
        return(list(intercept.sd=intercept.sd,slope.sd=slope.sd))
      }
      
      

      $intercept.sd
[1] 0.6180515

$intercept.sd

[1] 0.6180515



2.1 Weighted Least Squares as a Solution to Heteroskedasticity


   # Plot the data
      plot(x,y)
      # Plot the true regression line
      abline(a=3,b=-2,col="grey")
      # Fit by ordinary least squares
      fit.ols = lm(y~x)
      # Plot that line
      abline(fit.ols,lty="dashed")
      fit.wls = lm(y~x, weights=1/(1+0.5*x^2))
      abline(fit.wls,lty="dotted")
      

      


Figure 6: Figure 2, plus the weighted least squares regression line (dotted)





### As previous two functions, but with weighted regression
# Generate random sample from model (with fixed x), fit by weighted least
# squares
# Inputs: number of samples
# Presumes: x fixed outside function
# Outputs: errors in parameter estimates
wls.heterosked.example = function(n) {
y = 3-2*x + rnorm(n,0,sapply(x,function(x){1+0.5*x^2}))
fit.wls = lm(y~x,weights=1/(1+0.5*x^2))
# Return the errors
return(fit.wls$coefficients - c(3,-2))
}
# Calculate standard errors in parameter estiamtes over many replications
# Inputs: number of samples per replication, number of replications (defaults
# to 10,000)
# Calls: wls.heterosked.example
# Outputs: standard deviation of estimated intercept and slope
wls.heterosked.error.stats = function(n,m=10000) {
wls.errors.raw = t(replicate(m,wls.heterosked.example(n)))
# transpose gives us a matrix with named columns
intercept.sd = sd(wls.errors.raw[,"(Intercept)"])
slope.sd = sd(wls.errors.raw[,"x"])
return(list(intercept.sd=intercept.sd,slope.sd=slope.sd))

}



2.2 Some Explanations for Weighted Least Squares





Stata 是一个广泛使用的统计分析软件,其内置了丰富的命令用于数据处理和统计分析。`avar2` 并不是 Stata 官方命令的一部分,而是一个用户编写的命令,通常用于估计方差分析中的异方差稳健标准误(heteroskedasticity-robust standard errors)[^1]。这个命令在经济学、社会学等领域的实证研究中非常有用,尤其是在回归模型中存在异方差性的情况下。 ### 用途 `avar2` 命令的主要用途是提供一种方式来计算线性回归模型中的异方差稳健标准误。当模型的误差项具有不同的方差时,传统的OLS标准误可能会产生误导性的推断结果,而使用 `avar2` 可以得到更加准确的标准误估计,从而提高假设检验的有效性。 ### 安装 由于 `avar2` 是一个用户编写的命令,因此在首次使用之前需要先安装。可以通过以下命令进行安装: ```stata ssc install avar2 ``` ### 使用方法 假设我们有一个简单的线性回归模型 `y = x1 + x2 + u`,其中 `y` 是因变量,`x1` 和 `x2` 是自变量,`u` 是误差项。如果我们怀疑模型中存在异方差性,可以使用 `avar2` 来估计模型参数及其标准误: ```stata * 加载示例数据集 sysuse auto, clear * 使用 avar2 命令估计模型 avar2 price mpg weight ``` 在这个例子中,`price` 是因变量,`mpg` 和 `weight` 是自变量。执行上述命令后,`avar2` 将输出包括异方差稳健标准误在内的回归结果。 ### 注意事项 - 在使用 `avar2` 命令之前,确保已经正确安装了该命令。 - `avar2` 命令仅适用于线性回归模型。 - 如果你的模型中包含了固定效应或其他复杂结构,可能需要寻找其他方法来处理异方差性。 ### 示例 为了更直观地展示 `avar2` 的使用方法,下面给出一个具体的例子: ```stata * 创建模拟数据 clear set obs 100 gen id = _n gen x1 = rnormal() gen x2 = rnormal() gen y = 1 + 2*x1 + 3*x2 + rnormal() * 使用 avar2 命令估计模型 avar2 y x1 x2 ``` 在这个例子中,我们首先创建了一个包含100个观测值的数据集,其中 `x1` 和 `x2` 是自变量,`y` 是因变量。然后使用 `avar2` 命令估计了模型参数及其标准误。 通过使用 `avar2`,研究人员可以在面对异方差性问题时,获得更为可靠的标准误估计,进而做出更准确的统计推断[^1]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值