R中的图形数据概述(摘要)函数

机器算法验证 r 数据可视化 描述性统计 探索性数据分析
2022-01-26 16:36:06

我确定我以前在 R 包中遇到过这样的函数,但经过广泛的谷歌搜索后,我似乎无法在任何地方找到它。我正在考虑的函数为给定的变量生成图形摘要,生成带有一些图形的输出(直方图,也许还有一个箱须图)和一些给出细节的文本,如平均值、SD 等。

我很确定这个函数不包含在基础 R 中,但我似乎找不到我使用的包。

有谁知道这样的功能,如果知道,它在什么包中?

4个回答

Frank Harrell 的Hmisc包有一些带有注释选项的基本图形:查看summary.formula()和相关的plotwrap 函数。我也喜欢这个describe()功能。

有关更多信息,请查看Hmisc 库S-Plus 简介和 Hmisc 和设计库

以下是从在线帮助中获取的一些图片(bppltdescribeplot(summary(...))): 替代文字 替代文字 替代文字

许多其他示例可以在R 图形手册上在线浏览,请参阅Hmisc(不要错过rms)。

我强烈推荐PerformanceAnalytics包中的函数chart.Correlations它将大量信息打包到一个图表中:每个变量的核密度图和直方图,以及每个变量对的散点图、lowess 平滑器和相关性。这是我最喜欢的图形数据汇总功能之一:

library(PerformanceAnalytics)
chart.Correlation(iris[,1:4],col=iris$Species)

我喜欢这张图表!

我发现这个功能很有用……原作者的句柄是respiratoryclub

这是一个输出示例

f_summary <- function(data_to_plot)
{
## univariate data summary
require(nortest)
#data <- as.numeric(scan ("data.txt")) #commenting out by mike
data <- na.omit(as.numeric(as.character(data_to_plot))) #added by mike
dataFull <- as.numeric(as.character(data_to_plot))

# first job is to save the graphics parameters currently used
def.par <- par(no.readonly = TRUE)
par("plt" = c(.2,.95,.2,.8))
layout( matrix(c(1,1,2,2,1,1,2,2,4,5,8,8,6,7,9,10,3,3,9,10), 5, 4, byrow = TRUE))

#histogram on the top left
h <- hist(data, breaks = "Sturges", plot = FALSE)
xfit<-seq(min(data),max(data),length=100)
yfit<-yfit<-dnorm(xfit,mean=mean(data),sd=sd(data))
yfit <- yfit*diff(h$mids[1:2])*length(data)
plot (h, axes = TRUE, main = paste(deparse(substitute(data_to_plot))), cex.main=2, xlab=NA)
lines(xfit, yfit, col="blue", lwd=2)
leg1 <- paste("mean = ", round(mean(data), digits = 4))
leg2 <- paste("sd = ", round(sd(data),digits = 4))
count <- paste("count = ", sum(!is.na(dataFull)))
missing <- paste("missing = ", sum(is.na(dataFull)))
legend(x = "topright", c(leg1,leg2,count,missing), bty = "n")

## normal qq plot
qqnorm(data, bty = "n", pch = 20)
qqline(data)
p <- ad.test(data)
leg <- paste("Anderson-Darling p = ", round(as.numeric(p[2]), digits = 4))
legend(x = "topleft", leg, bty = "n")

## boxplot (bottom left)
boxplot(data, horizontal = TRUE)
leg1 <- paste("median = ", round(median(data), digits = 4))
lq <- quantile(data, 0.25)
leg2 <- paste("25th percentile =  ", round(lq,digits = 4))
uq <- quantile(data, 0.75)
leg3 <- paste("75th percentile = ", round(uq,digits = 4))
legend(x = "top", leg1, bty = "n")
legend(x = "bottom", paste(leg2, leg3, sep = "; "), bty = "n")

## the various histograms with different bins
h2 <- hist(data,  breaks = (0:20 * (max(data) - min (data))/20)+min(data), plot = FALSE)
plot (h2, axes = TRUE, main = "20 bins")

h3 <- hist(data,  breaks = (0:10 * (max(data) - min (data))/10)+min(data), plot = FALSE)
plot (h3, axes = TRUE, main = "10 bins")

h4 <- hist(data,  breaks = (0:8 * (max(data) - min (data))/8)+min(data), plot = FALSE)
plot (h4, axes = TRUE, main = "8 bins")

h5 <- hist(data,  breaks = (0:6 * (max(data) - min (data))/6)+min(data), plot = FALSE)
plot (h5, axes = TRUE,main = "6 bins")

## the time series, ACF and PACF
plot (data, main = "Time series", pch = 20, ylab = paste(deparse(substitute(data_to_plot))))
acf(data, lag.max = 20)
pacf(data, lag.max = 20)

## reset the graphics display to default
par(def.par)

#original code for f_summary by respiratoryclub

}

我不确定这是否是您的想法,但您可能想查看fitdistrplus包。它有很多不错的功能,可以自动生成有关您的分布的有用摘要信息,并绘制其中一些信息的图表。以下是小插图中的一些示例

library(fitdistrplus)
data(groundbeef)
windows()              # or quartz() for mac
  plotdist(groundbeef$serving)  

在此处输入图像描述

windows()
> descdist(groundbeef$serving, boot=1000)
summary statistics
------
min:  10   max:  200 
median:  79 
mean:  73.64567 
estimated sd:  35.88487 
estimated skewness:  0.7352745 
estimated kurtosis:  3.551384 

在此处输入图像描述

fw = fitdist(groundbeef$serving, "weibull")

>summary(fw)
Fitting of the distribution ' weibull ' by maximum likelihood 
Parameters : 
       estimate Std. Error
shape  2.185885  0.1045755
scale 83.347679  2.5268626
Loglikelihood:  -1255.225   AIC:  2514.449   BIC:  2521.524 
Correlation matrix:
         shape    scale
shape 1.000000 0.321821
scale 0.321821 1.000000

fg  = fitdist(groundbeef$serving, "gamma")
fln = fitdist(groundbeef$serving, "lnorm")
windows()
  plot(fw)

在此处输入图像描述

windows()
  cdfcomp(list(fw,fln,fg), legendtext=c("Weibull","logNormal","gamma"), lwd=2,
          xlab="serving sizes (g)")

在此处输入图像描述

>gofstat(fw)
Kolmogorov-Smirnov statistic:  0.1396646 
Cramer-von Mises statistic:  0.6840994 
Anderson-Darling statistic:  3.573646