-1
在呼叫,在代碼的末尾,以:錯誤的eval(表達式,ENVIR,enclos):對象roll_belt「中找不到預測
predict(pml_training_rf_model_1, pml_validation$classe)
我得到的錯誤:
錯誤的eval(表達式,ENVIR,enclos):對象roll_belt'未找到
那是因爲我要調用這樣的功能:
predict(pml_training_rf_model_1, pml_validation)
「roll_belt」屬性確實出現在我正在使用的數據框中,所以我清楚地犯了一些其他錯誤,現在已經糾正並保存爲後人。
#Start code
rm(list=ls())
library("caret")
library("data.table")
library("randomForest")
set.seed(12345)
pml_training_file <- "pml-training.csv"
pml_testing_file <- "pml-testing.csv"
if (!file.exists(pml_training_file)) {
pml_training_url <- "http://d396qusza40orc.cloudfront.net/predmachlearn/pml-training.csv"
download.file(pml_training_url, pml_training_file)
}
pml_testing_file <- "pml-testing.csv"
if (!file.exists(pml_testing_file)) {
pml_testing_url <- "http://d396qusza40orc.cloudfront.net/predmachlearn/pml-testing.csv"
download.file(pml_testing_url, pml_testing_file)
}
pml_training_original <- fread(pml_training_file, na.strings=c("NA","#DIV/0!",""), data.table = FALSE, stringsAsFactors = TRUE)
partition_index <- createDataPartition(y=pml_training_original$classe, p=0.6, list = FALSE)
pml_training <- pml_training_original[partition_index,]
pml_validation <- pml_training_original[-partition_index,]
#Remove metadata columns
pml_training <- pml_training[,-c(1:7)]
#Remove columns where the number of NA results is above a given level
na_level = .75
nrow_pml_training = nrow(pml_training)
na_col_nums <- numeric()
for(i in 1:length(pml_training)) {
sum_na = sum(is.na(pml_training[, i]))
if(sum_na/nrow_pml_training >= na_level) {
na_col_nums <- c(na_col_nums, i)
}
}
pml_training <- pml_training[-na_col_nums]
#Set the columns in the validation data to be the same as those in the training data
pml_training_colnames <- colnames(pml_training)
pml_validation <- pml_validation[, pml_training_colnames]
pml_training_rf_model_1 <- randomForest(classe ~ ., data=pml_training)
#Wrong! pml_training_predictions_1 <- predict(pml_training_rf_model_1, pml_validation$classe)
pml_training_predictions_1 <- predict(pml_training_rf_model_1, pml_validation)
confusionMatrix(pml_validation$classe, pml_training_predictions_1)
證明roll_belt在pml_validation $ classe中。因爲我認爲R說它不在那裏。 – Roland
您需要提供一個使其失敗的輸入數據示例,以便我們可以重現和測試您的問題。謝謝。 – lrnzcig
感謝@Irnzcig,輸入數據加載也包含在代碼示例中。 –