【问题标题】:Plot the conditional distribution in r绘制 r 中的条件分布
【发布时间】:2019-10-30 09:21:25
【问题描述】:

我正在尝试使用联合分布和边际分布绘制双变量正态分布的条件分布。

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 的特定值。了解更多信息see here

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 必须有相同的行数?我可以在代码的哪一部分修复这个问题?

我不明白,请帮帮我。

【问题讨论】:

  • 我的问题基于这里给出的答案stackoverflow.com/questions/47879000/…
  • 为什么计算quot的代码在函数体之外?
  • @Rohit 哦应该在里面?我虽然顺序无关紧要。我确实修改了它,但仍然收到消息错误:Error in matplot(xs, res, col = cols, type = "l", lty = 1, lwd = 2, xlab = "x", : 'x' and 'y' must have same number of rows

标签: r plot statistics distribution


【解决方案1】:

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

考虑以下

 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")

展示

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-08
    • 2018-01-20
    • 2023-04-08
    • 2013-08-01
    • 2012-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多