{"id":475,"date":"2018-09-02T16:44:45","date_gmt":"2018-09-02T07:44:45","guid":{"rendered":"https:\/\/best-biostatistics.com\/toukei-er\/entry\/classification-by-machine-learning-in-r\/"},"modified":"2024-10-13T14:27:45","modified_gmt":"2024-10-13T05:27:45","slug":"classification-by-machine-learning-in-r","status":"publish","type":"post","link":"https:\/\/best-biostatistics.com\/toukei-er\/entry\/classification-by-machine-learning-in-r\/","title":{"rendered":"R \u3067\u5206\u985e\u8ab2\u984c\u3092\u6a5f\u68b0\u5b66\u7fd2\u30e2\u30c7\u30eb\u3067\u5b9f\u884c\u3059\u308b\u65b9\u6cd5"},"content":{"rendered":"\n<p>\u6a5f\u68b0\u5b66\u7fd2\u3067\u3001\u3088\u308a\u3088\u304f\u63a8\u6e2c\u3067\u304d\u308b\u30e2\u30c7\u30eb\u3092\u9078\u3076\u3002<\/p>\n\n\n\n<p>\u7d71\u8a08\u30bd\u30d5\u30c8R\u306eISLR\u30d1\u30c3\u30b1\u30fc\u30b8\u306eWeekly\u30c7\u30fc\u30bf\u3067\u57fa\u790e\u7684\u306a\u6a5f\u68b0\u5b66\u7fd2\u3092\u884c\u3063\u3066\u307f\u305f\u3002<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\" id=\"R\u3067\u6a5f\u68b0\u5b66\u7fd2\u3092\u884c\u3046\u305f\u3081\u30c7\u30fc\u30bf\u306e\u6e96\u5099\">R\u3067\u6a5f\u68b0\u5b66\u7fd2\u3092\u884c\u3046\u305f\u3081\u30c7\u30fc\u30bf\u306e\u6e96\u5099<\/h2>\n\n\n\n<p>ISLR\u30d1\u30c3\u30b1\u30fc\u30b8\u306eWeekly\u30c7\u30fc\u30bf\u306f\u3001S&amp;P500\u6307\u6570\u306e\u9031\u5f53\u305f\u308a\u306e\u30ea\u30bf\u30fc\u30f3\u306e\u30c7\u30fc\u30bf\u3002<\/p>\n\n\n\n<p>9\u3064\u306e\u5909\u6570\u30011089\u884c\u306e\u30c7\u30fc\u30bf\u304b\u3089\u6210\u308a\u7acb\u3063\u3066\u3044\u308b\u3002<\/p>\n\n\n\n<p>ISLR \u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u6700\u521d\u306e\u4e00\u56de\u3060\u3051\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>install.packages(\"ISLR\")<\/code><\/pre>\n\n\n\n<p>\u4f7f\u3046\u3068\u304d\u306b\u306f\u3001library() \u3067\u547c\u3073\u51fa\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>library(ISLR)<\/code><\/pre>\n\n\n\n<p>Weekly\u30c7\u30fc\u30bf\u3092\u78ba\u8a8d\u3057\u3066\u307f\u308b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt; str(Weekly)\n'data.frame':   1089 obs. of  9 variables:\n$ Year     : num  1990 1990 1990 1990 1990 1990 1990 1990 1990 1990 ...\n$ Lag1     : num  0.816 -0.27 -2.576 3.514 0.712 ...\n$ Lag2     : num  1.572 0.816 -0.27 -2.576 3.514 ...\n$ Lag3     : num  -3.936 1.572 0.816 -0.27 -2.576 ...\n$ Lag4     : num  -0.229 -3.936 1.572 0.816 -0.27 ...\n$ Lag5     : num  -3.484 -0.229 -3.936 1.572 0.816 ...\n$ Volume   : num  0.155 0.149 0.16 0.162 0.154 ...\n$ Today    : num  -0.27 -2.576 3.514 0.712 1.178 ...\n$ Direction: Factor w\/ 2 levels \"Down\",\"Up\": 1 1 2 2 2 1 2 2 2 1 ...\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"R\u3067\u6a5f\u68b0\u5b66\u7fd2\u3092\u30ed\u30b8\u30b9\u30c6\u30a3\u30c3\u30af\u56de\u5e30\u30e2\u30c7\u30eb\u3067\u884c\u3046\u65b9\u6cd5\">R\u3067\u6a5f\u68b0\u5b66\u7fd2\u3092\u30ed\u30b8\u30b9\u30c6\u30a3\u30c3\u30af\u56de\u5e30\u30e2\u30c7\u30eb\u3067\u884c\u3046\u65b9\u6cd5<\/h2>\n\n\n\n<p>Lag1\u304b\u3089Lag5\u3001Volume\u306e6\u3064\u306e\u5909\u6570\u3067Direction\u3092\u4e88\u6e2c\u3059\u308b\u30ed\u30b8\u30b9\u30c6\u30a3\u30c3\u30af\u56de\u5e30\u30e2\u30c7\u30eb\u3092\u4f5c\u6210\u3059\u308b\u3002<\/p>\n\n\n\n<p>Direction\u306f\u30dd\u30b8\u30c6\u30a3\u30d6\u306a\u30ea\u30bf\u30fc\u30f3\u304b\u30cd\u30ac\u30c6\u30a3\u30d6\u306a\u30ea\u30bf\u30fc\u30f3\u304b\u3002<\/p>\n\n\n\n<p>Lag2\u3060\u3051\u7d71\u8a08\u5b66\u7684\u6709\u610f\u306b\u95a2\u9023\u3059\u308b\u3068\u3044\u3046\u7d50\u679c\u3002Lag2\u306f2\u9031\u9593\u524d\u304b\u3089\u307f\u305f\u30d1\u30fc\u30bb\u30f3\u30c6\u30fc\u30b8\u30ea\u30bf\u30fc\u30f3\u3002\u30dd\u30b8\u30c6\u30a3\u30d6\u30ea\u30bf\u30fc\u30f3\u306b\u95a2\u9023\u3057\u3066\u3044\u308b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt; res1 &lt;- glm(Direction ~ Lag1 + Lag2 + Lag3 + Lag4 + Lag5 + Volume, family=binomial, data=Weekly)\n&gt;\n&gt; summary(res1)\nCall:\nglm(formula = Direction ~ Lag1 + Lag2 + Lag3 + Lag4 + Lag5 +\nVolume, family = binomial, data = Weekly)\nDeviance Residuals:\nMin       1Q   Median       3Q      Max\n-1.6949  -1.2565   0.9913   1.0849   1.4579\nCoefficients:\nEstimate Std. Error z value Pr(&gt;|z|)\n(Intercept)  0.26686    0.08593   3.106   0.0019 **\nLag1        -0.04127    0.02641  -1.563   0.1181\nLag2         0.05844    0.02686   2.175   0.0296 *\nLag3        -0.01606    0.02666  -0.602   0.5469\nLag4        -0.02779    0.02646  -1.050   0.2937\nLag5        -0.01447    0.02638  -0.549   0.5833\nVolume      -0.02274    0.03690  -0.616   0.5377\n---\nSignif. codes:  0 \u2018***\u2019 0.001 \u2018**\u2019 0.01 \u2018*\u2019 0.05 \u2018.\u2019 0.1 \u2018 \u2019 1\n(Dispersion parameter for binomial family taken to be 1)\nNull deviance: 1496.2  on 1088  degrees of freedom\nResidual deviance: 1486.4  on 1082  degrees of freedom\nAIC: 1500.4\nNumber of Fisher Scoring iterations: 4\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u5b66\u7fd2\u30c7\u30fc\u30bf\u3068\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u306b\u5206\u5272\u3057\u3066\u6b63\u7b54\u7387\u3092\u898b\u308b\">\u5b66\u7fd2\u30c7\u30fc\u30bf\u3068\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u306b\u5206\u5272\u3057\u3066\u6b63\u7b54\u7387\u3092\u898b\u308b<\/h3>\n\n\n\n<p>\u6a5f\u68b0\u5b66\u7fd2\u624b\u6cd5\u306e\u57fa\u672c\u306f\u3001\u5b66\u7fd2\u30c7\u30fc\u30bf\u3067\u4f5c\u3063\u305f\u30e2\u30c7\u30eb\u304c\u3001\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u3067\u3069\u306e\u304f\u3089\u3044\u4e88\u6e2c\u306b\u4f7f\u3048\u308b\u304b\u3092\u78ba\u8a8d\u3059\u308b\u3082\u306e\u3002<\/p>\n\n\n\n<p>\u3070\u3063\u3061\u308a\u4e88\u6e2c\u3067\u304d\u308b\u306a\u3089\u3001\u4f5c\u3063\u305f\u30e2\u30c7\u30eb\u306f\u4f7f\u3048\u308b\u53ef\u80fd\u6027\u304c\u9ad8\u3044\u3002<\/p>\n\n\n\n<p>\u5b66\u7fd2\u30c7\u30fc\u30bf\u3067\u306f\u4e88\u6e2c\u3067\u304d\u308b\u304c\u3001\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u306b\u306a\u3063\u305f\u3089\u9014\u7aef\u306b\u4e88\u6e2c\u3067\u304d\u306a\u304f\u306a\u308b\u3088\u3046\u306a\u3089\u3001\u6c4e\u7528\u6027\u306f\u4f4e\u304f\u4f7f\u3048\u308b\u30e2\u30c7\u30eb\u3067\u306f\u306a\u3044\u3002<\/p>\n\n\n\n<p>1990 \u5e74\u304b\u30892008 \u5e74\u307e\u3067\u3092\u5b66\u7fd2\u30c7\u30fc\u30bf\u3001\u6b8b\u308a\uff082009 \u30682010 \u5e74\uff09\u3092\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u3068\u3059\u308b\u3002<\/p>\n\n\n\n<p>Direction \u3092Lag2\u3060\u3051\u3067\u63a8\u6e2c\u3059\u308b\u30ed\u30b8\u30b9\u30c6\u30a3\u30c3\u30af\u56de\u5e30\u30e2\u30c7\u30eb\u3067\u5206\u6790\u3057\u3066\u307f\u308b\u3002\u6b63\u7b54\u7387\u306f\u6df7\u5408\u884c\u5217\u3067\u8a08\u7b97\u3059\u308b\u3002<\/p>\n\n\n\n<p>\u307e\u305a\u3001training\u7528\u3068test\u7528\u306eData frame\u306b\u5206\u3051\u308b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Weekly.tr &lt;- subset (Weekly, Weekly$Year &lt; 2009)\nWeekly.te &lt;- subset (Weekly, Weekly$Year &gt;= 2009)<\/code><\/pre>\n\n\n\n<p>training\u7528\u306e\u30c7\u30fc\u30bf\u3067logistic regression model\u3092\u4f5c\u6210\u3059\u308b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt; res.tr &lt;- glm(Direction ~ Lag2, family=binomial, data=Weekly.tr)\n&gt;\n&gt; summary(res.tr)\nCall:\nglm(formula = Direction ~ Lag2, family = binomial, data = Weekly.tr)\nDeviance Residuals:\nMin      1Q  Median      3Q     Max\n-1.536  -1.264   1.021   1.091   1.368\nCoefficients:\nEstimate Std. Error z value Pr(&gt;|z|)\n(Intercept)  0.20326    0.06428   3.162  0.00157 **\nLag2         0.05810    0.02870   2.024  0.04298 *\n---\nSignif. codes:  0 \u2018***\u2019 0.001 \u2018**\u2019 0.01 \u2018*\u2019 0.05 \u2018.\u2019 0.1 \u2018 \u2019 1\n(Dispersion parameter for binomial family taken to be 1)\nNull deviance: 1354.7  on 984  degrees of freedom\nResidual deviance: 1350.5  on 983  degrees of freedom\nAIC: 1354.5\nNumber of Fisher Scoring iterations: 4\n<\/code><\/pre>\n\n\n\n<p>test\u7528\u30c7\u30fc\u30bf\u3092\u7528\u3044\u3066\u3044\u307e\u4f5c\u3063\u305f\u30e2\u30c7\u30eb\u304b\u3089\u4e88\u6e2c\u5024\u3092\u7b97\u51fa\u3059\u308b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt; pre.te &lt;- predict(res.tr, newdata=Weekly.te, type=\"response\")\n&gt;\n&gt; summary(pre.te)\nMin. 1st Qu.  Median    Mean 3rd Qu.    Max.\n0.4488  0.5331  0.5573  0.5555  0.5826  0.6954\n<\/code><\/pre>\n\n\n\n<p>\u6df7\u540c\u884c\u5217confusion matrix\u3092\u4f5c\u6210\u3057\u3001\u6b63\u7b54\u7387 accuracy\u3092\u8a08\u7b97\u3059\u308b\u300262.5%\u3068\u8a08\u7b97\u3055\u308c\u305f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt; (tab.te &lt;- table(Weekly.te$Direction, pre.te&gt;0.5))\nFALSE TRUE\nDown     9   34\nUp       5   56\n&gt;\n&gt; sum(diag(prop.table(tab.te)))*100\n&#91;1] 62.5\n<\/code><\/pre>\n\n\n\n<div id=\"biost-56866047\" class=\"biost- biost-entity-placement\"><p style=\"text-align: center;\"><span style=\"font-size: 20px;\"><strong><a href=\"https:\/\/best-biostatistics.com\/kmhl\">\uff1e\uff1e\u3082\u3046\u7d71\u8a08\u3067\u60a9\u3080\u306e\u306f\u7d42\u308f\u308a\u306b\u3057\u307e\u305b\u3093\u304b\uff1f\u00a0<\/a><\/strong><\/span><\/p>\r\n<a href=\"https:\/\/best-biostatistics.com\/kmhl\"><img class=\"aligncenter wp-image-2794 size-full\" src=\"https:\/\/best-biostatistics.com\/wp\/wp-content\/uploads\/2023\/11\/bn_r_03.png\" alt=\"\" width=\"500\" height=\"327\" \/><\/a>\r\n<p style=\"text-align: center;\"><span style=\"color: #ff0000; font-size: 20px;\"><strong><span class=\"marker2\">\u21911\u4e07\u4eba\u4ee5\u4e0a\u306e\u533b\u7642\u5f93\u4e8b\u8005\u304c\u8cfc\u8aad\u4e2d<\/span><\/strong><\/span><\/p><\/div><h2 class=\"wp-block-heading\" id=\"R\u3067\u6a5f\u68b0\u5b66\u7fd2\u3092\u7dda\u5f62\u5224\u5225\u5206\u6790\u3067\u884c\u3046\u65b9\u6cd5\">R\u3067\u6a5f\u68b0\u5b66\u7fd2\u3092\u7dda\u5f62\u5224\u5225\u5206\u6790\u3067\u884c\u3046\u65b9\u6cd5<\/h2>\n\n\n\n<p>\u30ed\u30b8\u30b9\u30c6\u30a3\u30c3\u30af\u56de\u5e30\u3068\u4f3c\u305f\u65b9\u6cd5\u3067\u7dda\u5f62\u5224\u5225\u5206\u6790\u304c\u3042\u308b\u3002\u7dda\u5f62\u5224\u5225\u5206\u6790\u3067\u540c\u69d8\u306b\u4e88\u6e2c\u3055\u305b\u308b\u3068\u3069\u3046\u306a\u308b\u304b\uff1f<\/p>\n\n\n\n<p>\u7dda\u5f62\u5224\u5225\u5206\u6790\u306e\u95a2\u6570lda()\u306fMASS\u30d1\u30c3\u30b1\u30fc\u30b8\u306b\u3042\u308b\u306e\u3067\u3001MASS\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u547c\u3073\u51fa\u3057\u3066\u304b\u3089\u4f7f\u7528\u3059\u308b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>library(MASS)\nres.lda.tr &lt;- lda(Direction ~ Lag2, data=Weekly.tr)\npre.lda.te &lt;- predict(res.lda.tr, newdata=Weekly.te)\n(tab.lda.te &lt;- table(Weekly.te$Direction, pre.lda.te$class))\nsum(diag(prop.table(tab.lda.te)))*100<\/code><\/pre>\n\n\n\n<p>\u6b63\u7b54\u7387\u306f\u3001\u30ed\u30b8\u30b9\u30c6\u30a3\u30c3\u30af\u56de\u5e30\u3068\u540c\u305862.5%\u3068\u306a\u3063\u305f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt; (tab.lda.te &lt;- table(Weekly.te$Direction, pre.lda.te$class))\nDown Up\nDown    9 34\nUp      5 56\n&gt;\n&gt; sum(diag(prop.table(tab.lda.te)))*100\n&#91;1] 62.5\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"R\u3067\u6a5f\u68b0\u5b66\u7fd2\u3092\u4e8c\u6b21\u5224\u5225\u5206\u6790\u3067\u884c\u3046\u65b9\u6cd5\">R\u3067\u6a5f\u68b0\u5b66\u7fd2\u3092\u4e8c\u6b21\u5224\u5225\u5206\u6790\u3067\u884c\u3046\u65b9\u6cd5<\/h2>\n\n\n\n<p>\u4e8c\u6b21\u5224\u5225\u5206\u6790\u306f\u3001\u7dda\u5f62\u5224\u5225\u5206\u6790\u3067\u5fc5\u8981\u3060\u3063\u305f\u7b49\u5206\u6563\u6027\u304c\u8981\u3089\u306a\u3044\u3068\u3044\u3046\u5229\u70b9\u304c\u3042\u308b\u3002<\/p>\n\n\n\n<p>\u4e8c\u6b21\u5224\u5225\u5206\u6790\u3067\u6b63\u7b54\u7387\u3092\u898b\u3066\u307f\u308b\u3002MASS\u30d1\u30c3\u30b1\u30fc\u30b8\u306eqda()\u3092\u4f7f\u3046\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>res.qda.tr &lt;- qda(Direction ~ Lag2, data=Weekly.tr)\npre.qda.te &lt;- predict(res.qda.tr, newdata=Weekly.te)\n(tab.qda.te &lt;- table(Weekly.te$Direction, pre.qda.te$class))\nsum(diag(prop.table(tab.qda.te)))*100&nbsp;\n<\/code><\/pre>\n\n\n\n<p>Down\u306b\u4e00\u4ef6\u3082\u6b63\u3057\u304f\u5224\u5225\u3067\u304d\u305a\u3001\u6b63\u7b54\u7387\u306f58.7%\u306b\u4e0b\u304c\u3063\u305f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt; (tab.qda.te &lt;- table(Weekly.te$Direction, pre.qda.te$class))\nDown Up\nDown    0 43\nUp      0 61\n&gt;\n&gt; sum(diag(prop.table(tab.qda.te)))*100\n&#91;1] 58.65385\n<\/code><\/pre>\n\n\n\n<p>\u53c2\u8003\uff1a<\/p>\n\n\n\n<p><a href=\"https:\/\/www.datacamp.com\/doc\/r\/discriminant\">https:\/\/www.datacamp.com\/doc\/r\/discriminant<\/a><\/p>\n\n\n\n\n\n\n\n<h2 class=\"wp-block-heading\" id=\"R\u3067\u6a5f\u68b0\u5b66\u7fd2\u3092-k-\u8fd1\u508d\u6cd5\u3067\u884c\u3046\u65b9\u6cd5\">R\u3067\u6a5f\u68b0\u5b66\u7fd2\u3092 k \u8fd1\u508d\u6cd5\u3067\u884c\u3046\u65b9\u6cd5<\/h2>\n\n\n\n<p>k \u8fd1\u508d\u6cd5\u306f\u3001\u5224\u5225\u3057\u305f\u3044\u65b0\u3057\u3044\u30c7\u30fc\u30bf\u306e\u8fd1\u508d\u306b\u3042\u308bk\u500b\u30c7\u30fc\u30bf\u304b\u3089\u3001\u3069\u306e\u30b0\u30eb\u30fc\u30d7\u306b\u5272\u308a\u632f\u3063\u305f\u3089\uff08\u5224\u5225\u3057\u305f\u3089\uff09\u3044\u3044\u304b\u3092\u8003\u3048\u308b\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>k\u30921\u500b\u30012\u500b\u30013\u500b\u3068\u5897\u3084\u3057\u3066\u3001\u5224\u5225\u304c\u6b63\u89e3\u3059\u308b\u7387\u3092\u6bd4\u8f03\u3059\u308b\u3002<\/p>\n\n\n\n<p>class\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u7528\u3044\u308b\u3002class\u30d1\u30c3\u30b1\u30fc\u30b8\u306f\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u6e08\u307f\u3060\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>library(class)<\/code><\/pre>\n\n\n\n<p>\u30bf\u30a4\u306e\u30c7\u30fc\u30bf\u304c\u3042\u3063\u305f\u5834\u5408\u30e9\u30f3\u30c0\u30e0\u306b\u5206\u3051\u308b\u305f\u3081\u3001\u7b54\u3048\u304c\u6bce\u56de\u5909\u308f\u308b\u3002<\/p>\n\n\n\n<p>\u7b54\u3048\u304c\u6bce\u56de\u5909\u308f\u308b\u306e\u3092\u9632\u6b62\u3059\u308b\u305f\u3081\u306b\u3001set.seed()\u3092\u8a2d\u5b9a\u3059\u308b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"k1\u306e\u5834\u5408\">k=1\u306e\u5834\u5408<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>set.seed(20180902)\nres.knn.te1 &lt;- knn(train=data.frame(Weekly.tr$Lag2), test=data.frame(Weekly.te$Lag2), cl=Weekly.tr$Direction, k=1)\nsummary(res.knn.te1)\n(tab.knn.te1 &lt;- table(Weekly.te$Direction,res.knn.te1))\nsum(diag(prop.table(tab.knn.te1)))*100<\/code><\/pre>\n\n\n\n<p>\u6b63\u7b54\u7387\u306f51.0%\u3067\u3042\u3063\u305f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt; (tab.knn.te1 &lt;- table(Weekly.te$Direction,res.knn.te1))\nres.knn.te1\nDown Up\nDown   21 22\nUp     29 32\n&gt;\n&gt; sum(diag(prop.table(tab.knn.te1)))*100\n&#91;1] 50.96154\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"k2\u306e\u5834\u5408\">k=2\u306e\u5834\u5408<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>set.seed(20180902)\nres.knn.te2 &lt;- knn(train=data.frame(Weekly.tr$Lag2), test=data.frame(Weekly.te$Lag2), cl=Weekly.tr$Direction, k=2)\nsummary(res.knn.te2)\n(tab.knn.te2 &lt;- table(Weekly.te$Direction,res.knn.te2))\nsum(diag(prop.table(tab.knn.te2)))*100<\/code><\/pre>\n\n\n\n<p>\u6b63\u7b54\u7387\u306f53.8%\u306b\u4e0a\u6607\u3057\u305f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt; (tab.knn.te2 &lt;- table(Weekly.te$Direction,res.knn.te2))\nres.knn.te2\nDown Up\nDown   22 21\nUp     27 34\n&gt;\n&gt; sum(diag(prop.table(tab.knn.te2)))*100\n&#91;1] 53.84615\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"k3\u306e\u5834\u5408\">k=3\u306e\u5834\u5408<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>set.seed(20180902)\nres.knn.te3 &lt;- knn(train=data.frame(Weekly.tr$Lag2), test=data.frame(Weekly.te$Lag2), cl=Weekly.tr$Direction, k=3)\nsummary(res.knn.te3)\n(tab.knn.te3 &lt;- table(Weekly.te$Direction,res.knn.te3))\nsum(diag(prop.table(tab.knn.te3)))*100<\/code><\/pre>\n\n\n\n<p>\u6b63\u7b54\u738755.8%\u306b\u306a\u3063\u305f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt; (tab.knn.te3 &lt;- table(Weekly.te$Direction,res.knn.te3))\nres.knn.te3\nDown Up\nDown   16 27\nUp     19 42\n&gt;\n&gt; sum(diag(prop.table(tab.knn.te3)))*100\n&#91;1] 55.76923\n<\/code><\/pre>\n\n\n\n<p>\u53c2\u8003\uff1a<\/p>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/19396947\/how-can-i-resolve-the-following-dimension-mismatch-with-rs-k-nearest-neighbors\">https:\/\/stackoverflow.com\/questions\/19396947\/how-can-i-resolve-the-following-dimension-mismatch-with-rs-k-nearest-neighbors<\/a><\/p>\n\n\n\n\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u307e\u3068\u3081\">\u307e\u3068\u3081<\/h2>\n\n\n\n<p>\u7d71\u8a08\u30bd\u30d5\u30c8R\u306eISLR\u30d1\u30c3\u30b1\u30fc\u30b8Weekly\u30c7\u30fc\u30bf\u3092\u7528\u3044\u3066\u3001\u5b66\u7fd2\u30bb\u30c3\u30c8\u3067\u4f5c\u6210\u3057\u305f\u30e2\u30c7\u30eb\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u3067\u691c\u8a3c\u3059\u308b\u6a5f\u68b0\u5b66\u7fd2\u3092\u884c\u3063\u305f\u3002<\/p>\n\n\n\n<p>\u30ed\u30b8\u30b9\u30c6\u30a3\u30c3\u30af\u56de\u5e30\u3001\u7dda\u5f62\u5224\u5225\u5206\u6790\u3001\u4e8c\u6b21\u5224\u5225\u5206\u6790\u3001k\u8fd1\u508d\u6cd5\u3092\u6bd4\u8f03\u3057\u305f\u3002<\/p>\n\n\n\n<p>\u7d50\u679c\u3068\u3057\u3066\u3001\u30ed\u30b8\u30b9\u30c6\u30a3\u30c3\u30af\u56de\u5e30\u3068\u7dda\u5f62\u5224\u5225\u5206\u6790\u304c\u540c\u7387\u4e00\u4f4d\uff08\u6b63\u7b54\u738762.5%\uff09\u3067\u3042\u3063\u305f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6a5f\u68b0\u5b66\u7fd2\u3067\u3001\u3088\u308a\u3088\u304f\u63a8\u6e2c\u3067\u304d\u308b\u30e2\u30c7\u30eb\u3092\u9078\u3076\u3002 \u7d71\u8a08\u30bd\u30d5\u30c8R\u306eISLR\u30d1\u30c3\u30b1\u30fc\u30b8\u306eWeekly\u30c7\u30fc\u30bf\u3067\u57fa\u790e\u7684\u306a\u6a5f\u68b0\u5b66\u7fd2\u3092\u884c\u3063\u3066\u307f\u305f\u3002<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"swell_btn_cv_data":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5,103],"tags":[],"class_list":["post-475","post","type-post","status-publish","format-standard","hentry","category-r","category-103"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/best-biostatistics.com\/toukei-er\/wp-json\/wp\/v2\/posts\/475","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/best-biostatistics.com\/toukei-er\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/best-biostatistics.com\/toukei-er\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/best-biostatistics.com\/toukei-er\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/best-biostatistics.com\/toukei-er\/wp-json\/wp\/v2\/comments?post=475"}],"version-history":[{"count":2,"href":"https:\/\/best-biostatistics.com\/toukei-er\/wp-json\/wp\/v2\/posts\/475\/revisions"}],"predecessor-version":[{"id":2768,"href":"https:\/\/best-biostatistics.com\/toukei-er\/wp-json\/wp\/v2\/posts\/475\/revisions\/2768"}],"wp:attachment":[{"href":"https:\/\/best-biostatistics.com\/toukei-er\/wp-json\/wp\/v2\/media?parent=475"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/best-biostatistics.com\/toukei-er\/wp-json\/wp\/v2\/categories?post=475"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/best-biostatistics.com\/toukei-er\/wp-json\/wp\/v2\/tags?post=475"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}