【发布时间】:2016-07-22 13:50:16
【问题描述】:
对于我的问题进行如此冗长而详细的解释,我提前道歉。我使用三个函数Shuffle100my_List 和Final_lists(下图)从主列表中的分类树类概率(分组因子:G8 和 V4)中生成了 10 个嵌套数据帧。很抱歉,我问了这个简单的问题,但我无法弄清楚。如果有人找到解决方案,非常感谢。
目标 1
(1) 我想将caret package 中的函数confusionMatrix() 插入到函数shuffle100 中,为每个子集生成10 个混淆矩阵
函数shuffle100、my_list 和Final_lists
library(plyr)
library(caret)
library(e1071)
library(rpart)
set.seed(1235)
shuffle100 <-lapply(seq(10), function(n){ #Select the production of 10 dataframes
subset <- normalised_scores[sample(nrow(normalised_scores), 80),] #Shuffle rows
subset_idx <- sample(1:nrow(subset), replace = FALSE)
subset <- subset[subset_idx, ] #training subset
subset1<-subset[-subset_idx, ] #test subset
subset_resampled_idx <- createDataPartition(subset_idx, times = 1, p = 0.7, list = FALSE) #70 % training set
subset_resampled <- subset[subset_resampled_idx, ]
ct_mod<-rpart(Matriline~., data=subset_resampled, method="class", control=rpart.control(cp=0.005)) #10 ct
ct_pred<-predict(ct_mod, newdata=subset[, 2:13])
ct_dataframe=as.data.frame(ct_pred)#create new data frame
confusionMatrix(ct_dataframe, normalised_scores$Family)
}
Error in sort.list(y) : 'x' must be atomic for 'sort.list'
Have you called 'sort' on a list?
1: lapply(seq(10), function(n) {
subset <- normalised_scores[sample(nrow(normalised_scores
2: FUN(X[[i]], ...)
3: confusionMatrix(ct_dataframe, normalised_scores$Family)
4: confusionMatrix.default(ct_dataframe, normalised_scores$Family)
5: factor(data)
6: sort.list(y)
#Produce three columns: Predicted, Actual and Binary
my_list <- lapply(shuffle100, function(df){#Create two new columns Predicted and Actual
if (nrow(df) > 0)
cbind(df, Predicted = c(""), Actual = c(""), Binary = c(""))
else
bind(df, Predicted = character(), Actual = c(""), Binary = c (""))
})
#Fill the empty columns with NA's
Final_lists <- lapply(my_list, function(x) mutate(x, Predicted = NA, Actual = NA, Binary = NA))
#Create a dataframe from the column normalised_scores$Family to fill the Actual column
Actual_scores<-Final_normalised3$Family
Final_scores<-as.data.frame(Actual_scores)
#Fill in the Predicted, Actual and Binary columns
Predicted_Lists <- Final_lists %>%
mutate(Predicted=ifelse(G8 > V4, G8, V4)) %>% # assuming if G8 > V4 then Predicted=G8
mutate(Actual=Final_scores) %>% # your definition of Actual is not clear
mutate(Binary=ifelse(Predicted==Actual, 1, 0))
#Error messages
Error in ifelse(G8 > V4, G8, V4) : object 'G8' not found
目标 2
根据列 V4 或 G8 的行中的概率可能大于或小于彼此的条件,编写函数或 for 循环来填充每个子集的 Predicted、Actual 和 Binary 列.但是,我对函数和循环的正确语法感到困惑
一个不起作用的for loop
for(i in 1:length(Final_lists)){ #i loops through each dataframe in the list
for(j in 2:nrow(Final_lists[[i]])){ #j loops through each row of each dataframe in the list
if(Final_lists[[i]][j, "G8"] > Final_lists[[i]][j, "V4"]) { #if the probability of G8 > V4 in each row of each dataframe in each list
Final_lists[[i]][j, [j["Predicted" == "NA"]] ="G8" #G8 will be filled into the same row in the `Predicted' column
}
else {
Final_lists[[i]][j, [Predicted == "NA"]] ="V4" #V4 will be filled into the same row in the `Predicted' column
}
print(i)
}
}
当列被填充时,每个子集都应该具有这种格式:
G8 V4 Predicted Actual Binary
0.1764706 0.8235294 V4 V4 1
0.7692308 0.2307692 G8 V4 0
0.7692308 0.2307692 G8 V4 0
0.7692308 0.2307692 G8 V4 0
0.7692308 0.2307692 G8 V4 0
0.1764706 0.8235294 V4 V4 1
填写Predicted 列
如果 G8 > V4 的概率,则为空的Predicted 行分配 G8。但是,如果 V4 > G8,那么空的 `Predicted' 行将被分配 V4。
填写Actual 列
这些是来自每个子集的分类树模型的实际预测类别概率预测,包含在 data_frame `normalised_scores
填写Binary栏
如果Predicted 和Actual 行具有相同的结果(例如G8 和G8),则Binary 行被赋值为1。但是,如果Predicted 和Actual 的行列不同(例如 G8 和 V4),则将 Binary 行赋值为 0。
我使用此工作代码实现了这些目标,但是,我不确定如何将此代码应用于主列表中的子集。
单个子集的工作代码
set.seed(1235)
# Randomly permute the data before subsetting
mydat_idx <- sample(1:nrow(Final_normalised_scores), replace = FALSE)
mydat <- Final_normalised3[mydat_idx, ]
mydat_resampled_idx <- createDataPartition(mydat_idx, times = 1, p = 0.7, list = FALSE)
mydat_resampled <- mydat[mydat_resampled_idx, ] # Training portion of the data
mydat_resampled1 <- mydat[-mydat_resampled_idx, ]
#Classification tree
ct_mod <- train(x = mydat_resampled[, 2:13], y = as.factor(mydat_resampled[, 1]),
method = "rpart", trControl = trainControl(method = "repeatedcv", number=10, repeats=100, classProbs = TRUE))
#Model predictions
ct_pred <- predict(ct_mod, newdata = mydat[ , 2:13], type = "prob")
Final_Predicted<-as.data.frame(ct_pred)
#Produce three empty columns: Predicted, Actual and Binary
Final_Predicted$Predicted<-NA
Final_Predicted$Actual<-NA
Final_Predicted$Binary<-NA
#Fill in the Predicted column
for (i in 1:length(Final_Predicted$G8)){
if(Final_Predicted$G8[i]>Final_Predicted$V4[i]) {
Final_Predicted$Predicted[i]<-"G8"
}
else {
Final_Predicted$Predicted[i]<-"V4"
}
print(i)
}
#Fill in the Actual column using the actual predictions from the dataframe normalised_scores
Final_Predicted$Actual<-normalised_scores$Family
#Fill in the Binary column
for (i in 1:length(Final_Predicted$Binary)){
if(Final_Predicted$Predicted[i]==Final_Predicted$Actual[i]) {
Final_Predicted$Binary[i]<-1
}
else {
Final_Predicted$Binary[i]<-0
}
print(i)
}
主列表中的子集
G8 V4 Predicted Actual Binary
0.1764706 0.8235294 NA NA NA
0.7692308 0.2307692 NA NA NA
0.7692308 0.2307692 NA NA NA
0.7692308 0.2307692 NA NA NA
0.7692308 0.2307692 NA NA NA
0.1764706 0.8235294 NA NA NA
0.7692308 0.2307692 NA NA NA
0.7692308 0.2307692 NA NA NA
0.7692308 0.2307692 NA NA NA
0.7692308 0.2307692 NA NA NA
0.7692308 0.2307692 NA NA NA
0.1764706 0.8235294 NA NA NA
0.1764706 0.8235294 NA NA NA
0.1764706 0.8235294 NA NA NA
0.7692308 0.2307692 NA NA NA
0.1764706 0.8235294 NA NA NA
0.7692308 0.2307692 NA NA NA
0.7692308 0.2307692 NA NA NA
0.7692308 0.2307692 NA NA NA
0.7692308 0.2307692 NA NA NA
0.7692308 0.2307692 NA NA NA
0.7692308 0.2307692 NA NA NA
0.7692308 0.2307692 NA NA NA
0.7692308 0.2307692 NA NA NA
0.7692308 0.2307692 NA NA NA
0.1764706 0.8235294 NA NA NA
0.7692308 0.2307692 NA NA NA
0.7692308 0.2307692 NA NA NA
0.7692308 0.2307692 NA NA NA
0.1764706 0.8235294 NA NA NA
0.7692308 0.2307692 NA NA NA
0.7692308 0.2307692 NA NA NA
0.7692308 0.2307692 NA NA NA
0.7692308 0.2307692 NA NA NA
0.1764706 0.8235294 NA NA NA
0.1764706 0.8235294 NA NA NA
0.7692308 0.2307692 NA NA NA
0.7692308 0.2307692 NA NA NA
0.7692308 0.2307692 NA NA NA
0.7692308 0.2307692 NA NA NA
0.7692308 0.2307692 NA NA NA
0.1764706 0.8235294 NA NA NA
0.7692308 0.2307692 NA NA NA
0.7692308 0.2307692 NA NA NA
0.1764706 0.8235294 NA NA NA
0.7692308 0.2307692 NA NA NA
0.1764706 0.8235294 NA NA NA
0.1764706 0.8235294 NA NA NA
0.7692308 0.2307692 NA NA NA
0.7692308 0.2307692 NA NA NA
0.7692308 0.2307692 NA NA NA
0.1764706 0.8235294 NA NA NA
0.7692308 0.2307692 NA NA NA
0.7692308 0.2307692 NA NA NA
0.7692308 0.2307692 NA NA NA
0.1764706 0.8235294 NA NA NA
0.7692308 0.2307692 NA NA NA
0.7692308 0.2307692 NA NA NA
0.1764706 0.8235294 NA NA NA
0.1764706 0.8235294 NA NA NA
0.7692308 0.2307692 NA NA NA
0.1764706 0.8235294 NA NA NA
0.1764706 0.8235294 NA NA NA
0.1764706 0.8235294 NA NA NA
0.7692308 0.2307692 NA NA NA
0.7692308 0.2307692 NA NA NA
0.1764706 0.8235294 NA NA NA
0.7692308 0.2307692 NA NA NA
0.7692308 0.2307692 NA NA NA
0.7692308 0.2307692 NA NA NA
0.7692308 0.2307692 NA NA NA
0.1764706 0.8235294 NA NA NA
0.1764706 0.8235294 NA NA NA
0.7692308 0.2307692 NA NA NA
0.7692308 0.2307692 NA NA NA
0.1764706 0.8235294 NA NA NA
0.7692308 0.2307692 NA NA NA
0.7692308 0.2307692 NA NA NA
0.7692308 0.2307692 NA NA NA
0.1764706 0.8235294 NA NA NA
可重现的虚拟数据
SummarySE (Rmisc package) to produce a barplot with error bars (ggplot2)
【问题讨论】:
-
我认为如果你减少这个问题,你可能会获得更好的帮助。据我了解,您基本上想遍历数据框列表(或数据框列表列表)并使用来自数据框内其他列或某些伴随数据集的数据(我认为有一个-与嵌套列表一一对应)。您能否改为发布嵌套列表结构的一个子集,以及一些示例代码,说明您将如何为列表中的一个元素完成任务?我也不清楚你为什么包含
Final_normalised。 -
嗨,mikeck,感谢您的建议。我将问题分为两个小节,并为一个子集添加了工作代码。我希望这种格式更好。谢谢你。一切顺利,保重
标签: r list r-caret rpart confusion-matrix