R language学习(一)

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)
>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值