r语言 - 查找特征的形状值



目前,我正在做一个项目来估计特征的形状值,因此我使用下面的代码一次随机选择几(2)列并找到形状值。然而,这种方法给了我一个难以识别的错误。希望有人能帮忙

data("Boston", package = "MASS")
dt<-cbind(Boston$lstat,Boston$rm,Boston$dis,Boston$indus)
Var1<-Boston[,sample(ncol(dt), size = 1), drop = FALSE]
Var2<-Boston[,sample(ncol(dt), size = 1), drop = FALSE]
Var3<-Boston$medv
modt<-cbind(Var1,Var2,Var3)
varmod<-lm(Var3~.,data=modt)
summary(varmod)

x_var<-c(colnames(modt[1]),colnames(modt[2]))
y_var<-"Var3"
x_train <- as.matrix(modt[-1:-6, x_var])
y_train <- as.matrix(modt[-1:-6, y_var])
x_test <- as.matrix(modt[1:6, x_var])
y_test <- as.matrix(modt[1:6, y_var])
explainer <- shapr(x_train,varmod )
p <- mean(y_train)
explan<-explain(
x_test,
explainer=explainer,
approach="empirical",
prediction_zero=p
)

错误:UseMethod("explain")错误:对c('matrix', 'double', 'numeric')类的对象没有适用的'explain'方法

您的代码在我的环境中运行没有错误。请检查您的sessionInfo()是否报告不同的软件包版本:

library(shapr)
data("Boston", package = "MASS")
dt<-cbind(Boston$lstat,Boston$rm,Boston$dis,Boston$indus)
Var1<-Boston[,sample(ncol(dt), size = 1), drop = FALSE]
Var2<-Boston[,sample(ncol(dt), size = 1), drop = FALSE]
Var3<-Boston$medv
modt<-cbind(Var1,Var2,Var3)
varmod<-lm(Var3~.,data=modt)
summary(varmod)
#> 
#> Call:
#> lm(formula = Var3 ~ ., data = modt)
#> 
#> Residuals:
#>     Min      1Q  Median      3Q     Max 
#> -16.540  -5.421  -1.878   2.575  30.134 
#> 
#> Coefficients:
#>             Estimate Std. Error t value Pr(>|t|)    
#> (Intercept) 23.61403    0.41862  56.409  < 2e-16 ***
#> crim        -0.40598    0.04339  -9.358  < 2e-16 ***
#> chas         5.57772    1.46926   3.796 0.000165 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 8.373 on 503 degrees of freedom
#> Multiple R-squared:  0.1744, Adjusted R-squared:  0.1712 
#> F-statistic: 53.14 on 2 and 503 DF,  p-value: < 2.2e-16

x_var<-c(colnames(modt[1]),colnames(modt[2]))
y_var<-"Var3"
x_train <- as.matrix(modt[-1:-6, x_var])
y_train <- as.matrix(modt[-1:-6, y_var])
x_test <- as.matrix(modt[1:6, x_var])
y_test <- as.matrix(modt[1:6, y_var])
explainer <- shapr(x_train,varmod )
p <- mean(y_train)
explan<-explain(
x_test,
explainer=explainer,
approach="empirical",
prediction_zero=p
)
explan
#> $dt
#>      none     crim       chas
#> 1: 22.446 1.559507 -0.3940416
#> 2: 22.446 1.550867 -0.3939230
#> 3: 22.446 1.550875 -0.3939228
#> 4: 22.446 1.548879 -0.3939895
#> 5: 22.446 1.534062 -0.3940640
#> 6: 22.446 1.549869 -0.3939564
#> 
#> $model
#> 
#> Call:
#> lm(formula = Var3 ~ ., data = modt)
#> 
#> Coefficients:
#> (Intercept)         crim         chas  
#>      23.614       -0.406        5.578  
#> 
#> 
#> $p
#>        1        2        3        4        5        6 
#> 23.61147 23.60294 23.60295 23.60089 23.58600 23.60191 
#> 
#> $x_test
#>         crim chas
#> [1,] 0.00632    0
#> [2,] 0.02731    0
#> [3,] 0.02729    0
#> [4,] 0.03237    0
#> [5,] 0.06905    0
#> [6,] 0.02985    0
#> 
#> attr(,"class")
#> [1] "shapr" "list"
sessionInfo()
#> R version 4.1.0 (2021-05-18)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 20.04.2 LTS
#> 
#> Matrix products: default
#> BLAS/LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.8.so
#> 
#> locale:
#>  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
#>  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=C             
#>  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
#>  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
#> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] shapr_0.2.0
#> 
#> loaded via a namespace (and not attached):
#>  [1] Rcpp_1.0.7        knitr_1.33        magrittr_2.0.1    rlang_0.4.11     
#>  [5] fastmap_1.1.0     fansi_0.5.0       stringr_1.4.0     styler_1.4.1     
#>  [9] highr_0.9         tools_4.1.0       data.table_1.14.0 xfun_0.24        
#> [13] utf8_1.2.1        withr_2.4.2       htmltools_0.5.2   ellipsis_0.3.2   
#> [17] yaml_2.2.1        digest_0.6.27     tibble_3.1.2      lifecycle_1.0.0  
#> [21] crayon_1.4.1      purrr_0.3.4       vctrs_0.3.8       fs_1.5.0         
#> [25] glue_1.4.2        evaluate_0.14     rmarkdown_2.9     reprex_2.0.1     
#> [29] stringi_1.6.2     compiler_4.1.0    pillar_1.6.1      backports_1.2.1  
#> [33] pkgconfig_2.0.3

由reprex包(v2.0.1)于2021-09-13创建

最新更新