Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
666 views
in Technique[技术] by (71.8m points)

plot - How to get bivariate normal contour values from a non-random sample in R?

I have some trajectory points from an experiment I conducted (see red points below.) What I would like to do is to obtain the predetermined contour values for a bivariate standard normal distribution corresponding to these red line points. This bivariate distribution is centered at at c(3.4, 1) (where the blu circle is.)

In the plot below, I have plotted a bivariate normal distribution through a simulation using mvrnorm . In this case, the x and y values are sampled and then I can obtain the z-values. But how can I obtain the z-values for predetermined points?

(the code for the bivariate distribution is from this website.)

library(MASS)
library(shape)
#> Warning: package 'shape' was built under R version 3.6.2
x <- c(5.04,4.68,4.39,4.09,3.73,3.37,3.07,2.77,2.47,2.11)
y <- c(0.74,0.69,0.64,0.60,0.56,0.52,0.50,0.50,0.50,0.51)

N <- 200 # Number of random samples
set.seed(123)
# Parameters 
rho <- 0.5
mu1 <- 3.4; s1 <- 0.25
mu2 <- 1; s2 <- 0.25

# Parameters for bivariate normal distribution
# Mean 
mu <- c(mu1,mu2) 
# Covariance matrix
sigma <- matrix(c(s1^2, s1*s2*rho, s1*s2*rho, s2^2),2) 

X <- mvrnorm(N, mu = mu, Sigma = sigma)
tail(X,2)
#>            [,1]      [,2]
#> [199,] 3.425264 0.7100933
#> [200,] 3.187654 0.6990182

z <- kde2d(X[,1], X[,2], n=50)
plot(X, xlab="X label", ylab="Y label",  
   xlim=c(2,6), ylim=c(-1,3.5),pch=19, cex=.4)
contour(z, drawlabels=FALSE, add=TRUE)
lines(x,y,type="p", xlim=range(x), ylim=c(-1,3.5), 
       xlab="x", ylab="y", pch=16, col = "red")
plotcircle(mid = c(3.4, 1), r = 0.05, col="blue") 

I had another question about how to center the distribution at specific values. The question was stemming from a misunderstanding, which was clarified in the comments. I leave it below for the record, but there is no need to address it.

  • how can I "center" the bivariate distribution where the blue circle (c(3.4, 1)) is? Right now the distribution is centered there because I have set mu1 <- 3.4 and mu2 <- 1, but I thought that mu1 and mu2 were the values of the means. I actually would like the means to be about 0.5 each.
question from:https://stackoverflow.com/questions/65600027/how-to-get-bivariate-normal-contour-values-from-a-non-random-sample-in-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...