2009年3月25日星期三

R 令人好氣餒

我不是統計學人,但間中都要用統計軟件,所以電腦裏自然裝了免費的 R。雖說 R 理論上可以作一般的數、理或工程用途,但它的根源始終在統計學,亦因此,R 對我等 casual users 相當難上手。

如果只是拿它在做一兩個 linear regressions,R 的確很好用,單是它的 read.table function 已經很就手。換了是 C/C++ 或 Matlab,就要自己寫程式來區分 headers 與真正的 data 了,不過亦正因為 R 源自統計用途,所以它的設計到處都有統計味,令其他範疇的用家非常不自在。電鋸說 R 有 Unix 那種 KISS 哲學,但見諸 R 的 data structures,卻完全不是這回事。舉個例,R 的 vector 與 matrix 其實不是數學上的 vector 與 matrix,而是近似於 Matlab 的 cell array,亦即是說它們所裝載的不一定是數字,也可以是 heterogeneous data types,包括不同類型的資料甚至是 data structures。此外,它們的每一行或每一列也可以有自己的名稱,因此它們比 Matlab 的 cell array 更為複雜。若我要用 R 來寫一個快速的算法,那麼我就要關心它複製小物件的速度是否夠快,有時還要關心記憶體是否夠用。如果我明明只需要 vectors 與 matrices,但 R 卻一抽二連地附加一大堆資訊在這兩種 data structures 中,這難言是理想狀況。

更令我不自在的是 R 濫用一些數學名詞。Vector, matrix 與 cross product 是其中幾個例子。這些名詞本身有特定意義,R 改了它們的內涵,令人好生混淆。其他語言亦有這種情況,例如 standard C++ 亦稱 dynamic array 為 vector,不過 R 特別離譜。

不過最糟的,還是 R 的 documentation 不夠清楚。如果是說統計方面的,例如在 R 中如何執行某個 statistical test,那麼 R 確是一流的,但是非統計的部份,就沒有那麼幸運了,甚至在官方文件 "An Introduction to R" 之中,你也可以不時看到作者只能夠揣測 R 到底怎樣詮釋某些算式,例如在 5.4.1,作者說:
The precise rule affecting element by element mixed calculations with vectors and arrays is somewhat quirky and hard to find in the references. From experience we have found the following to be a reliable guide.
在 5.7.1 之中又說:
Note that x %*% x is ambiguous, as it could mean either x'x or xx', where x is the column form. In such cases the smaller matrix seems implicitly to be the interpretation adopted, so the scalar x'x is in this case the result.
感覺不是很可靠。我認為除非只用 R 來做一些簡單的事情,否則學習 R 的門檻一點也不低。我間中才用 R,以前學過的很快就忘掉,每次都要重新學習 R 那種一抽二連的資料結構,感覺十分氣餒。

呻完。繼續同 R 搏鬥。

**************
給新手(包括在下這個「永遠新手」)的 R 入門:
附註:在中文版 Windows 中安裝 R,請於安裝後右擊 R 的 shortcut,揀選「內容」,然後在「捷徑」那個 tab 的「目標(T)」一項中,將
"C:\Program Files\R\R-2.7.0\bin\Rgui.exe"
改為
"C:\Program Files\R\R-2.7.0\bin\Rgui.exe" --no-restore-data LC_ALL=en
否則 R 可能無法正常運作。

1 則留言:

匿名 說...

R (或 S )第一天就這樣說:

[R 網的 what is R?]

R is a language and environment for statistical computing and graphics.(...) R provides a wide variety of statistical (linear and nonlinear modelling, classical statistical tests, time-series analysis, classification, clustering, ...) and graphical techniques, and is highly extensible. (...)

One of R's strengths is the ease with which well-designed publication-quality plots can be produced, (...)

整個東西都是為統計和繪圖而設。實情是大部份應用統計只會用到 data frame 這種 data structure 。假如是數理運算的話,__可能__用 GNU Octave 會更好。