如何将公式传递给具有权重的lm对象?

问题描述

为什么公式一级以上时,无法将公式传递给具有权重的lm对象?

select max(case when rw_id = 100 then val end) as t1,max(case when rw_id = 1 then val end) as t2       
from tabl1.bg_s_stavka
group by gc_number

R版本4.0.2

解决方法

问题是lm试图在weights_i或编写公式的环境中找到dat。一种简单的解决方案是在dat中将权重创建为临时列。这样可以防止将变量写入全局环境。

form <- y~x
sapply( seq(x),function(i){
    dat$weight_i <- dnorm( (x[i]-x)/2)
    reg <- lm(form,data=dat,weights=weight_i)
})
#>               [,1]        [,2]        [,3]        [,4]        [,5]       
#> coefficients  Numeric,2   Numeric,2  
#> residuals     Numeric,100 Numeric,100
#> fitted.values Numeric,100
#> effects       Numeric,78  Numeric,79  Numeric,80  Numeric,81  Numeric,82 
#> weights       Numeric,100
#> rank          2           2           2           2           2          
#> assign        Integer,2   Integer,2  
#> qr            List,5      List,5     
#> df.residual   76          77          78          79          80         
#> xlevels       List,0      List,0     
#>               [,6]        [,7]        [,8]        [,9]        [,10]      
#> coefficients  Numeric,83  Numeric,84  Numeric,85  Numeric,86  Numeric,87 
#> weights       Numeric,100
#> rank          2           2           2           2           2          
#> 
#> ...etc

好消息是,这不会对dat或您的通话环境造成任何永久性更改:

head(dat)
#>           y x
#> 1 0.6125909 1
#> 2 1.5101739 2
#> 3 1.9893877 3
#> 4 4.6632718 4
#> 5 6.1132429 5
#> 6 5.2509379 6
,

您还可以创建一个包含所有参数的列表,并使用do.call()

form <- y~x
llls <- sapply( seq(x),function(i){
  l <- list(formula=form,data = dat,weights = weight_i <- dnorm( (x[i]-x)/2)
        )
  reg <- do.call(lm,l)
})