ForcedClassificationのプログラムなんだけど

サイトに置いてあるプログラムを少しアップデート。
要は「基準になる変数」がアイテムデータだと、関数内部でダミー変数化しちゃうので、指定する列番号も変わっちゃうという問題がありました。

対応したけど、考え方を変えて、素直にダミー化するのは外部でやってもらうようにしました。
その方がシンプルだし、やる方も何をやっているかわかりやすくて良いと思うから。

備忘録的に、対応した版をブログに乗せておきます。
サイトに載ってるのはごくシンプルなヤツです。最初からこうすりゃよかったよ(笑)

FC <span class="synStatement">&lt;-</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">&quot;corresp&quot;</span><span class="synSpecial">,</span><span class="synConstant">&quot;dual&quot;</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">&lt;-</span> data
ct <span class="synStatement">&lt;-</span>   criteria
ncat <span class="synStatement">&lt;-</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">&lt;-</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">&lt;-</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">&lt;-</span> <span class="synConstant">1</span><span class="synSpecial">}</span><span class="synStatement">else</span><span class="synSpecial">{</span>mnct <span class="synStatement">&lt;-</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">&lt;-</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">&lt;-</span> c<span class="synSpecial">(</span>mnct<span class="synSpecial">:</span>mxct<span class="synSpecial">)</span>
start <span class="synStatement">&lt;-</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">&lt;-</span> sum<span class="synSpecial">(</span>mx<span class="synSpecial">)</span>
retv <span class="synStatement">&lt;-</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">&lt;-</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">&lt;-</span> <span class="synConstant">1</span>
zeros
<span class="synSpecial">}</span>
<span class="synSpecial">))</span>
data <span class="synStatement">&lt;-</span> retv
<span class="synSpecial">}</span>
Fj <span class="synStatement">&lt;-</span> data<span class="synSpecial">[,</span>criteria<span class="synSpecial">]</span>	<span class="synComment">#分類に使う変数列Fjを取り出す</span>
F0 <span class="synStatement">&lt;-</span> data<span class="synSpecial">[,</span>-criteria<span class="synSpecial">]</span>	<span class="synComment"># F-Fj=F0行列を作る</span>
C <span class="synStatement">&lt;-</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">&quot;corresp&quot;</span><span class="synSpecial">){</span>
library<span class="synSpecial">(</span>MASS<span class="synSpecial">)</span>
result <span class="synStatement">&lt;-</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">&quot;dual&quot;</span><span class="synSpecial">){</span>
source<span class="synSpecial">(</span><span class="synConstant">&quot;http://aoki2.si.gunma-u.ac.jp/R/src/dual.R&quot;</span><span class="synSpecial">,</span> encoding=<span class="synConstant">&quot;<a class="keyword" href="http://d.hatena.ne.jp/keyword/euc">euc</a>-jp&quot;</span><span class="synSpecial">)</span>
source<span class="synSpecial">(</span><span class="synConstant">&quot;http://aoki2.si.gunma-u.ac.jp/R/src/summary.dual.R&quot;</span><span class="synSpecial">,</span> encoding=<span class="synConstant">&quot;<a class="keyword" href="http://d.hatena.ne.jp/keyword/euc">euc</a>-jp&quot;</span><span class="synSpecial">)</span>
source<span class="synSpecial">(</span><span class="synConstant">&quot;http://aoki2.si.gunma-u.ac.jp/R/src/plot.dual.R&quot;</span><span class="synSpecial">,</span> encoding=<span class="synConstant">&quot;<a class="keyword" href="http://d.hatena.ne.jp/keyword/euc">euc</a>-jp&quot;</span><span class="synSpecial">)</span>
result <span class="synStatement">&lt;-</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>
日記
オンラインでもオフラインでも

後期が始まりました。 徐々に対面授業も解禁。後期の授業どうしますか,と聞かれたので一応対面で申し込ん …

日記
停電と漏電

夜にAmazon Prime Videoを見るのが最近のルーチン。昨日はジム・キャリーの「イエスマン …

日記
コロナとボン

不定期につづける日記だが,前に書いたのは一ヶ月前か。そんなに経つんだな。毎日家の中で過ごしていると, …