R语言有哪些常用的包???

Python011

R语言有哪些常用的包???,第1张

R语言是数据处理的神器,常用的包很多,比如ggplot2, dplyr, knitr等等,可以参考这个博客

http://blog.csdn.net/tanzuozhev/article/details/46536651

作者:任坤

链接:http://www.zhihu.com/question/21792740/answer/27104765

来源:知乎

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

reshape2 横向、纵向做数据变换,例如把纵向堆叠在数据库中的证券行情数据转换成一个按照不同证券代码横向排列,按照时间纵向排列收盘价的数据表

stringr 方便地用正则表达式做批量字符串操作,可做检测、匹配、替换、计数等等

lubridate 方便地做日期/时间操作,各种标准化时间和时区的处理

plyr 轻松地在vector, list, data.frame之间做分组变换,实现拆分、变换、合并的操作

dplyr 轻松地处理data.frame, data.table以及多种数据库为基础的数据,实现选择、变换、分组等等,速度很快

RODBC 连接ODBC数据库接口

RSQLite 连接轻量级SQLite数据库连接

jsonlite 读写json文件

yaml 读写yaml文件,实现灵活的程序外部配置

Rcpp, Rcpp11 写C++03/11代码直接编译后给R调用,大幅提升算法性能

data.table 快速处理较大数据表

ggplot2 高级绘图,一套统一的语法实现复杂图像组合绘制

zoo 时间序列数据的预处理,比如滚动平均等等

rmarkdown 用Markdown写文档并可方便地运行R代码与绘图

knitr 自动文档生成

devtools 扩展包开发必备,在线安装托管的扩展包,检查扩展包是否符合CRAN标准等等

testthat 扩展包自动测试

pipeR 自己写的高性能、低损耗、分工明确的管道操作(pipeline operator)扩展包,使得数据变换流程化

=== 专业领域(数值计算) ===

rootSolve 非线性方程求根、ODE均衡状态解

Rsolnp 非线性优化

=== 专业领域(计量和统计学习) ===

sde 随机微分方程模拟和统计推断

KernSmooth 非参数平滑与分布估计

cpm Change Point Detection 实时分布或者统计关系变化检测

stats4 可用来方便地做MLE估计

R语言学习笔记之聚类分析

使用k-means聚类所需的包:

factoextra

cluster #加载包

library(factoextra)

library(cluster)l

#数据准备

使用内置的R数据集USArrests

#load the dataset

data("USArrests")

#remove any missing value (i.e, NA values for not available)

#That might be present in the data

USArrests <- na.omit(USArrests)#view the first 6 rows of the data

head(USArrests, n=6)

在此数据集中,列是变量,行是观测值

在聚类之前我们可以先进行一些必要的数据检查即数据描述性统计,如平均值、标准差等

desc_stats <- data.frame( Min=apply(USArrests, 2, min),#minimum

Med=apply(USArrests, 2, median),#median

Mean=apply(USArrests, 2, mean),#mean

SD=apply(USArrests, 2, sd),#Standard deviation

Max=apply(USArrests, 2, max)#maximum

)

desc_stats <- round(desc_stats, 1)#保留小数点后一位head(desc_stats)

变量有很大的方差及均值时需进行标准化

df <- scale(USArrests)

#数据集群性评估

使用get_clust_tendency()计算Hopkins统计量

res <- get_clust_tendency(df, 40, graph = TRUE)

res$hopkins_stat

## [1] 0.3440875

#Visualize the dissimilarity matrix

res$plot

Hopkins统计量的值<0.5,表明数据是高度可聚合的。另外,从图中也可以看出数据可聚合。

#估计聚合簇数

由于k均值聚类需要指定要生成的聚类数量,因此我们将使用函数clusGap()来计算用于估计最优聚类数。函数fviz_gap_stat()用于可视化。

set.seed(123)

## Compute the gap statistic

gap_stat <- clusGap(df, FUN = kmeans, nstart = 25, K.max = 10, B = 500)

# Plot the result

fviz_gap_stat(gap_stat)

图中显示最佳为聚成四类(k=4)

#进行聚类

set.seed(123)

km.res <- kmeans(df, 4, nstart = 25)

head(km.res$cluster, 20)

# Visualize clusters using factoextra

fviz_cluster(km.res, USArrests)

#检查cluster silhouette图

Recall that the silhouette measures (SiSi) how similar an object ii is to the the other objects in its own cluster versus those in the neighbor cluster. SiSi values range from 1 to - 1:

A value of SiSi close to 1 indicates that the object is well clustered. In the other words, the object ii is similar to the other objects in its group.

A value of SiSi close to -1 indicates that the object is poorly clustered, and that assignment to some other cluster would probably improve the overall results.

sil <- silhouette(km.res$cluster, dist(df))

rownames(sil) <- rownames(USArrests)

head(sil[, 1:3])

#Visualize

fviz_silhouette(sil)

图中可以看出有负值,可以通过函数silhouette()确定是哪个观测值

neg_sil_index <- which(sil[, "sil_width"] <0)

sil[neg_sil_index, , drop = FALSE]

##          cluster    neighbor     sil_width

## Missouri    3          2        -0.07318144

#eclust():增强的聚类分析

与其他聚类分析包相比,eclust()有以下优点:

简化了聚类分析的工作流程

可以用于计算层次聚类和分区聚类

eclust()自动计算最佳聚类簇数。

自动提供Silhouette plot

可以结合ggplot2绘制优美的图形

#使用eclust()的K均值聚类

# Compute k-means

res.km <- eclust(df, "kmeans")

# Gap statistic plot

fviz_gap_stat(res.km$gap_stat)

# Silhouette plotfviz_silhouette(res.km)

##    cluster size ave.sil.width

## 1     1     13      0.31

## 2     2     29      0.38

## 3     3      8      0.39

#使用eclust()的层次聚类

# Enhanced hierarchical clustering

res.hc <- eclust(df, "hclust") # compute hclust

fviz_dend(res.hc, rect = TRUE) # dendrogam

#下面的R代码生成Silhouette plot和分层聚类散点图。

fviz_silhouette(res.hc) # silhouette plot

##   cluster size ave.sil.width

## 1    1     19      0.26

## 2    2     19      0.28

## 3    3     12      0.43

fviz_cluster(res.hc) # scatter plot

#Infos

This analysis has been performed using R software (R version 3.3.2)