分析の練習(2)
ロジスティック回帰分析
irisのデータを使って練習。
irisは
> head(iris) Sepal.Length Sepal.Width Petal.Length Petal.Width Species 1 5.1 3.5 1.4 0.2 setosa 2 4.9 3.0 1.4 0.2 setosa 3 4.7 3.2 1.3 0.2 setosa 4 4.6 3.1 1.5 0.2 setosa 5 5.0 3.6 1.4 0.2 setosa 6 5.4 3.9 1.7 0.4 setosa
って感じで、1:50がsetosa
、51:100がversicolor
、101:150がvirginica
となっている。今回はロジスティック回帰ということで、51:150に絞ってやる。また、変数もSepal.LengthとPetal.Lengthの二つに絞る。
> test <- iris[51:150,c(1,3,5)] > test$Species<-ifelse(test$Species=="versicolor",1,0) > head(test) Sepal.Length Petal.Length Species 51 7.0 4.7 1 52 6.4 4.5 1 53 6.9 4.9 1 54 5.5 4.0 1 55 6.5 4.6 1 56 5.7 4.5 1 > > > res<-glm(test$Species~.,test[1:2],family=binomial) > summary(res) Call: glm(formula = test$Species ~ ., family = binomial, data = test[1:2]) Deviance Residuals: Min 1Q Median 3Q Max -1.54924 -0.02909 0.00001 0.04669 2.81936 Coefficients: Estimate Std. Error z value Pr(>|z|) (Intercept) 39.839 13.089 3.044 0.002338 ** Sepal.Length 4.017 1.623 2.474 0.013348 * Petal.Length -13.313 3.913 -3.402 0.000669 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 (Dispersion parameter for binomial family taken to be 1) Null deviance: 138.63 on 99 degrees of freedom Residual deviance: 23.85 on 97 degrees of freedom AIC: 29.85 Number of Fisher Scoring iterations: 9
こんな感じ。次はできた直線をplotしていく。
> xp<-seq(min(test[,1]),max(test[,1]),0.1) > yp<-seq(min(test[,2]),max(test[,2]),0.1) > cushT<-expand.grid(Sepal.Length=xp,Petal.Length=yp) #x軸(Sepal.Length)、y軸(Petal.Length)について0.1刻みのgridを作る。expand.gridではラベル名を指定。 > head(cushT) Sepal.Length Petal.Length 1 4.9 3 2 5.0 3 3 5.1 3 4 5.2 3 5 5.3 3 6 5.4 3 > length(xp) [1] 31 #0.1刻みで要素数が31個 > Z<-predict(res,cushT,type="response") #作ったgridの全要素に対して評価。Zには結果が一次配列で格納される。 > plot(test[,1],test[,2],col=ifelse(test[,3],"red","blue")) #もともとのデータをplot > contour(xp,yp,matrix(Z,length(xp)),add=T,levels=0.5,col="green") #評価結果をplot。
最後のcontourが分かりにくいが、今回は、z=ax+byという関数を二次元でplotしようとしているため、zをどうにか表現しないといけない。そこで、contour
を用いる。この関数は等高線を引くための関数で、matrix(Z,length(xp))
でlength(xp)×length(xy)の全要素の高さZを指定してあげて、levels=0.5
の線を引く、という感じ。
SVM
続いて、SVM(Support Vector Machine)。
パッケージとしては、kernlab
ってのもあるみたいだけど、なんかエラーが出てインストールできなかったので、e1071
でトライ。
とりあえずインストールした後、irisの半分を訓練データとして確保し、残りを評価する。
> install.packages("e1071") > library(e1071) > rowdata<-nrow(iris) > random_ids<-sample(rowdata,rowdata*0.5) > random_ids [1] 2 111 107 143 122 119 121 114 10 84 103 6 60 109 11 118 138 89 113 [20] 88 75 129 76 125 91 15 64 140 80 36 74 52 145 47 102 135 94 13 [39] 82 142 23 8 33 71 55 69 85 133 58 37 81 97 56 16 137 7 131 [58] 42 28 100 53 104 9 87 26 27 95 120 90 66 30 83 25 77 61 > iris_training<-iris[random_ids,] > iris_training Sepal.Length Sepal.Width Petal.Length Petal.Width Species 2 4.9 3.0 1.4 0.2 setosa 111 6.5 3.2 5.1 2.0 virginica 107 4.9 2.5 4.5 1.7 virginica 143 5.8 2.7 5.1 1.9 virginica 122 5.6 2.8 4.9 2.0 virginica > iris_predicting<-iris[-random_ids,] > iris_predicting Sepal.Length Sepal.Width Petal.Length Petal.Width Species 1 5.1 3.5 1.4 0.2 setosa 3 4.7 3.2 1.3 0.2 setosa 4 4.6 3.1 1.5 0.2 setosa 5 5.0 3.6 1.4 0.2 setosa 12 4.8 3.4 1.6 0.2 setosa 14 4.3 3.0 1.1 0.1 setosa 17 5.4 3.9 1.3 0.4 setosa 18 5.1 3.5 1.4 0.3 setosa 19 5.7 3.8 1.7 0.3 setosa > iris_svm<-svm(Species~.,data=iris_training) > iris_svm Call: svm(formula = Species ~ ., data = iris_training) Parameters: SVM-Type: C-classification SVM-Kernel: radial cost: 1 gamma: 0.25 Number of Support Vectors: 38 > result_predict<-predict(iris_svm,iris_predicting) > result_predict 1 3 4 5 12 14 17 setosa setosa setosa setosa setosa setosa setosa 18 19 20 21 22 24 29 setosa setosa setosa setosa setosa setosa setosa 31 32 34 35 38 39 40 setosa setosa setosa setosa setosa setosa setosa 41 43 44 45 46 48 49 setosa setosa setosa setosa setosa setosa setosa 50 51 54 57 59 62 63 setosa versicolor versicolor versicolor versicolor versicolor versicolor 65 67 68 70 72 73 78 versicolor versicolor versicolor versicolor versicolor versicolor versicolor 79 86 92 93 96 98 99 versicolor versicolor versicolor versicolor versicolor versicolor versicolor 101 105 106 108 110 112 115 virginica virginica virginica virginica virginica virginica virginica 116 117 123 124 126 127 128 virginica virginica virginica virginica virginica virginica virginica 130 132 134 136 139 141 144 virginica virginica versicolor virginica virginica virginica virginica 146 147 148 149 150 virginica virginica virginica virginica virginica Levels: setosa versicolor virginica > table(result_predict,iris_predicting$Species) result_predict setosa versicolor virginica setosa 29 0 0 versicolor 0 20 1 virginica 0 0 25 > (29+20+25)/75 [1] 0.9866667
ということで、irisデータでは98.66%の精度に!なるほどよくわからんがすごそうだな