[ Main Page ]

R について Octaveとの比較

統計計算をするのにはどのソフトが一番便利でしょうか。簡単な計算であれば、EXCELなどの表計算ソフトが使いやすいかもしれません。 しかし、EXCELには、いくつかの問題点が存在している上に、それらがいつまでたっても修正されず、科学的に間違った結論を 出してしまう可能性が指摘されています。また、ある程度大きいデータだと、EXCELでは操作するのに時間がかかります。 (EXCELの間違いについて、詳しくは ひどい話です などを参照すればわかります。)
では、他のソフトはあるのかといえば、実はいろいろとあります。ここでとりあげるRというソフトは、フリー(GNU)でしかも かなり使い勝手の良いものです。

参考になるページ

Octaveとの書き方の比較

ここには、Octaveとのちょっとした文法の比較を記しています。Octaveは行列などの数値計算に強いと言われていますが、 RでもATLASなどを使用できるので、自分の使う範囲ではあまり 違いが感じられません。線型の計算で差がでるのでしょうか。

正弦波の加算(矩形波) 行列の使い方

たいした違いはありません。やはり、うまく短く書くためには、ある程度関数を知っている必要があるようです。実は、Octaveは Gnuplotなど外部のプログラムを呼び出してグラフなどを書いているので、その点には注意する必要があるかもしれません。

R

x = 0:62/10
A = c(1.2732, 0.4244, 0.2546)
h = c(1,3,5)
Sum = matrix(0, length(x), 1)
Y = matrix(0, nrow=length(A), ncol=length(x)) 
for (i in 1:3) {
Y[i,] <- A[i] * sin(h[i] * x)
Sum = Sum + Y[i,]
}
matplot(cbind(aperm(Y),Sum), pch=1:4, type="l")
      

Octave

x = 0:0.1:pi*2;
A = [1.2732 0.4244 0.2546];
h = [1 3 5];
Sum = zeros(1,63)
for i = 1:3
Y(i, :) = A(i) * sin(h(i) * x);
Sum = Sum + Y(i, :);
end
plot(x, Y, x, Sum);
      

Fourier変換

参考までに。また、;をつけるかで評価が変わってくるので、注意するとよいでしょう。

R

n = 512
nh = n/2
t = 0:n-1
Fs = 51.2
A = c(1.2732, 0.4244, 0.2546)
h = c(1, 3, 5) * 2 * pi
Sum = matrix(0, length(t), 1)
Y = matrix(0, nrow=length(A), ncol=length(t)) 
for (i in 1:3) {
        Y[i,] <- A[i] * sin(h[i] * t / Fs)
        Sum = Sum + Y[i,]
}

Y = fft(Sum)
Z = abs(Y)/(n/2)
f = t/n*Fs
plot(f[1:n/2], Z[1:n/2], type="l")
      

Octave

clear;
n = 512;
nh = n/2;
t = 0:n-1;
Fs = 51.2;
A = [1.2732 0.4244 0.2546];
h = [1 3 5] * 2 * pi;

Sum = zeros(1, n);
for i = 1:3
y(i, :) = A(i) * sin(h(i) * t / Fs);
Sum = Sum + y(i, :);
end

Y = fft(Sum, n);
Z = abs(Y)/256;
f = (0:n-1) / n * Fs;
plot(f(1:nh), Z(1:nh));
      

Tips

グローバル変数、永続付値

通常、=や<-といった代入は、関数などいわゆる{}で囲まれた部分のみで有効であり、Rでも同じである。だから、関数において 関数の外で使われている変数に書き込むには=や<-は使えない。

> a <- 1
> b <- 2
> func <- function(){a <- 2;b <<- 3;print(a);print(b)}
> func()
[1] 2
[1] 3
> print(a)
[1] 1
> print(b)
[1] 3
      

この例では、グローバル変数a,bはそれぞれ1,2で初期化され、func()では、ローカル変数のa,グローバル変数のbにそれぞれ2,3 を代入したことになる。

 <rindolf>  sussman: for the record, I think the build system is the
            ultimate proof that python code can be as bad as Perl one.
         *  clkao giggles
   <jackr>  hehe
         *  rindolf hopes he's not starting a flamewar
   <clkao>  btw, freebsd svn port maintainer was complaining about unable
            to do --with-swig specifying only perl or pythong bindings to
            build..
         *  cmpilato notes that the topic of this channel is Subversion.
   <clkao>  (so he refused to include the option for building either
            bindings in the port!)
 <rindolf>  I once saw a perl5 code written in perl4 style. Now that was
            hideous.
    <fitz>  complicated != bad
    <fitz>  "Building is complicated--that's why build systems are
            complicated." --kfogel

    -- #svn, Freenode

To the popular press, "hacker" means someone who breaks into computers. Among
programmers it means a good programmer. But the two meanings are connected. To
programmers, "hacker" connotes mastery in the most literal sense: someone who
can make a computer do what he wants-- whether the computer wants to or not.

To add to the confusion, the noun "hack" also has two senses. It can be either
a compliment or an insult. It's called a hack when you do something in an ugly
way. But when you do something so clever that you somehow beat the system,
that's also called a hack. The word is used more often in the former than the
latter sense, probably because ugly solutions are more common than brilliant
ones.

Believe it or not, the two senses of "hack" are also connected. Ugly and
imaginative solutions have something in common: they both break the rules. And
there is a gradual continuum between rule breaking that's merely ugly (using
duct tape to attach something to your bike) and rule breaking that is
brilliantly imaginative (discarding Euclidean space).

    -- Paul Graham
    -- The Word "Hacker" ( http://www.paulgraham.com/gba.html )


Powered by UNIX fortune(6)
[ Main Page ]