{"id":486,"date":"2018-08-30T20:04:24","date_gmt":"2018-08-30T11:04:24","guid":{"rendered":"https:\/\/best-biostatistics.com\/toukei-er\/entry\/variable-selection-in-linear-regression-using-bestglm-in-r\/"},"modified":"2024-10-13T15:11:24","modified_gmt":"2024-10-13T06:11:24","slug":"variable-selection-in-linear-regression-using-bestglm-in-r","status":"publish","type":"post","link":"https:\/\/best-biostatistics.com\/toukei-er\/entry\/variable-selection-in-linear-regression-using-bestglm-in-r\/","title":{"rendered":"R \u3067\u91cd\u56de\u5e30\u5206\u6790\u306e\u5909\u6570\u9078\u629e\u306b\u53c2\u8003\u3068\u306a\u308b\u8a08\u7b97\u4e0a\u30d9\u30b9\u30c8\u306a\u5909\u6570\u30bb\u30c3\u30c8\u3092\u63d0\u6848\u3057\u3066\u304f\u308c\u308b\u65b9\u6cd5"},"content":{"rendered":"\n<p>R \u3067\u91cd\u56de\u5e30\u5206\u6790\u3092\u884c\u3063\u305f\u969b\u306e\u5909\u6570\u9078\u629e\u306e\u65b9\u6cd5\u306e\u89e3\u8aac\u3002<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\" id=\"bestglm\u306e\u6e96\u5099\u3068\u30b5\u30f3\u30d7\u30eb\u30c7\u30fc\u30bf\">bestglm\u306e\u6e96\u5099\u3068\u30b5\u30f3\u30d7\u30eb\u30c7\u30fc\u30bf<\/h2>\n\n\n\n<p>R \u306e bestglm() \u95a2\u6570\u306f\u3001AIC, BIC(\u30c7\u30d5\u30a9\u30eb\u30c8), BICq\u306a\u3069\u306e Information Criterion \u60c5\u5831\u898f\u6e96\u3092\u4f7f\u3063\u3066 \u30d9\u30b9\u30c8\u306e\u5909\u6570\u306e\u7d44\u307f\u5408\u308f\u305b\u3092\u898b\u3064\u3051\u3066\u304f\u308c\u308b\u3002<\/p>\n\n\n\n<p>bestglm\u30d1\u30c3\u30b1\u30fc\u30b8\u306eznuclear\u30c7\u30fc\u30bf\u3067\u8a66\u3057\u3066\u307f\u308b\u3002<\/p>\n\n\n\n<p>\u4e8b\u524d\u306b\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3066\u304a\u3044\u305f\u3042\u3068 bestglm\u3092\u547c\u3073\u51fa\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>install.packages(\"bestglm\")<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>library(bestglm)<\/code><\/pre>\n\n\n\n<p>znuclear\u30c7\u30fc\u30bf\u30d5\u30ec\u30fc\u30e0\u306f\u3001\u539f\u767a\u306e\u30c7\u30fc\u30bf\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Data on 32 nuclear power plants. The response variable is cost and there are ten covariates.<\/p>\n<\/blockquote>\n\n\n\n<p>bestglm\u306e\u5834\u5408\u3001data.frame\u5185\u3067\u3001 \u30ab\u30e9\u30e0\u306e\u9806\u756a\u306b\u6c7a\u307e\u308a\u304c\u3042\u308b\u3002<\/p>\n\n\n\n<p>\u307e\u305a\u72ec\u7acb\u5909\u6570\u3092\u4e26\u3079\u3066\u3001 \u4e00\u756a\u6700\u5f8c\u306e\u30ab\u30e9\u30e0\u306b\u5f93\u5c5e\u5909\u6570\u3092\u7f6e\u304f\u3002 &nbsp;<\/p>\n\n\n\n<p>\u3053\u306edata.frame\u3067\u306fcost\u304c\u5f93\u5c5e\u5909\u6570\u3060\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u9806\u756a\u306b\u4e26\u3093\u3067\u3044\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002<\/p>\n\n\n\n<p>\u53f3\u7aef\u304ccost\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>> round(znuclear, 2)\n    date    T1    T2 capacity PR NE CT BW     N PT  cost\n1  68.58  0.19 -1.72    -0.62  0  1  0  0  0.89  0  0.17\n2  67.33 -1.17  1.01     1.14  0  0  1  0 -1.80  0  0.13\n3  67.33 -1.17  1.91     1.14  1  0  1  0 -1.80  0  0.07\n4  68.00 -0.79  0.50     1.14  0  1  1  0  0.73  0  1.09\n5  68.00 -0.79  1.40     1.14  1  1  1  0  0.73  0  1.05\n6  67.92 -0.11 -1.11    -1.79  0  1  1  0 -0.68  0 -0.59\n7  68.17 -0.43 -1.23     0.10  0  0  0  0 -0.16  0 -1.22\n8  68.42  0.19 -0.25    -2.26  0  0  0  0 -1.80  0 -0.81\n9  68.42  0.47 -0.66     0.10  1  0  0  0 -0.16  0  0.15\n10 68.33 -0.43  0.85    -0.05  0  1  1  1 -1.09  0  1.24\n11 68.58 -0.43  0.23    -1.45  0  0  0  0 -0.68  0 -0.55\n12 68.75 -0.11 -1.59    -0.06  0  1  0  0  0.02  0 -0.18\n13 68.42  0.47  0.05    -1.67  0  0  1  0 -1.09  0 -0.12\n14 68.92  0.98 -1.00     1.08  0  0  0  0  0.18  0  0.37\n15 68.92 -0.11  0.32     0.23  0  0  0  1  1.02  0 -0.24\n16 68.42 -0.79  0.50    -0.12  0  0  0  0 -0.68  0 -0.05\n17 69.50  1.21 -0.15     0.21  0  1  0  0  1.08  0  1.33\n18 68.42  0.47  1.25    -1.67  1  0  1  0 -1.09  0 -1.05\n19 69.17  0.47  0.50     1.23  0  0  0  0 -1.80  0  1.89\n20 68.92  0.73 -0.25     1.08  1  0  0  0  0.32  0  0.34\n21 68.75 -0.79  0.76     0.52  0  0  1  1  0.96  0  0.73\n22 70.92  2.02 -0.45     0.13  1  1  0  0  1.25  0  1.15\n23 69.67  0.73 -0.25    -0.08  0  0  1  0  1.14  0  0.97\n24 70.08  1.43 -0.35     0.09  1  0  0  0 -0.68  0  0.91\n25 70.42  1.43 -1.98    -1.61  0  0  1  0  1.20  0  0.25\n26 71.08  1.64 -0.45     1.38  0  0  1  0  1.30  0  1.27\n27 67.25 -0.11  0.14    -0.30  0  0  0  0  0.32  1 -1.94\n28 67.17 -1.60 -1.47     0.09  0  0  1  0  0.18  1 -1.07\n29 67.83 -0.43  0.14     0.40  0  0  0  1  0.64  1 -1.10\n30 67.83 -0.43  0.85     0.40  1  0  0  1  0.64  1 -1.14\n31 67.25 -0.11  0.93    -0.30  1  0  0  0  0.32  1 -1.81\n32 67.83 -2.62  1.55     0.40  1  0  0  1  0.64  1 -1.23\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"bestglm\u306e\u5b9f\u884c\u3068\u7d50\u679c\">bestglm\u306e\u5b9f\u884c\u3068\u7d50\u679c<\/h2>\n\n\n\n<p>bestglm()\u306e\u7d50\u679c\u3092\u898b\u3066\u307f\u308b\u3002<\/p>\n\n\n\n<p>BIC\u3067\u691c\u8a0e\u3057\u305fBest Model\u304c\u8868\u793a\u3055\u308c\u308b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>> out &lt;- bestglm(znuclear)\n> out\nBIC\nBICq equivalent for q in (0.349204366418933, 0.716418902103362)\nBest Model:\n               Estimate Std. Error   t value     Pr(>|t|)\n(Intercept) -38.7480703 7.91826983 -4.893502 4.910313e-05\ndate          0.5620284 0.11445901  4.910303 4.701224e-05\ncapacity      0.4759804 0.07818015  6.088252 2.310934e-06\nNE            0.6588957 0.19616044  3.358963 2.510375e-03\nCT            0.3714664 0.15987847  2.323430 2.858187e-02\nN            -0.2277672 0.10786682 -2.111560 4.489115e-02\nPT           -0.5982476 0.30044058 -1.991235 5.748951e-02\n<\/code><\/pre>\n\n\n\n<p>summary()\u3067Best model\u306e\u30e2\u30c7\u30eb\u306e\u6709\u610f\u6027\u304c\u78ba\u8a8d\u3067\u304d\u308b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>> summary(out)\nFitting algorithm:  BIC-leaps\nBest Model:\ndf  deviance\nNull Model 25  4.436699\nFull Model 31 31.000000\nlikelihood-ratio test - GLM\ndata:  H0: Null Model vs. H1: Best Fit BIC-leaps\nX = 26.563, df = 6, p-value = 0.0001748<\/code><\/pre>\n\n\n\n<p>out$Subsets\u3067\u3001\u691c\u8a0e\u3057\u305f\u30e2\u30c7\u30eb\u4e00\u89a7\u304c\u8868\u793a\u3055\u308c\u308b\u3002<\/p>\n\n\n\n<p>\uff0a\u304cBest model\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>> out$Subsets\n    (Intercept)  date    T1    T2 capacity    PR    NE    CT    BW     N    PT logLikelihood        BIC\n0          TRUE FALSE FALSE FALSE    FALSE FALSE FALSE FALSE FALSE FALSE FALSE     0.5079792  -1.015958\n1          TRUE FALSE FALSE FALSE    FALSE FALSE FALSE FALSE FALSE FALSE  TRUE    10.2059259 -16.946116\n2          TRUE FALSE FALSE FALSE     TRUE FALSE FALSE FALSE FALSE FALSE  TRUE    17.8241085 -28.716745\n3          TRUE  TRUE FALSE FALSE     TRUE FALSE FALSE FALSE FALSE FALSE  TRUE    23.3113617 -36.225516\n4          TRUE  TRUE FALSE FALSE     TRUE FALSE  TRUE FALSE FALSE FALSE  TRUE    26.6826218 -39.502300\n5          TRUE  TRUE FALSE FALSE     TRUE FALSE  TRUE  TRUE FALSE  TRUE FALSE    29.2577991 -41.186919\n6*         TRUE  TRUE FALSE FALSE     TRUE FALSE  TRUE  TRUE FALSE  TRUE  TRUE    31.6132054 -42.431995\n7          TRUE  TRUE FALSE FALSE     TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE    32.1063164 -39.952482\n8          TRUE  TRUE FALSE  TRUE     TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE    33.2254075 -38.724928\n9          TRUE  TRUE  TRUE  TRUE     TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE    33.2836564 -35.375690\n10         TRUE  TRUE  TRUE  TRUE     TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE    33.3647536 -32.072148\n<\/code><\/pre>\n\n\n\n<p>&nbsp; out$BestModels\u3067\u3001Best\u304b\u30895\u756a\u76ee\u307e\u3067\u304c\u8868\u793a\u3055\u308c\u308b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>> out$BestModels\n  date    T1    T2 capacity    PR   NE   CT    BW     N    PT Criterion\n1 TRUE FALSE FALSE     TRUE FALSE TRUE TRUE FALSE  TRUE  TRUE -42.43200\n2 TRUE FALSE FALSE     TRUE FALSE TRUE TRUE FALSE  TRUE FALSE -41.18692\n3 TRUE FALSE FALSE     TRUE FALSE TRUE TRUE FALSE FALSE  TRUE -40.64612\n4 TRUE FALSE FALSE     TRUE FALSE TRUE TRUE  TRUE  TRUE  TRUE -39.95248\n5 TRUE FALSE FALSE     TRUE  TRUE TRUE TRUE FALSE  TRUE  TRUE -39.84694\n<\/code><\/pre>\n\n\n\n<p>&nbsp; out$BestModel<span style=\"font-size: 1rem;\">\u3067\u3001Best model\u306ecoefficients\u3060\u3051\u304c\u8868\u793a\u3055\u308c\u308b\u3002<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt; out$BestModel\nCall:\nlm(formula = y ~ ., data = data.frame(Xy&#91;, c(bestset&#91;-1], FALSE),\ndrop = FALSE], y = y))\nCoefficients:\n(Intercept)         date     capacity           NE           CT            N           PT\n-38.7481       0.5620       0.4760       0.6589       0.3715      -0.2278      -0.5982\n<\/code><\/pre>\n\n\n\n<p>Best model\u306eresidual\u3092\u7528\u3044\u3066QQ plot\u3092\u63cf\u304f\u3053\u3068\u304c\u3067\u304d\u308b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>qqnorm(resid(out$BestModel), ylab=\"residuals, best model\")<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"587\" height=\"586\" src=\"https:\/\/best-biostatistics.com\/toukei-er\/wp-content\/uploads\/2018\/08\/20201005213955.png\" alt=\"\" class=\"wp-image-2785\" title=\"f:id:toukeier:20201005213955p:plain\" srcset=\"https:\/\/best-biostatistics.com\/toukei-er\/wp-content\/uploads\/2018\/08\/20201005213955.png 587w, https:\/\/best-biostatistics.com\/toukei-er\/wp-content\/uploads\/2018\/08\/20201005213955-300x300.png 300w, https:\/\/best-biostatistics.com\/toukei-er\/wp-content\/uploads\/2018\/08\/20201005213955-150x150.png 150w\" sizes=\"(max-width: 587px) 100vw, 587px\" \/><\/figure>\n\n\n\n\n\n\n\n<div id=\"biost-300335982\" 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=\"\u307e\u3068\u3081\">\u307e\u3068\u3081<\/h2>\n\n\n\n<p>\u91cd\u56de\u5e30\u5206\u6790\u306b\u304a\u3044\u3066\u60c5\u5831\u898f\u6e96\u306b\u3088\u3063\u3066\u30d9\u30b9\u30c8\u306e\u5909\u6570\u9078\u629e\u3092\u6559\u3048\u3066\u304f\u308c\u308b bestglm() \u306e\u4f7f\u3044\u65b9\u3092\u89e3\u8aac\u3057\u305f\u3002<\/p>\n\n\n\n<p>\u3082\u3061\u308d\u3093\u8a08\u7b97\u4e0a\u3082\u3063\u3068\u3082\u5f53\u3066\u306f\u307e\u308a\u306e\u3088\u3044\u30e2\u30c7\u30eb\u3092\u6559\u3048\u3066\u304f\u308c\u3066\u3044\u308b\u3060\u3051\u3067\u3001 \u5909\u6570\u306e\u9078\u629e\u306b\u306f\u5909\u6570\u306e\u610f\u5473\u5408\u3044\u306f\u5168\u304f\u52a0\u5473\u3055\u308c\u3066\u3044\u306a\u3044\u3002<\/p>\n\n\n\n<p>\u7814\u7a76\u8005\u306f\u3053\u306e\u7d50\u679c\u3092\u53c2\u8003\u306b\u3001 \u5148\u884c\u7814\u7a76\u306e\u7d50\u679c\u53ca\u3073\u7406\u5c48\u306e\u4e0a\u3067\u306e\u95a2\u9023\u6027\u3092\u7dcf\u5408\u3057\u3066\u3001\u81ea\u5206\u3067\u6700\u7d42\u89e3\u6790\u5909\u6570\u30bb\u30c3\u30c8\u3092\u6c7a\u3081\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>R \u3067\u91cd\u56de\u5e30\u5206\u6790\u3092\u884c\u3063\u305f\u969b\u306e\u5909\u6570\u9078\u629e\u306e\u65b9\u6cd5\u306e\u89e3\u8aac\u3002<\/p>\n","protected":false},"author":2,"featured_media":2785,"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,23,58],"tags":[],"class_list":["post-486","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-r","category-23","category-58"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/best-biostatistics.com\/toukei-er\/wp-content\/uploads\/2018\/08\/20201005213955.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/best-biostatistics.com\/toukei-er\/wp-json\/wp\/v2\/posts\/486","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=486"}],"version-history":[{"count":3,"href":"https:\/\/best-biostatistics.com\/toukei-er\/wp-json\/wp\/v2\/posts\/486\/revisions"}],"predecessor-version":[{"id":2787,"href":"https:\/\/best-biostatistics.com\/toukei-er\/wp-json\/wp\/v2\/posts\/486\/revisions\/2787"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/best-biostatistics.com\/toukei-er\/wp-json\/wp\/v2\/media\/2785"}],"wp:attachment":[{"href":"https:\/\/best-biostatistics.com\/toukei-er\/wp-json\/wp\/v2\/media?parent=486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/best-biostatistics.com\/toukei-er\/wp-json\/wp\/v2\/categories?post=486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/best-biostatistics.com\/toukei-er\/wp-json\/wp\/v2\/tags?post=486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}