質問編集履歴
2
補足の加筆
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
2
|
|
3
3
|
約2000名分のデータを使って、ランダムフォレストの結果を部分従属プロットにしたいです。
|
4
|
-
|
5
|
-
![イメージ説明](939ad2345b5375b16d7964b2b2bd7003.png)
|
6
4
|
|
7
5
|
しかし、partial_dependence関数を使うと以下のエラーメッセージが発生しました。
|
8
6
|
|
1
データ内容の添付
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
2
|
|
3
3
|
約2000名分のデータを使って、ランダムフォレストの結果を部分従属プロットにしたいです。
|
4
|
+
|
5
|
+
![イメージ説明](939ad2345b5375b16d7964b2b2bd7003.png)
|
4
6
|
|
5
7
|
しかし、partial_dependence関数を使うと以下のエラーメッセージが発生しました。
|
6
8
|
|
@@ -24,9 +26,83 @@
|
|
24
26
|
|
25
27
|
```R
|
26
28
|
|
27
|
-
#
|
29
|
+
#書いたコードをすべて加筆しました
|
30
|
+
|
31
|
+
n <- ncol(alldata)
|
32
|
+
|
33
|
+
ix <- 1:n
|
34
|
+
|
35
|
+
data1 <- lapply(alldata[ix], as.numeric) #alldataが元データの名前です
|
36
|
+
|
37
|
+
data1$SEX <-as.character(data1$SEX) #性別をcharacterにする
|
28
38
|
|
29
39
|
|
40
|
+
|
41
|
+
data1 <-as.data.frame(data1)
|
42
|
+
|
43
|
+
is.data.frame(data1)
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
data1$変数A <- rowMeans(data1[,c("A1","A2","A3","A4","A5","A6","A7","A8","A9","A10","A11","A12","A13")],na.rm = T)
|
48
|
+
|
49
|
+
data1$変数B <- rowMeans(data1[,c("B1","B2","B3","B4","B5","B6","B7","B8","B9","B10","B11","B12")],na.rm = T)
|
50
|
+
|
51
|
+
data1$変数Ca <- rowSums(data1[,c("C1","C3","C6","C7","C9","C13","C14")],na.rm = T)
|
52
|
+
|
53
|
+
data1$変数Cb <- rowSums(data1[,c("C2","C4","C11","C12","C17")],na.rm = T)
|
54
|
+
|
55
|
+
data1$変数Da <- rowMeans(data1[,c("D1","D2","D3","D4","D5","D6","D7","D8")],na.rm = T)
|
56
|
+
|
57
|
+
data1$変数Db <- rowMeans(data1[,c("D9","D10","D11","D12","D13","D14","D15")],na.rm = T)
|
58
|
+
|
59
|
+
data1$変数Dc <- rowMeans(data1[,c("D16","D17","D18","D19","D20")],na.rm = T)
|
60
|
+
|
61
|
+
data1$変数Dd <- rowMeans(data1[,c("D21","D22","D23","D24","D25","D26","D27")],na.rm = T)
|
62
|
+
|
63
|
+
data1$変数De <-rowMeans(data1[,c("D28","D29")],na.rm = T)
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
#クラスタの作成
|
68
|
+
|
69
|
+
library(dplyr)
|
70
|
+
|
71
|
+
ABC_s <- data1 %>% select(変数Da,変数Db,変数Dc,変数Dd,変数De)
|
72
|
+
|
73
|
+
distance <- dist(ABC_s)
|
74
|
+
|
75
|
+
hc <- hclust(distance, method="ward.D2")
|
76
|
+
|
77
|
+
plot(hc)
|
78
|
+
|
79
|
+
cluster <- cutree(hc, k=4) #クラスタ数の指定
|
80
|
+
|
81
|
+
cluster
|
82
|
+
|
83
|
+
cluster <- as.factor(cluster) #Factor型に変更
|
84
|
+
|
85
|
+
data1$cluster <- cluster
|
86
|
+
|
87
|
+
class(data1$cluster)
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
df <- data1
|
92
|
+
|
93
|
+
df <- mutate(df,cluster_N = if_else(cluster == 3,true = 1,false = 0))
|
94
|
+
|
95
|
+
data1$cluster_N <- df$cluster_N
|
96
|
+
|
97
|
+
data1$cluster_N <- as.factor(data1$cluster_N)
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
#最後の行を実行するとエラーが出ます--------------
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
library(randomForest)
|
30
106
|
|
31
107
|
set.seed(1)
|
32
108
|
|
@@ -46,7 +122,7 @@
|
|
46
122
|
|
47
123
|
#分類モデルの作成
|
48
124
|
|
49
|
-
model.rf <- randomForest(ABC ~ 変数A + 変数B + 変数C + 変数
|
125
|
+
model.rf <- randomForest(ABC ~ 変数A + 変数B + 変数Ca + 変数Cb, data = df.train)
|
50
126
|
|
51
127
|
#分類モデルの確認
|
52
128
|
|