繁体   English   中英

从R中的先前数据帧复制因子

[英]Copying factors from previous data frames in R

我想将因子水平从现有的数据框中复制到新创建的数据框中,而不是手动分配水平。

为了使用“预测”功能,R要求新数据位于因数与模型训练数据相同的数据框中。 我想相信这些因素可以从训练数据复制到新的数据框架。 我已经做到这一点,如下面的代码所示,尽管很笨拙。

# Build the model
naive_model <- NaiveBayes(outcome ~ purpose_ + home_ + emp_len_, data = loan_data, na.action = na.omit)

# Create new data point to be tested
new_loan_frame <- data.frame(purpose_ = "small_business", home_ = "MORTGAGE", emp_len_ = "> 1 Year")

# Add the necessary factors to match the training data
new_loan_frame$purpose_ <- factor(new_loan_frame$purpose_, levels = c("credit_card","debt_consolidation", "home_improvement", "major_purchase", "medical","other","small_business"))
new_loan_frame$home_ <- factor(new_loan_frame$home_, levels = c("MORTGAGE", "OWN", "RENT"))
new_loan_frame$emp_len_ <- factor(new_loan_frame$emp_len_, levels = c("< 1 Year", "> 1 Year"))

# Run the prediction using the model and the new data
predict(naive_model, new_loan_frame)

写出每种输入类型的因素似乎比我预期的要繁重。 清理此事的最佳方法是什么?

您可以自动执行所有操作。

for(cn in colnames(loan_data)) {
  new_loan_frame[,cn] <- factor(new_loan_frame[,cn], levels=levels(loan_data[,cn]))
}

嗨,欢迎来到Stackoverflow。正确的是,为了进行预测,您必须在一个数据框中很好地组织数据。 请尝试以下方法:

new_loan_frame <- data.frame(purpose= rep(levels(loan_data$purpose),3), home = rep(levels(loan_data$home),each=7), emp_len=rep(levels(loan_data$emp_len)))

Preds1<-predict(naive_model , newdata=new_load_frame, level=0)

另外,尝试不要在级别名称中使用“ _”。 相反,您可以简单地使用: , sep="_")

祝好运

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM