执行 Hosmer-Lemeshow 测试的问题

问题描述 投票:0回答:1

请帮忙!

我正在尝试执行 HL 测试以评估我的模型的拟合优度,但我不断收到上述错误!

我安装了这些包: 库(jtools) 图书馆(略读) 图书馆(epiR) 图书馆(有效值) 图书馆(epimisc) 图书馆(DescTools) 图书馆(车) 图书馆(readxl) 库(摘要工具) 图书馆(生存) 图书馆(ggplot2) 图书馆(幸存者) 图书馆(预测ABEL)

这是我的代码:

# Final model
    l_final <- glm(pro$treatment ~ pro$age,family = binomial(link="logit"),data = pro, x=TRUE)
    summ(l_final)
  
# Assessing the fit of the model
    
    #predict proabailities
    pro$l_final_pred <- predict(l_final, type = "response")

    # Hosmer-Lemeshow test
    HosmerLemeshowTest(pro$l_final_pred, pro$treatment, X = l_final_pred$x)   

这是输出:

> # Assessing the fit of the model
>     pro$l_final_pred <- predict(l_final, type = "response")
Error:
! Assigned data `predict(l_final, type = "response")` must be compatible with existing data.
✖ Existing data has 866 rows.
✖ Assigned data has 864 rows.
ℹ Only vectors of size 1 are recycled.
Backtrace:
  1. base::`$<-`(`*tmp*`, l_final_pred, value = `<dbl>`)
 12. tibble (local) `<fn>`(`<vctrs___>`)
>     # Hosmer-Lemeshow test
>     HosmerLemeshowTest(pro$l_final_pred, pro$treatment, X = l_final_pred$x)   
Error in cut.default(fit, breaks = brks, include.lowest = TRUE) : 
  'x' must be numeric
In addition: Warning message:
Unknown or uninitialised column: `l_final_pred`.

谢谢!

我知道它与数据集的长度有关(治疗中有两个缺失 = 864,与年龄 - 866 相比)但无法解决它

r logistic-regression
1个回答
0
投票

尝试检查数据

  1. NULL/NA是否存在(使用

https://www.statology.org/r-select-rows-with-na/

删除 data.frame 中所有或部分 NA(缺失值)的行

如何删除 R 中具有 NULL 值的行 )?

(如果是这样,请理解它存在的原因,并在必要时删除这些行)

  1. 所有数据的类型是否“足够”(使用

确定数据框列的数据类型

在 r 中将多列从字符格式转换为数字格式 )? (例如,数字可能是象征性的,这是不够的)

个人建议: 如果解决方案不是一个基本功能,最好使用库

我会做的

if (!require("pacman"))
  install.packages("pacman")

# install/load nedded library
pacman::p_load(
  hablar)


# create data frame
df <- data.frame(
  x = c(1, 3, NA,-9.5, "NULL"),
  y = c("2", "5", "-0.1", "-3", "100"),
  text1 = letters[1:5],
  text2 = LETTERS[1:5],
  z = c(78, 10,-99, NA, 99)
)


# check data type of data frame
str(df)
# (num only "z")


# replace NULL to NA, because they are the same thing
df[df == "NULL"] <- NA

# filter rows with NA
df[!complete.cases(df), ]
# (replace NA to values or remove rows whit them)


# remove rows with NA
df <- na.omit(df)
df


# fix "inadequate" data
df <- retype(df) 
str(df)

输出

> if (!require("pacman"))
+   install.packages("pacman")
Loading required package: pacman
> 
> # install/load nedded library
> pacman::p_load(
+   hablar)
> 
> 
> # create data frame
> df <- data.frame(
+   x = c(1, 3, NA,-9.5, "NULL"),
+   y = c("2", "5", "-0.1", "-3", "100"),
+   text1 = letters[1:5],
+   text2 = LETTERS[1:5],
+   z = c(78, 10,-99, NA, 99)
+ )
> 
> 
> # check data type of data frame
> str(df)
'data.frame':   5 obs. of  5 variables:
 $ x    : chr  "1" "3" NA "-9.5" ...
 $ y    : chr  "2" "5" "-0.1" "-3" ...
 $ text1: chr  "a" "b" "c" "d" ...
 $ text2: chr  "A" "B" "C" "D" ...
 $ z    : num  78 10 -99 NA 99
> # (num only "z")
> 
> 
> # replace NULL to NA, because they are the same thing
> df[df == "NULL"] <- NA
> 
> # filter rows with NA
> df[!complete.cases(df), ]
     x    y text1 text2   z
3 <NA> -0.1     c     C -99
4 -9.5   -3     d     D  NA
5 <NA>  100     e     E  99
> # (replace NA to values or remove rows whit them)
> 
> 
> # remove rows with NA
> df <- na.omit(df)
> df
  x y text1 text2  z
1 1 2     a     A 78
2 3 5     b     B 10
> 
> 
> # fix "inadequate" data
> df <- retype(df) 
> str(df)
'data.frame':   2 obs. of  5 variables:
 $ x    : int  1 3
 $ y    : int  2 5
 $ text1: chr  "a" "b"
 $ text2: chr  "A" "B"
 $ z    : int  78 10
 - attr(*, "na.action")= 'omit' Named int [1:3] 3 4 5
  ..- attr(*, "names")= chr [1:3] "3" "4" "5"

© www.soinside.com 2019 - 2024. All rights reserved.