视图中的错误:无法将 class ‘"infl"’ 强制转换为 data.frame - 问题出在 `infl` class

Error in View : cannot coerce class ‘"infl"’ to a data.frame - Issue is with `infl` class

我正在尝试使用控制台上方查看器中的 influence.measure() 功能查看诊断集合,因为对我来说它看起来“更干净”。回归函数没什么特别的,就是一个基本的多元线性回归。

total_labour_hrs_lm = lm(data = Grocery_Retailer, formula = Total_Labour_hrs ~ Cases_Shipped + Labour_Hrs_Cost + Holiday)
> dput(head(Grocery_Retailer[, 1:4], 10))
structure(list(Total_Labour_hrs = c(4264, 4496, 4317, 4292, 4945, 
4325, 4110, 4111, 4161, 4560), Cases_Shipped = c(305657, 328476, 
317164, 366745, 265518, 301995, 269334, 267631, 296350, 277223
), Labour_Hrs_Cost = c(7.17, 6.2, 4.61, 7.02, 8.61, 6.88, 7.23, 
6.27, 6.49, 6.37), Holiday = c(0, 0, 0, 0, 1, 0, 0, 0, 0, 0)), row.names = c("data.data.data.data.1", 
"data.data.data.data.2", "data.data.data.data.3", "data.data.data.data.4", 
"data.data.data.data.5", "data.data.data.data.6", "data.data.data.data.7", 
"data.data.data.data.8", "data.data.data.data.9", "data.data.data.data.10"
), class = "data.frame")

完成所有这些后,我调用 influence.measure():

View(as.data.frame(influence.measures(model = total_labour_hrs_lm)))
Error in View : cannot coerce class ‘"infl"’ to a data.frame

这是我得到的错误。我正在做一些阅读以尝试了解正在发生的事情,我推测它与无法将 influence.measure() table 中的最后一列强制转换为结构有关可以在 View() 函数中使用。当然必须有办法解决这个问题,因为最后一列的所有用途都是 * 来识别有影响的案例。

对于编程来说还是比较新的,所以 类、结构等的想法还没有被正式理解,并且基于我在 R 中所做的所有工作和实践中学到的知识.

您可以操纵模型结果和 infl 对象,将数据转换为您在 运行 influence.measures(model = total_labour_hrs_lm) 时看到的相同格式。您可以只将列表中的第一项变成数据框,然后找到有影响力的数据行和 mutate 包含该信息的新列(即 *)。

library(tidyverse)
library(tibble)

inflm <- influence.measures(total_labour_hrs_lm)

inflm.df <-
  as.data.frame(inflm[["infmat"]]) %>%
  tibble::remove_rownames() %>%
  dplyr::mutate(inf = ifelse(row_number() %in% unname(which(
    apply(inflm$is.inf, 1, any)
  )), "*", ""))

输出

         dfb.1_      dfb.Cs_S     dfb.L_H_    dfb.Hldy        dffit      cov.r       cook.d       hat inf
1   0.002676016 -0.0005907955 -0.004099162  0.00348584 -0.006017538 2.61954809 1.086293e-05 0.2085001    
2  -0.132020690  0.3163760133 -0.118513731  0.08050579  0.518084429 1.19096264 6.629460e-02 0.2000735    
3  -1.011752656 -0.2260721643  1.803437294 -1.06556827 -1.978217429 4.97708055 9.797699e-01 0.7978985   *
4   0.858588355 -0.8998589831 -0.357487343  0.06500172 -1.035351317 4.73921419 2.939023e-01 0.6947583   *
5   0.000000000  0.0000000000  0.000000000  0.00000000          NaN        NaN          NaN 1.0000000    
6  -0.027980403 -0.0014046128  0.059268845 -0.06382214  0.124813877 2.25009310 4.588414e-03 0.1437767    
7  -0.085471708  0.3210337973 -0.278119353  0.34105089 -0.529878642 2.16749365 7.637825e-02 0.3532302    
8  -0.454518871  0.4661922675  0.131276469  0.13744951 -0.611770744 1.44932953 9.444163e-02 0.2838286    
9  -0.062332350  0.0610196593 -0.002635099  0.07859391 -0.266734659 1.56401553 1.928013e-02 0.1173206    
10  0.974167713 -1.0201812141 -0.218184444 -0.41667266  1.544679713 0.03659811 2.467009e-01 0.2006134   *