[ 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 を代入したことになる。

I work for money. If you want loyalty,
buy yourself a dog.

	-- One of Nadav Har'El's Email Signatures.

        <integral>  hi perly!
 <perlygatekeeper>  hey Chris, hey integral
 <perlygatekeeper>  dabreegster, don't know you do I but HEY anyway
 <perlygatekeeper>  what's been up?
     <dabreegster>  Ignore me, fine.
                 *  dabreegster goes in a corner
       <Chris62vw>  dabreegster is the man, man
     <dabreegster>  Ah, that's better.
         <rindolf>  perlygatekeeper: yo, yo, yo, dude!
 <perlygatekeeper>  rindolf!!
         <rindolf>  perlygatekeeper: what's up?
 <perlygatekeeper>  hmmm
 <perlygatekeeper>  not much
 <perlygatekeeper>  you?
         <rindolf>  perlygatekeeper: fine. Let me recall what I said to
                    ezra.
         <rindolf>  perlygatekeeper: I'm fine. Got into a few flamewars,
                    and escaped alive to tell the tale.
         <rindolf>  perlygatekeeper: worked a bit on my story "The Human
                    Hacking Field Guide".
         <rindolf>  perlygatekeeper: (which, BTW, you appear there (as
                    your IRC nick at least)
         <rindolf>  perlygatekeeper: and now working on the Computer
                    Graphics section of my homepage.
 <perlygatekeeper>  rindolf, what the hell?
         <rindolf>  perlygatekeeper: excuse me?
 <perlygatekeeper>  rindolf was that someone pretending to be me?
 <perlygatekeeper>  I never said those things
         <rindolf>  perlygatekeeper: it's a fictitious story.
         <rindolf>  perlygatekeeper: relax.
     <dabreegster>  perlygatekeeper: or you could be the imposter right
                    now... or maybe just schizophrenic.
         <rindolf>  dabreegster: MPDed not schizophrenic.
         <rindolf>  dabreegster: schizophrenia is not
                    Multi-Persona-Disordered.
            <b0at>  perlygatekeeper: It's fan fiction from your fan!
     <dabreegster>  rindolf: what's the difference?
         <rindolf>  dabreegster: MPD is when there are several
                    personalities living inside your brain.
         <rindolf>  dabreegster: in schizophrenia, you have one I-ness,
                    but hear voices, hallucinate and stuff.
     <dabreegster>  rindolf: Ah. Why is it considered a disorder? MPD
                    could be quite useful... One would have different
                    perspectives on a subject.
 <perlygatekeeper>  where's beth, she'll know it's me
        <integral>  But how will we know it's beth?!
     <dabreegster>  rindolf: Oh, I have MPD then, not schizophrenia. I
                    don't hallucinate.
     <dabreegster>  integral: WE DON'T!
            <b0at>  I don't hallucinate, but my other personality does.
     <dabreegster>  How do I know all of you exist? Am I just a figment of
                    my own imagination?
     <dabreegster>  b0at: Interesting...
         <rindolf>  dabreegster: Julian Jaynes describes schizophrenia
                    very well in his "The Origins of Consciousness during
                    the Breakdown of the Bicameral Mind book".
        <integral>  nono, you're all just figments of _lilo_'s imagination
     <dabreegster>  rindolf: I'll check it out
            <b0at>  he wishes
     <dabreegster>  integral: and you?
 <perlygatekeeper>  the voices tell me if it's really beth or not
     <dabreegster>  perlygatekeeper: The voices tell me everything.
     <dabreegster>  Wait, I do have the Voices. Maybe I have MPD _and_
                    schizophrenia.
            <b0at>  Ah, but the question is: do the Voices have voices?
         <rindolf>  dabreegster:
                    http://en.wikipedia.org/wiki/The_Origin_of_Consciousness_in_the_Breakdown_of_the_Bicameral_Mind
            <b0at>  And if so, is it your own voice?
     <dabreegster>  b0at: And do the voices of the voices have voices?
            <b0at>  That's just going too far.
             <dkr>  don't worry, those are angels, invest in tarot cards
                    and you will be able to understand them
     <dabreegster>  b0at: and if it's not, then could it be the voice
                    of........ integral? rindolf? or.... buu!
            <b0at>  buu has other plans for our empty skulls
     <dabreegster>  b0at: and if they do, then what do the voices of the
                    voices of the voices of the Voices sound like?
        <integral>  *sob* it's the cabbages. The cabbages keep telling me
                    to do things
           <Botje>  really? most of the time it's the socks that tell me
                    stuff
     <dabreegster>  integral: The lawn gnomes tell me.
                    They're........everywhere...*sniffle*
        <integral>  *blubber* the socks are worse, there's moths living in
                    them
     <dabreegster>  The lawn gnomes tell me to stay away from Life. They
                    force me to write poetry.
     <dabreegster>  integral: *whispering* are the _moths_ the Voices? or
                    the voices of the Voices? or the voices of the voices
                    of the Voices?
                 *  dabreegster goes back to reading
        <integral>  *looks furtively around for moths*

    -- The voices told me so.
    -- #perl, Freenode


Powered by UNIX fortune(6)
[ Main Page ]