在ggplot2中微调stat_ellipse()

时间:2014-12-09 15:07:06

标签: r ggplot2

我想创建一个二元正态分布的散点图,其中95%"精确"置信度椭圆。

library(mvtnorm)
library(ggplot2)
set.seed(1)
n <- 1e3
c95 <- qchisq(.95, df=2) 
rho <- 0.8  #correlation 
Sigma <- matrix(c(1, rho, rho, 1), 2, 2) # Covariance matrix

我从双变量法线生成1000个观测值,均值为零,方差= Sigma

x <- rmvnorm(n, mean=c(0, 0), Sigma)
z  <- p95 <- rep(NA, n)
for(i in 1:n){
  z[i] <- x[i, ] %*% solve(Sigma, x[i, ])
  p95[i] <- (z[i] < c95)
}

我们可以使用stat_ellipse轻松地在生成数据的散点图顶部绘制95%置信度椭圆。得到的数字是完全令人满意的,直到你注意到几个红点位于置信椭圆内。我猜这种差异来自某些参数的估计,并随着样本量的增大而消失。

data <- data.frame(x, z, p95)
p <- ggplot(data, aes(X1, X2)) + geom_point(aes(colour = p95))
p + stat_ellipse(type = "norm")

Fig.1

有没有办法微调stat_ellipse(),以便它描绘出&#34;确切的&#34;置信椭圆,如下图所示,使用&#34;手工制作&#34; ellips功能? enter image description here

ellips <- function(center = c(0,0), c=c95, rho=-0.8, npoints = 100){
  t <- seq(0, 2*pi, len=npoints)
  Sigma <- matrix(c(1, rho, rho, 1), 2, 2)
  a <- sqrt(c*eigen(Sigma)$values[2])
  b <- sqrt(c*eigen(Sigma)$values[1])
  x <- center[1] + a*cos(t)
  y <- center[2] + b*sin(t)
  X <- cbind(x, y)
  R <- eigen(Sigma)$vectors
  data.frame(X%*%R)
}
dat <- ellips(center=c(0, 0), c=c95, rho, npoints=100)
p + geom_path(data=dat, aes(x=X1, y=X2), colour='blue')

2 个答案:

答案 0 :(得分:3)

这不是一个真正的答案,但它可能有所帮助。

使用以下命令探索stat_ellipse

stat_ellipse
ls(ggplot2:::StatEllipse)
ggplot2:::StatEllipse$calculate
ggplot2:::calculate_ellipse
?cov.wt

似乎cov.wt正在从模拟数据中估计协方差矩阵:

cov.wt(data[, c(1, 2)])$cov
#           X1        X2
# X1 1.1120267 0.8593946
# X2 0.8593946 1.0372208

# True covariance matrix:
Sigma
#      [,1] [,2]
# [1,]  1.0  0.8
# [2,]  0.8  1.0

您可以考虑使用估算的协方差矩阵计算p95值。或者只是坚持使用您自己执行良好的椭圆绘图代码。

答案 1 :(得分:0)

原始问题中提出的椭圆代码是错误的。当X1和X2变量的平均值为0且标准差为1时,该函数才起作用,而在一般情况下则不会。

这是从stat_ellipse源代码改编而成的替代实现。它以均值向量,协方差矩阵,半径(例如,用置信度计算)和形状的段数作为参数。

calculate_ellipse <- function(center, shape, radius, segments){
# Adapted from https://github.com/tidyverse/ggplot2/blob/master/R/stat-ellipse.R
    chol_decomp <- chol(shape)
    angles <- (0:segments) * 2 * pi/segments
    unit.circle <- cbind(cos(angles), sin(angles))
    ellipse <- t(center + radius * t(unit.circle %*% chol_decomp))
    colnames(ellipse) <- c("X1","X2")
    as.data.frame(ellipse)
}

让我们比较两种实现方式:

library(ggplot2)
library(MASS)  # mvrnorm function, to sample multivariate normal variables
set.seed(42)
mu = c(10, 20)  # vector of means
rho = -0.7      # correlation coefficient
correlation = matrix(c(1, rho, rho, 1), 2)  # correlation matrix
std = c(1, 10)  # vector of standard deviations
sigma =  diag(std) %*% correlation %*% diag(std)  # covariance matrix
N = 1000  # number of points
confidence = 0.95  # confidence level for the ellipse

df = data.frame(mvrnorm(n=N, mu=mu, Sigma=sigma))

radius = sqrt(2 * stats::qf(confidence, 2, Inf))  # radius of the ellipse

ellips <- function(center = c(0,0), c=c95, rho=-0.8, npoints = 100){
# Original proposal
    t <- seq(0, 2*pi, len=npoints)
    Sigma <- matrix(c(1, rho, rho, 1), 2, 2)
    a <- sqrt(c*eigen(Sigma)$values[2])
    b <- sqrt(c*eigen(Sigma)$values[1])
    x <- center[1] + a*cos(t)
    y <- center[2] + b*sin(t)
    X <- cbind(x, y)
    R <- eigen(Sigma)$vectors
    data.frame(X%*%R)
}

calculate_ellipse <- function(center, shape, radius, segments){
# Adapted from https://github.com/tidyverse/ggplot2/blob/master/R/stat-ellipse.R
    chol_decomp <- chol(shape)
    angles <- (0:segments) * 2 * pi/segments
    unit.circle <- cbind(cos(angles), sin(angles))
    ellipse <- t(center + radius * t(unit.circle %*% chol_decomp))
    colnames(ellipse) <- c("X1","X2")
    as.data.frame(ellipse)
}

ggplot(df) +
    aes(x=X1, y=X2) +
    theme_bw() +
    geom_point() +
    geom_path(aes(color="new implementation"), data=calculate_ellipse(mu, sigma, radius, 100)) +
    geom_path(aes(color="original implementation"), data=ellips(mu, confidence, rho, 100))

enter image description here