r语言 - relevel.default(delays.df$ORIGIN, ref = "IAD" ) 中的错误:'relevel'仅适用于(无序)因素



试图在我的书中工作这段代码(它一直给我不工作的代码)

#### Table 10.6
delays.df <- read.csv("/Users/CHAPTER 10 ASSIGNMENT 6/FlightDelays.csv")
head(delays.df)
# transform variables and create bins
delays.df$DAY_WEEK <- factor(delays.df$DAY_WEEK, levels = c(1:7),
labels = c("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"))
delays.df$CRS_DEP_TIME <- factor(round(delays.df$CRS_DEP_TIME/100))
head(delays.df)
DAY_WEEK     CRS_DEP_TIME
<fctr>        <fctr>

# create reference categories
delays.df$ORIGIN <- relevel(delays.df$ORIGIN, ref = "IAD")
delays.df$DEST <- relevel(delays.df$DEST, ref = "LGA")
delays.df$CARRIER <- relevel(delays.df$CARRIER, ref = "US")
delays.df$DAY_WEEK <- relevel(delays.df$DAY_WEEK, ref = "Wed")
delays.df$isDelay <- 1 * (delays.df$Flight.Status == "delayed")
# create training and validation sets
selected.var <- c(10, 1, 8, 4, 2, 9, 14)
train.index <- sample(c(1:dim(delays.df)[1]), dim(delays.df)[1]*0.6)  
train.df <- delays.df[train.index, selected.var]
valid.df <- delays.df[-train.index, selected.var]
# run logistic model, and show coefficients and odds
lm.fit <- glm(isDelay ~ ., data = train.df, family = "binomial")
data.frame(summary(lm.fit)$coefficients, odds = exp(coef(lm.fit))) 
round(data.frame(summary(lm.fit)$coefficients, odds = exp(coef(lm.fit))), 5)

我无法通过任何relevel命令,因为以下错误:

delays.df$ORIGIN <- relevel(delays.df$ORIGIN, ref = "IAD")

level.default(delayed .df$ORIGIN, ref = "IAD")出错:'relevel'仅用于(无序)因子

我所理解的是,我的变量是因素,relevel命令对我的因素不起作用。我不知道该怎么解决?如果不是这样,我真的不知道该怎么解决。

一如既往,任何帮助或见解都是感激的。

在r4.0之前,选项stringsAsFactors默认为TRUE,即在被(例如)read.csv()读入时,类字符串变量会自动转换为因子。除非您在读取数据之前设置options(stringsAsFactors=TRUE)(或在read.csv()调用中指定该选项作为参数,即read.csv("[filename]", stringsAsFactors=TRUE)),否则情况不再如此

您可以通过其中一种方式设置选项,或者您可以在relevel():

之前显式转换为因子。
delays.df$ORIGIN <- relevel(factor(delays.df$ORIGIN), ref = "IAD")

明智地使用transform()(base R)或mutate()(tidyverse)可以使您的设置代码更简洁。

相关内容

  • 没有找到相关文章

最新更新