1

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.
Emy
  • 817
  • 1
  • 8
  • 25
  • As with a univariate normal distribution, you can center it wherever you want by adding/subtracting whatever values you need to. If your data means are 3.4 and 1, but you want to transform them to be 0.5, you can subtract 2.9 from the x and 0.5 from the y values. – Gregor Thomas Jan 06 '21 at 17:10
  • @GregorThomas thank you for your feedback. My confusion comes from the fact that I thought mu1 and mu2 were values of means and therefore "segments", but instead they seem to be coordinates. – Emy Jan 06 '21 at 17:13
  • I don't know what you mean by that comment. Where is the "segments" concept coming from? – Gregor Thomas Jan 06 '21 at 17:25
  • @GregorThomas Thank you, I somewhat confused mean and sd in my mental representation of the distribution. It's clear now. I am editing the question. – Emy Jan 06 '21 at 17:46
  • 3
    I think OP's `z` values are intended to be contours of a bivariate Normal denisty (BND) which they are currently "constructing" empirically from a random sample. What I think they want is contours of a predefined BND, for which `mvtnorm::dmvnorm()` will probably be useful. – Limey Jan 06 '21 at 17:50
  • @Limey thank you, that's what I meant. – Emy Jan 06 '21 at 17:54
  • In which case, [this](https://stackoverflow.com/questions/25718363/how-to-plot-bivariate-normal-distribution-with-expanding-ellipses) will definitely be helpful. – Limey Jan 06 '21 at 17:59
  • @Limey, thanks, I have also edited the original posting, I hope that now it's clearer. – Emy Jan 06 '21 at 18:04

0 Answers0