如何确定Q-Q图中的错误在哪里?

时间:2018-08-29 19:56:28

标签: r plot r-markdown

以下两组代码完成相同的工作。但是输出看起来有所不同。我有什么地方被误解了吗?

```{r}
set.seed(100)
data = data.frame("Height" = sample(45:65, size = 100, replace = TRUE),
                 "Weight" = sample(145:175, size = 100, replace = TRUE),
                 "SBP"    = rnorm(100, 130, 10),
                 "DBP"    = rnorm(100, 90, 10)
                 )
head(data)
```


## Find Mahalanobis Distance (Sample Quantile)

```{r}
Stat.dist = mahalanobis(data, center = colMeans(data), cov = cov(data))
Stat.dist = sort(Stat.dist)
Stat.dist
```



## Find the theoretical quantile value

```{r}
theo.quant = NULL
for (j in 1:nrow(data)){
n = nrow(data)
k = (n-j+1/2)/n
theo.quant[j] = qchisq(k,df = 4,lower.tail = FALSE)
}
theo.quant
```

## Plot Statistical Distance Vs Theoretical Quantile

```{r, fig.height= 4, fig.width=5,fig.align='center'}
plot(Stat.dist~theo.quant,
     xlim = c(0,15), ylim = c(0,15),
     ylab = "Theorectical Quantile",
     xlab = "Statistical Distance (Mahalanobis Distance)",
     pch = 16)
```
Plotting the Q-Q plot using `MVN` package. 

```{r message=FALSE, warning=FALSE, fig.height=4, fig.width=5, fig.align='center'}
library(MVN)
mvn(data = data, multivariateOutlierMethod = "quan" )
```

相应方法的输出如下:

绘制马哈拉比斯距离与卡方分位数:

enter image description here

和使用MVN软件包的Q-Q图:

enter image description here

1 个答案:

答案 0 :(得分:2)

第一个情节在theo.quant上有x-axis,在Stat.dist上有y-axis。第二个情节切换了它们。

plot(y = Stat.dist, x = theo.quant)

plot(x = Stat.dist, y = theo.quant)