在 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 的特定值。了解更多信息 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必须有相同的行数?我可以在代码的哪一部分解决这个问题?
我不明白,请帮助我。
我已经解决了问题:)
考虑以下
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")
显示器
我正在尝试使用联合分布和边际分布绘制双变量正态分布的条件分布。
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必须有相同的行数?我可以在代码的哪一部分解决这个问题?
我不明白,请帮助我。
我已经解决了问题:)
考虑以下
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")
显示器