繁体   English   中英

将样本的 qqplot 与 R 中的参考概率分布进行比较

[英]compare qqplot of a sample with a reference probability distribution in R

我有一个天气数据集,我为两列温度和湿度找到了一个简单的线性 model,并绘制了其残差的直方图并计算了平均值和标准差。

model <- lm(Temperature..C. ~ Humidity, data = inputData)
model.res = resid(model) 
hist(model.res) 
mean(model.res)
sd(model.res)

我应该 Plot 残差 QQ 图与估计标准差的零均值正态分布。 我使用 Kolmogorov-Smirnov 将样本与参考概率分布进行比较,但我不知道如何将 plot 一起使用:

ks<-ks.test(model.res, "pnorm", mean=0, sd=sd(model.res)) 
qqnorm(model.res, main="qqnorm") 
qqline(model.res)

数据示例:

        Temperature..C. Humidity
1          9.472222     0.89
2          9.355556     0.86
3          9.377778     0.89
4          8.288889     0.83
5          8.755556     0.83
6          9.222222     0.85
7          7.733333     0.95
8          8.772222     0.89
9         10.822222     0.82
10        13.772222     0.72
11        16.016667     0.67
12        17.144444     0.54
13        17.800000     0.55
14        17.333333     0.51
15        18.877778     0.47
16        18.911111     0.46
17        15.388889     0.60
18        15.550000     0.63
19        14.255556     0.69
20        13.144444     0.70

这是使用ggplot2的解决方案

ggplot(model, aes(sample = rstandard(model))) + 
  geom_qq() + 
  stat_qq_line(dparams=list(sd=sd(model.res)), color="red") +
  stat_qq_line()

红线代表带有您的样本 sd 的 qqline,黑线代表 sd 为 1。

你没有要求,但你也可以添加一个平滑的 qqplot:

data_model <- model
data_model$theo <- unlist(qqnorm(data_model$residuals)[1])

ggplot(data_model, aes(sample = rstandard(data_model))) + 
  geom_qq() + 
  stat_qq_line(dparams=list(sd=sd(model.res)), color="red") +
  geom_smooth(aes(x=data_model$theo, y=data_model$residuals), method = "loess")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM