繁体   English   中英

绘制 r 中的条件分布

[英]Plot the conditional distribution in r

我正在尝试使用联合分布和边际分布从二元正态分布绘制条件分布。

X 给定 Y 的条件分布为 X|Y=y~N(rho * y,1-rho^2) 而 Y 给定 X 的条件分布为 Y|X=x~N(rho*x,1-rho^2)

这是我使用模拟在 R 中实现这一点的方法(在关节和边缘距离中采用 rho=1/2 的特定值。有关更多信息, 请参见此处

plot.new()

num<-exp(-2*((x^2)-(x*y)+(y^2))/3)
den<-(exp((-x^2)/2)*sqrt(3))/sqrt(2*pi)

quot<-num/den

fun <- function(x, y){quot} 

xs <- seq(-10, 10, by=1)
ys <- seq(-10, 10, by=1)

res <- mapply(fun, list(xs), list(ys))

cols <- c("black", "cornflowerblue", "orange")
matplot(xs, res, col=cols, type="l", lty=1, lwd=2, xlab="x", ylab="result")
legend("bottomright", legend=ys, title="value of y", lwd=2, col=cols)

当我运行代码时,我收到消息错误

Error in matplot(xs, res, col = cols, type = "l", lty = 1, lwd = 2, xlab = "x", : 'x' and 'y' must have same number of rows

为什么它说 x 和 y 必须具有相同的行数? 我可以在代码的哪个部分解决这个问题?

我不明白,请帮助我。

我已经解决了这个问题:)

考虑以下

 fun <- function(x, y){(exp(-2*((x^2)-(x*y)+ 
   (y^2))/3))/((exp((-x^2)/2)*sqrt(3))/sqrt(2*pi))}

    xs <- seq(-5, 5, by=.01)
    ys <- seq(-5, 5, by=.01)

    res <- mapply(fun, list(xs), list(ys))


    matplot(xs, res, col="purple", type="l", lwd=3, xlab="x", ylab="pdf")

显示器

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM