
我復制它們的嘗試ggplot2
如下:
P1 = qqPlot2(resmcp, ylab = "Resíduos",
xlab = "Quantil N(0,1)", pch = 20)
PP1 = ggplot(data = P1, aes(resmcp))
geom_point(aes(y = resmcp), show.legend = FALSE)
P2 = qqPlot2(dm, distribution = 'chisq', df = q, pch = 20,
ylab = expression(paste("Quantis de Mahalanobis")),
xlab = "Quantis da Qui-quadrado")
PP2 = ggplot(data = P2, aes(dm))
geom_point(aes(y = dm), show.legend = FALSE)
x11()
gridExtra::grid.arrange(PP1,PP2, ncol = 2)
但是,發生了一些事情,因為我得到了以下結果:
請參閱下面的分位數馬氏距離圖與卡方分位數的嘗試:
gVals <- function(y, dist, conf){ # distribution; confidence interval
y <- sort(y) # make sure they're in order
p <- ppoints(length(y))
if(dist == "chisq") {
zi <- qchisq(p, df = length(p) - 1)
zd <- dchisq(zi, df = length(p) - 1)
qz <- qchisq(c(.25, .75), length(p) - 1)
} else {
zi <- qnorm(p)
zd <- dnorm(zi)
qz <- qnorm(c(.25, .75))
}
# if quartiles preferred
qx <- quantile(y, c(.25, .75))
b <- (qx[2] - qx[1]) / (qz[2] - qz[1])
a <- qx[1] - b * qz[1]
# if robust preferred
# coef <- coef(rlm(y~zi))
# a <- coef[1]
# b <- coef[2]
z <- qnorm(1 - (1 - conf)/2) # z = 1.96 for 95%...
se <- (b / zd) * sqrt(p * (1 - p)/length(p))
ft <- a b * zi
uc <- ft z * se
dc <- ft - z * se
dff = data.frame(z = zi, y = y, uc = uc, dc = dc)
list(a = a, b = b, dff = dff) # returns intercept, slope, and data frame
}
cdf <- gVals(dm, "chisq", .95) # dm is defined in the previous code above
ggplot(cdf$dff, aes(x = z, y = y))
geom_point()
geom_abline(intercept = cdf$a[[1]], slope = cdf$b[[1]])
annotate("line", x = cdf$dff$z, y = cdf$dff$uc, color = "red", lty = 2)
annotate("line", x = cdf$dff$z, y = cdf$dff$dc, color = "red", lty = 2)
請注意,x 軸應從 0 到 8,y 軸應從 0 到 14。此外,模擬包絡的形狀也不相似。我無法解決這個問題。
uj5u.com熱心網友回復:
更新
quartile
我沒有注釋掉選項的代碼,而是注釋掉了robust
函式中選項的代碼。此外,它不是回傳資料框,而是回傳一個串列。僅供參考,如果您使用robust
選項(用于功能rlm
),則只需要 MASS 包。
此功能基于qqPlot2
您問題中使用的代碼。但是,它不會回傳情節;它回傳資料。
library(car)
library(MASS)
library(tidyverse)
gVals <- function(y, dist, conf){ # distribution; confidence interval
y <- sort(y) # make sure they're in order
p <- ppoints(length(y))
if(dist == "chisq") {
zi <- qchisq(p, df = length(p) - 1)
zd <- dchisq(zi, df = length(p) - 1)
qz <- qchisq(c(.25, .75), length(p) - 1)
} else {
zi <- qnorm(p)
zd <- dnorm(zi)
qz <- qnorm(c(.25, .75))
}
# if quartiles preferred
qx <- quantile(y, c(.25, .75))
b <- (qx[2] - qx[1]) / (qz[2] - qz[1])
a <- qx[1] - b * qz[1]
# if robust preferred
# coef <- coef(rlm(y~zi))
# a <- coef[1]
# b <- coef[2]
z <- qnorm(1 - (1 - conf)/2) # z = 1.96 for 95%...
se <- (b / zd) * sqrt(p * (1 - p)/length(p))
ft <- a b * zi
uc <- ft z * se
dc <- ft - z * se
dff = data.frame(z = zi, y = y, uc = uc, dc = dc)
list(a = a, b = b, dff = dff) # returns intercept, slope, and data frame
}
這是與一些任意資料的比較。
data(mtcars)
qqPlot2(mtcars$mpg)
qqPlot2(mtcars$mpg, dist = "chisq", df = 31)
ndf <- gVals(mtcars$mpg, "norm", .95)
ggplot(ndf$dff, aes(x = z, y = y))
geom_point()
geom_abline(intercept = ndf$a[[1]], slope = ndf$b[[1]])
annotate("line", x = ndf$dff$z, y = ndf$dff$uc, color = "red", lty = 2)
annotate("line", x = ndf$dff$z, y = ndf$dff$dc, color = "red", lty = 2)
cdf <- gVals(mtcars$mpg, "chisq", .95)
ggplot(cdf$dff, aes(x = z, y = y))
geom_point()
geom_abline(intercept = cdf$a[[1]], slope = cdf$b[[1]])
annotate("line", x = cdf$dff$z, y = cdf$dff$uc, color = "red", lty = 2)
annotate("line", x = cdf$dff$z, y = cdf$dff$dc, color = "red", lty = 2)
uj5u.com熱心網友回復:
我設法通過圖書館解決了它qqplotr
。
library(qqplotr)
dist <- "chisq"
dpar <- list(df = q)
QT <- data.frame(QUANTIS = dm); ggplot(QT, aes(sample = QUANTIS))
stat_qq_band(distribution = dist, dparams = dpar)
stat_qq_point(distribution = dist, dparams = dpar)
stat_qq_line(distribution = dist, dparams = dpar, color = "blue");
qqPlot2(dm, distribution = 'chisq', df = q, pch = 20,
ylab = expression(paste("Quantis de Mahalanobis")),
xlab = "Quantis da Qui-quadrado")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/485242.html