サイトに置いてあるプログラムを少しアップデート。
要は「基準になる変数」がアイテムデータだと、関数内部でダミー変数化しちゃうので、指定する列番号も変わっちゃうという問題がありました。
対応したけど、考え方を変えて、素直にダミー化するのは外部でやってもらうようにしました。
その方がシンプルだし、やる方も何をやっているかわかりやすくて良いと思うから。
備忘録的に、対応した版をブログに乗せておきます。
サイトに載ってるのはごくシンプルなヤツです。最初からこうすりゃよかったよ(笑)
FC <span class="synStatement"><-</span> <span class="synType">function</span><span class="synSpecial">(</span> data<span class="synSpecial">,</span> criteria<span class="synSpecial">,</span> method=c<span class="synSpecial">(</span><span class="synConstant">"corresp"</span><span class="synSpecial">,</span><span class="synConstant">"dual"</span><span class="synSpecial">),</span> itemdata=<span class="synConstant">FALSE</span><span class="synSpecial">)</span> <span class="synSpecial">{</span> <span class="synComment">#アイテムデータの場合は、青木先生のカテゴリーデータ変換関数を借りてダミー変数にしておく</span> <span class="synComment"># この場合は基準変数が数字or変数名で入っているので、青木先生のmake.dummy関数に少し手を入れる</span> <span class="synStatement">if</span><span class="synSpecial">(</span>itemdata<span class="synSpecial">){</span> dat <span class="synStatement"><-</span> data ct <span class="synStatement"><-</span> criteria ncat <span class="synStatement"><-</span> ncol<span class="synSpecial">(</span>dat<span class="synSpecial">)</span> dat<span class="synSpecial">[,</span> <span class="synConstant">1</span><span class="synSpecial">:</span>ncat<span class="synSpecial">]</span> <span class="synStatement"><-</span> lapply<span class="synSpecial">(</span>dat<span class="synSpecial">,</span> <span class="synType">function</span><span class="synSpecial">(</span>x<span class="synSpecial">)</span> <span class="synSpecial">{</span> <span class="synStatement">if</span> <span class="synSpecial">(</span>is.factor<span class="synSpecial">(</span>x<span class="synSpecial">))</span> <span class="synSpecial">{</span> <span class="synStatement">return</span><span class="synSpecial">(</span>as.integer<span class="synSpecial">(</span>x<span class="synSpecial">))</span> <span class="synSpecial">}</span><span class="synStatement">else</span> <span class="synSpecial">{</span> <span class="synStatement">return</span><span class="synSpecial">(</span>x<span class="synSpecial">)</span> <span class="synSpecial">}</span> <span class="synSpecial">})</span> mx <span class="synStatement"><-</span> sapply<span class="synSpecial">(</span>dat<span class="synSpecial">,</span> max<span class="synSpecial">)</span> <span class="synComment">#ここを変えました。基準となる変数の列数をカウントするために</span> <span class="synStatement">if</span><span class="synSpecial">(</span>min<span class="synSpecial">(</span>ct<span class="synSpecial">)</span>==<span class="synConstant">1</span><span class="synSpecial">){</span>mnct <span class="synStatement"><-</span> <span class="synConstant">1</span><span class="synSpecial">}</span><span class="synStatement">else</span><span class="synSpecial">{</span>mnct <span class="synStatement"><-</span> sum<span class="synSpecial">(</span>mx<span class="synSpecial">[</span><span class="synConstant">1</span><span class="synSpecial">:</span>min<span class="synSpecial">(</span>ct<span class="synSpecial">)</span>-<span class="synConstant">1</span><span class="synSpecial">])</span>+<span class="synConstant">1</span><span class="synSpecial">}</span> mxct <span class="synStatement"><-</span> sum<span class="synSpecial">(</span>mx<span class="synSpecial">[</span><span class="synConstant">1</span><span class="synSpecial">:</span>max<span class="synSpecial">(</span>ct<span class="synSpecial">)])</span> criteria <span class="synStatement"><-</span> c<span class="synSpecial">(</span>mnct<span class="synSpecial">:</span>mxct<span class="synSpecial">)</span> start <span class="synStatement"><-</span> c<span class="synSpecial">(</span><span class="synConstant">0</span><span class="synSpecial">,</span> cumsum<span class="synSpecial">(</span>mx<span class="synSpecial">)[</span><span class="synConstant">1</span><span class="synSpecial">:(</span>ncat-<span class="synConstant">1</span><span class="synSpecial">)])</span> nobe <span class="synStatement"><-</span> sum<span class="synSpecial">(</span>mx<span class="synSpecial">)</span> retv <span class="synStatement"><-</span> t<span class="synSpecial">(</span>apply<span class="synSpecial">(</span>dat<span class="synSpecial">,</span> <span class="synConstant">1</span><span class="synSpecial">,</span> <span class="synType">function</span><span class="synSpecial">(</span>obs<span class="synSpecial">)</span> <span class="synSpecial">{</span> zeros <span class="synStatement"><-</span> <span class="synType">numeric</span><span class="synSpecial">(</span>nobe<span class="synSpecial">)</span> zeros<span class="synSpecial">[</span>start+obs<span class="synSpecial">]</span> <span class="synStatement"><-</span> <span class="synConstant">1</span> zeros <span class="synSpecial">}</span> <span class="synSpecial">))</span> data <span class="synStatement"><-</span> retv <span class="synSpecial">}</span> Fj <span class="synStatement"><-</span> data<span class="synSpecial">[,</span>criteria<span class="synSpecial">]</span> <span class="synComment">#分類に使う変数列Fjを取り出す</span> F0 <span class="synStatement"><-</span> data<span class="synSpecial">[,</span>-criteria<span class="synSpecial">]</span> <span class="synComment"># F-Fj=F0行列を作る</span> C <span class="synStatement"><-</span> t<span class="synSpecial">(</span>Fj<span class="synSpecial">)</span>%*%F0 <span class="synComment">#分析する行列</span> <span class="synComment"># MASSライブラリのcorresp関数を使わせてもらう場合</span> <span class="synStatement">if</span><span class="synSpecial">(</span>method==<span class="synConstant">"corresp"</span><span class="synSpecial">){</span> library<span class="synSpecial">(</span>MASS<span class="synSpecial">)</span> result <span class="synStatement"><-</span> corresp<span class="synSpecial">(</span>C<span class="synSpecial">,</span>nf=min<span class="synSpecial">(</span>ncol<span class="synSpecial">(</span>C<span class="synSpecial">),</span>nrow<span class="synSpecial">(</span>C<span class="synSpecial">))</span>-<span class="synConstant">1</span><span class="synSpecial">)</span> <span class="synSpecial">}</span> <span class="synComment">#青木先生の双対尺度法関数を使わせてもらう場合</span> <span class="synStatement">if</span><span class="synSpecial">(</span>method==<span class="synConstant">"dual"</span><span class="synSpecial">){</span> source<span class="synSpecial">(</span><span class="synConstant">"http://aoki2.si.gunma-u.ac.jp/R/src/dual.R"</span><span class="synSpecial">,</span> encoding=<span class="synConstant">"<a class="keyword" href="http://d.hatena.ne.jp/keyword/euc">euc</a>-jp"</span><span class="synSpecial">)</span> source<span class="synSpecial">(</span><span class="synConstant">"http://aoki2.si.gunma-u.ac.jp/R/src/summary.dual.R"</span><span class="synSpecial">,</span> encoding=<span class="synConstant">"<a class="keyword" href="http://d.hatena.ne.jp/keyword/euc">euc</a>-jp"</span><span class="synSpecial">)</span> source<span class="synSpecial">(</span><span class="synConstant">"http://aoki2.si.gunma-u.ac.jp/R/src/plot.dual.R"</span><span class="synSpecial">,</span> encoding=<span class="synConstant">"<a class="keyword" href="http://d.hatena.ne.jp/keyword/euc">euc</a>-jp"</span><span class="synSpecial">)</span> result <span class="synStatement"><-</span> dual<span class="synSpecial">(</span>C<span class="synSpecial">)</span> <span class="synSpecial">}</span> <span class="synStatement">return</span><span class="synSpecial">(</span>result<span class="synSpecial">)</span> <span class="synError">}</span>