R的lm()中的分数频率权重

时间:2019-04-17 23:53:01

标签: r regression lm weighted

我了解到lmweights视为“分析”权重,这意味着观察值之间才相互加权(例如,lm将用weight = weight = 1)的2倍,并且该模型的整体N不受影响。另一方面,“频率”权重将使模型具有与数据中实际观测值不同的N

人们曾经问过R before中的频率权重,但据我所知,先前的问题与调查数据有关。我没有为此问题使用调查数据。

我想实现小于1的频率权重,这会使模型的N小于数据中的实际行数。例如,如果nrow(df) = 8,并且所有观察值的weight = 0.5,则模型N应该是4,标准误差应该反映出这种差异。据我所知,基R的lm的权重无法通过这种方式使用:

library(tidyverse)
library(broom)

df.unweighted <- tribble(
  ~x, ~y, ~w,
  0, 10, 1,
  0, 20, 1,
  1, 40, 1,
  1, 50, 1,
) %>%
  bind_rows(., .) # make twice as large

df.weighted <- df.unweighted %>%
  mutate(w = 0.5)

lm(data=df.unweighted, y~x, weights=w) %>%
  tidy
#> # A tibble: 2 x 5
#>   term        estimate std.error statistic  p.value
#>   <chr>          <dbl>     <dbl>     <dbl>    <dbl>
#> 1 (Intercept)      15.      2.89      5.20 0.00202 
#> 2 x                30       4.08      7.35 0.000325

lm(data=df.weighted, y~x, weights=w) %>%
  tidy
#> # A tibble: 2 x 5
#>   term        estimate std.error statistic  p.value
#>   <chr>          <dbl>     <dbl>     <dbl>    <dbl>
#> 1 (Intercept)     15.       2.89      5.20 0.00202 
#> 2 x               30.0      4.08      7.35 0.000325

# identical

我正在寻找的内容可以使用stataiweights中实现。注意模型N和标准错误:

library(RStata)
stata("reg y x [iweight=w]",
      data.in = df.weighted)
#> . reg y x [iweight=w]
#> 
#>       Source |       SS       df       MS              Number of obs =       4
#> -------------+------------------------------           F(  1,     2) =   18.00
#>        Model |         900     1         900           Prob > F      =  0.0513
#>     Residual |         100     2          50           R-squared     =  0.9000
#> -------------+------------------------------           Adj R-squared =  0.8500
#>        Total |        1000     3  333.333333           Root MSE      =  7.0711
#> 
#> ------------------------------------------------------------------------------
#>            y |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
#> -------------+----------------------------------------------------------------
#>            x |         30   7.071068     4.24   0.051    -.4243492    60.42435
#>        _cons |         15          5     3.00   0.095    -6.513264    36.51326
#> ------------------------------------------------------------------------------

在我的实际用法中,并非所有观察值都具有相同的权重。我在这里只是为了便于演示。

0 个答案:

没有答案
相关问题