私の問題についてのこのような詳細な説明は事前にお詫び申し上げます。私はShuffle100
my_List
とFinal_lists
という3つの関数を使って、マスターリスト内の分類木クラス確率(グループ化因子:G8とV4)から10個のネストされたデータフレームを生成しました。私はこの簡単な質問をしていることを申し訳なく思っていますが、私はそれを理解することができませんでした。誰かが解決策を見つけたら、多くの感謝は前進しています。リスト内の分類ツリークラス確率のネストされたリストから混乱行列を生成する関数を適用する
ねらい1
(1)Iは、各サブセットのため
機能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
10の混同行列を生成する機能
shuffle100
に
caret package
から機能
confusionMatrix()
を挿入したいです
目的2
列V4またはG8の行の確率が互いに大きくても小さくてもよいという条件に基づいて、サブセットごとにPredicted
,Actual
およびBinary
の各列に関数またはループを書き込むことができます。しかし、私は、関数の正しい構文と混同し、ループ午前の列が満たされている。この形式が必要です
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)
}
}
各サブセットを動作しません
for loop
:
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に含まれる各サブセットの分類ツリーモデルから実際の予測クラス確率予測である `Binary
カラム
で
充填normalised_scores Predicted
とActual
の行が同じ結果(たとえばG8とG8)を持つ場合、Binary
の行には値1が割り当てられます。ただし、Predicted
との行列が異なります(例: 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)
私はあなたがこの質問をトリムするなら、助けを得ることがより良い運を持っているかもしれないと思います。私が理解しているところでは、基本的にデータフレームのリスト(またはデータフレームのリスト)を歩き、データフレーム内の他の列のデータか、コンパニオンデータセットのどちらかのデータを使用して列を埋めたいとします(私は、ネストされたリストと1対1の対応)。代わりに、ネストされたリスト構造のサブセットと、リストの1つの要素に対してどのようにタスクを達成するかに関するサンプルコードを投稿できますか?また、なぜあなたが 'Final_normalised'をインクルードしたのかはわかりません。 – mikeck
こんにちはmikeck、ありがとうございます。質問を2つのサブセクションにまとめ、1つのサブセットの作業コードを追加しました。私はこのフォーマットがより良いことを願っています。ありがとうございました。すべてのベストと世話をする –