首页 > 解决方案 > R中的Levene测试下面的数据框

问题描述

dput(head(Clean, 100))

structure(list(`Reporting unit` = c("Albury Wodonga Health [Albury Campus]", 
"Albury Wodonga Health [Albury Campus]", "Albury Wodonga Health [Albury Campus]", 
"Albury Wodonga Health [Albury Campus]", "Albury Wodonga Health [Albury Campus]", 
"Albury Wodonga Health [Albury Campus]", "Albury Wodonga Health [Albury Campus]", 
"Albury Wodonga Health [Albury Campus]", "Albury Wodonga Health [Albury Campus]", 
"Albury Wodonga Health [Albury Campus]"), `Reporting unit type` = c("Hospital", 
"Hospital", "Hospital", "Hospital", "Hospital", "Hospital", "Hospital", 
"Hospital", "Hospital", "Hospital"), State = c("NSW", "NSW", 
"NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW", "NSW"), `Local Hospital Network (LHN)` = c("Albury Wodonga Health", 
"Albury Wodonga Health", "Albury Wodonga Health", "Albury Wodonga Health", 
"Albury Wodonga Health", "Albury Wodonga Health", "Albury Wodonga Health", 
"Albury Wodonga Health", "Albury Wodonga Health", "Albury Wodonga Health"
), `Peer group` = c("Large hospitals", "Large hospitals", "Large hospitals", 
"Large hospitals", "Large hospitals", "Large hospitals", "Large hospitals", 
"Large hospitals", "Large hospitals", "Large hospitals"), `Time period` = c("2011–12", 
"2012–13", "2013–14", "2014–15", "2015–16", "2016–17", "2011–12", 
"2012–13", "2013–14", "2014–15"), Category = c("Cellulitis", 
"Cellulitis", "Cellulitis", "Cellulitis", "Cellulitis", "Cellulitis", 
"Chronic Obstructive Pulmonary Disease (without complications)", 
"Chronic Obstructive Pulmonary Disease (without complications)", 
"Chronic Obstructive Pulmonary Disease (without complications)", 
"Chronic Obstructive Pulmonary Disease (without complications)"
), `Total number of stays` = c(111, 116, 141, 155, 210, 196, 
109, 116, 75, 132), `Number of overnight stays` = c(92, 98, 115, 
123, 166, 155, 108, 113, 71, 122), `Percentage of overnight stays` = c(0.83, 
0.84, 0.82, 0.79, 0.79, 0.79, 0.99, 0.97, 0.95, 0.92), `Average length of stay (days)` = c(3.9, 
3.3, 3.1, 2.5, 2.6, 2.7, 5.8, 4.6, 5.7, 4.4), `Peer group average (days)` = c(3.7, 
3.5, 3.3, 3.2, 3, 3, 4.8, 4.4, 4.2, 3.9), `Total overnight patient bed days` = c(356, 
326, 351, 306, 431, 418, 622, 518, 405, 538)), row.names = c(NA, 
-10L), class = c("tbl_df", "tbl", "data.frame"))

嗨,我正在尝试对对等组(大中型医院)和平均长度之间的上述数据框进行 levene 测试,但出现以下错误:

leveneTest.formula(Clean$group ~ Clean$ Average length of stay (days), : Levene 的测试不适合定量解释变量。

有人可以帮忙吗?

标签: r

解决方案


后续可以解决问题。

  1. 如果使用公式接口,则不需要其中的data.frame名称,只需在argument中data
  2. 公式项的顺序应该颠倒,解释变量应该在RHS上。

由于leveneTest可能未加载的包,我在函数调用前加上前缀。

car::leveneTest(`Average length of stay (days)` ~ `Peer group`, data = Clean)

此外,列名在语法上无效,名称中的空格需要反引号。

names(Clean) <- make.names(names(Clean))

推荐阅读