R 是一个有着统计分析功能及强大作图功能的软件系统,是由奥克兰大学统计学系的Ross Ihaka 和Robert Gentleman 共同创立。由于R 受Becker, Chambers & Wilks 创立的S 和Sussman 的Scheme两种语言的影响,所以R 看起来和S 语言非常相似。
之前安装了RStudio与R语言包2.140,基本配置如下:
platform i386-pc-mingw32
arch i386
os mingw32
system i386, mingw32
status
major 2
minor 14.0
year 2011
month 10
day 31
svn rev 57496
language R
version.string R version 2.14.0 (2011-10-31)
上面的demo较多,运行效果也很好,真的感觉R很好很强大。
我是做GIS的,关注的是spatial data 与 parallel computing,发现R与很多GIS东西都有了绑定,如rgdal,rgeos。看来用R来做空间分析是完全可行的。一下篇中将继续研究R与GIS如何更好的组合。
常用的基本命令如下:
help().search("帮助信息"),如help.search("gdal")
require(库名) 加载依赖库
值得关注的是有:doParallel库,支持多核并行运行;RMongo,Mongodb数据库client,可用于统计分析mongod中的数据。
强!!!
以下为并行程序测试代码(4核):
### R code from vignette source 'gettingstartedParallel.Rnw'
###################################################
### code chunk number 1: loadLibs
###################################################
library(doParallel)
cl <- makeCluster(4)
registerDoParallel(cl)
foreach(i=1:4) %dopar% sqrt(i)
###################################################
### code chunk number 2: bootpar
###################################################
x <- iris[which(iris[,5] != "setosa"), c(1,5)]
trials <- 10000
ptime <- system.time({
r <- foreach(icount(trials), .combine=cbind) %dopar% {
ind <- sample(100, 100, replace=TRUE)
result1 <- glm(x[ind,2]~x[ind,1], family=binomial(logit))
coefficients(result1)
}
})[3]
ptime
###################################################
### code chunk number 3: bootseq
###################################################
stime <- system.time({
r <- foreach(icount(trials), .combine=cbind) %do% {
ind <- sample(100, 100, replace=TRUE)
result1 <- glm(x[ind,2]~x[ind,1], family=binomial(logit))
coefficients(result1)
}
})[3]
stime
###################################################
### code chunk number 4: getDoParWorkers
###################################################
getDoParWorkers()
###################################################
### code chunk number 5: getDoParName
###################################################
getDoParName()
getDoParVersion()
###################################################
### code chunk number 6: stopcluster
###################################################
stopCluster(cl)
### R code from vignette source 'gettingstartedParallel.Rnw'
###################################################
### code chunk number 1: loadLibs
###################################################
library(doParallel)
cl <- makeCluster(3)
registerDoParallel(cl)
foreach(i=1:3) %dopar% sqrt(i)
###################################################
### code chunk number 2: bootpar
###################################################
x <- iris[which(iris[,5] != "setosa"), c(1,5)]
trials <- 10000
ptime <- system.time({
r <- foreach(icount(trials), .combine=cbind) %dopar% {
ind <- sample(100, 100, replace=TRUE)
result1 <- glm(x[ind,2]~x[ind,1], family=binomial(logit))
coefficients(result1)
}
})[3]
ptime
###################################################
### code chunk number 3: bootseq
###################################################
stime <- system.time({
r <- foreach(icount(trials), .combine=cbind) %do% {
ind <- sample(100, 100, replace=TRUE)
result1 <- glm(x[ind,2]~x[ind,1], family=binomial(logit))
coefficients(result1)
}
})[3]
stime
###################################################
### code chunk number 4: getDoParWorkers
###################################################
getDoParWorkers()
###################################################
### code chunk number 5: getDoParName
###################################################
getDoParName()
getDoParVersion()
###################################################
### code chunk number 6: stopcluster
###################################################
stopCluster(cl)
输出结果:
> ### R code from vignette source 'gettingstartedParallel.Rnw'
>
> ###################################################
> ### code chunk number 1: loadLibs
> ###################################################
> library(doParallel)
> cl <- makeCluster(3)
> registerDoParallel(cl)
> foreach(i=1:3) %dopar% sqrt(i)
[[1]]
[1] 1
[[2]]
[1] 1.414214
[[3]]
[1] 1.732051
>
>
> ###################################################
> ### code chunk number 2: bootpar
> ###################################################
> x <- iris[which(iris[,5] != "setosa"), c(1,5)]
> trials <- 10000
>
> ptime <- system.time({
+ r <- foreach(icount(trials), .combine=cbind) %dopar% {
+ ind <- sample(100, 100, replace=TRUE)
+ result1 <- glm(x[ind,2]~x[ind,1], family=binomial(logit))
+ coefficients(result1)
+ }
+ })[3]
> ptime
elapsed
26.89
>
>
> ###################################################
> ### code chunk number 3: bootseq
> ###################################################
> stime <- system.time({
+ r <- foreach(icount(trials), .combine=cbind) %do% {
+ ind <- sample(100, 100, replace=TRUE)
+ result1 <- glm(x[ind,2]~x[ind,1], family=binomial(logit))
+ coefficients(result1)
+ }
+ })[3]
> stime
elapsed
50.45
>
>
> ###################################################
> ### code chunk number 4: getDoParWorkers
> ###################################################
> getDoParWorkers()
[1] 3
>
>
> ###################################################
> ### code chunk number 5: getDoParName
> ###################################################
> getDoParName()
[1] "doParallel"
> getDoParVersion()
[1] "1.0.0"
>
>
> ###################################################
> ### code chunk number 6: stopcluster
> ###################################################
> stopCluster(cl)
>