以下两组代码完成相同的工作。但是输出看起来有所不同。我有什么地方被误解了吗?
```{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" )
```
相应方法的输出如下:
绘制马哈拉比斯距离与卡方分位数:
和使用MVN
软件包的Q-Q图: