- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当type = "norm"时,有什么方法可以计算这个椭圆的面积吗?
默认为 type = "t"
。 type = "norm"
显示不同的椭圆,因为它假定多元正态分布而不是多元 t 分布
这是代码和情节(使用与其他帖子类似的代码):
library(ggplot2)
set.seed(1234)
data <- data.frame(x = rnorm(1:1000), y = rnorm(1:1000))
ggplot (data, aes (x = x, y = y))+
geom_point()+
stat_ellipse(type = "norm")
之前的回答是:
#Plot object
p = ggplot (data, aes (x = x, y = y))+
geom_point()+
stat_ellipse(segments=201) # Default is 51. We use a finer grid for more accurate area.
#Get ellipse coordinates from plot
pb = ggplot_build(p)
el = pb$data[[2]][c("x","y")]
# Center of ellipse
ctr = MASS::cov.trob(el)$center
# I tried changing this to 'stats::cov.wt' instead of 'MASS::cov.trob'
#from what is saw from (https://github.com/tidyverse/ggplot2/blob/master/R/stat-ellipse.R#L98)
# Calculate distance to center from each point on the ellipse
dist2center <- sqrt(rowSums((t(t(el)-ctr))^2))
# Calculate area of ellipse from semi-major and semi-minor axes.
These are, respectively, the largest and smallest values of dist2center.
pi*min(dist2center)*max(dist2center)
更改为 stats::cov.wt
不足以获得“标准”椭圆的面积(计算值相同)。关于如何更改代码的任何想法?
谢谢!
最佳答案
好问题,我学到了一些东西。但是我无法重现您的问题并使用不同的方法获得(当然)不同的值。
我认为链接答案中的方法不太正确,因为椭圆中心不是用数据计算的,而是基于椭圆坐标的。我已经更新为根据数据计算这个。
library(ggplot2)
set.seed(1234)
data <- data.frame(x = rnorm(1:1000), y = rnorm(1:1000))
p_norm <- ggplot(data, aes(x = x, y = y)) +
geom_point() +
stat_ellipse(type = "norm")
pb <- ggplot_build(p_norm)
el <- pb$data[[2]][c("x", "y")]
ctr <- MASS::cov.trob(data)$center #updated previous answer here
dist2center <- sqrt(rowSums((t(t(el) - ctr))^2))
pi * min(dist2center) * max(dist2center)
#> [1] 18.40872
由 reprex package 创建于 2020-02-27 (v0.3.0)
更新感谢 Axeman 的想法。
通过先计算特征值,可以直接从协方差矩阵计算面积。您需要通过您想要获得的置信度来缩放方差/特征值。 This blog helped me a lot to understand this a bit better
set.seed(1234)
dat <- data.frame(x = rnorm(1:1000), y = rnorm(1:1000))
cov_dat <- cov(dat) # covariance matrix
eig_dat <- eigen(cov(dat))$values #eigenvalues of covariance matrix
vec <- sqrt(5.991* eig_dat) # half the length of major and minor axis for the 95% confidence ellipse
pi * vec[1] * vec[2]
#> [1] 18.38858
由 reprex package 创建于 2020-02-27 (v0.3.0)
在这种特殊情况下,协方差为零,特征值或多或少是变量的方差。因此,您可以仅使用方差进行计算。 - 假设两者都是正态分布的。
set.seed(1234)
data <- data.frame(x = rnorm(1:1000), y = rnorm(1:1000))
pi * 5.991 * sd(data$x) * sd(data$y) # factor for 95% confidence = 5.991
#> [1] 18.41814
由 reprex package 创建于 2020-02-27 (v0.3.0)
因子 5.991 表示数据置信度为 95% 的卡方似然。更多信息,see this thread
关于r - 'type = "范数时如何计算 ggplot stat_ellipse() 的面积”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60423973/
我有一个包含四类元音的数据集,类似于以下内容: speaker vowel_category f1 f2 1 a x x 1
您有一个包含多个组(例如 10 个)的散点图。您为各组绘制 95% 置信度椭圆。 问题:你不需要看到所有组的置信度椭圆(因为没有必要或者因为其中一些点很少,导致巨大的椭圆) 问题:如何去除确定组的置信
我想创建一个具有 95%“精确”置信椭圆的二元正态分布散点图。 library(mvtnorm) library(ggplot2) set.seed(1) n <- 1e3 c95 <- qchisq
因此,我正在尝试更改从 ggplot2 中的 stat_ellipse 生成的椭圆的线型(参见此处 https://raw.github.com/low-decarie/FAAV/master/r/s
Similar to this question 当type = "norm"时,有什么方法可以计算这个椭圆的面积吗? 默认为 type = "t"。 type = "norm" 显示不同的椭圆,因为
这可能很简单,但我试图在我的 PCoA 图上围绕我的治疗绘制椭圆。 我的数据框 (sc) 是: MDS1 MDS2 Treatment X1xF1 -0.197
我是一名优秀的程序员,十分优秀!