我想为数据框中的所有数字变量生成单独的 qqplots 以评估单变量正态性(只需要一个 x 变量)。这些图不必存储为列表——仅在 r-studio 中显示。
我尝试了多种方法,但没有成功,包括 qqline/qqnorm (base r)、qplot (ggplot2) 的各种迭代,以及 qqPlot (EnvStats) 与 apply 和 for 循环的结合。下面是几个例子。 txhousing 来自 ggplot2.
使用您认为合适的任何库来解决问题的意图。
df <- txhousing
df.num.vec <- names(df)[sapply(df, is.numeric)]
df.num <- df[, df.num.vec]
apply(df.num,2,qqPlot)
这会导致一系列错误:
Warning messages:
1: In is.not.finite.warning(x) :
There were 568 nonfinite values in x : 568 NA's
2: In FUN(newX[, i], ...) :
568 observations with NA/NaN/Inf in 'x' removed.
3: In is.not.finite.warning(x) :
There were 568 nonfinite values in x : 568 NA's
4: In FUN(newX[, i], ...) :
568 observations with NA/NaN/Inf in 'x' removed.
5: In is.not.finite.warning(x) :
There were 616 nonfinite values in x : 616 NA's
6: In FUN(newX[, i], ...) :
616 observations with NA/NaN/Inf in 'x' removed.
7: In is.not.finite.warning(x) :
There were 1424 nonfinite values in x : 1424 NA's
8: In FUN(newX[, i], ...) :
1424 observations with NA/NaN/Inf in 'x' removed.
9: In is.not.finite.warning(x) :
There were 1467 nonfinite values in x : 1467 NA's
10: In FUN(newX[, i], ...) :
1467 observations with NA/NaN/Inf in 'x' removed.
df <- txhousing
for (i in seq_along(df)) {
x <- df[[i]]
if (!is.numeric(x)) next
qqPlot(df[,i])
}
这导致:
Error in qqPlot(df[, i]) : 'x' must be a numeric vector
因为你已经过滤了保存在 df.num 中的数字列,你可以直接在 for() 循环中使用 df.num:
library(car)
for (i in 1:ncol(df.num)) {
qqPlot(df.num[, i], main = names(df.num)[i])
}
如果您想将绘图保存到例如 .pdf 文件中,您可以执行以下操作:
myqq = "qq.pdf"
pdf(file=myqq)
for (i in 1:ncol(df.num)) {
qqPlot(df.num[, i], main = names(df.num)[i])
}
dev.off()
您可以访问您工作目录中名为'qq.pdf'的pdf文件