Confidence Ellipse Construction and Issues with Y-Shaped Output

Confidence Ellipse Construction and Issues with Y-Shaped Output

Confidence ellipses are a fundamental concept in statistical inference, used to visualize the uncertainty associated with estimates of population parameters. In this post, we’ll explore how to construct a confidence ellipse using R and identify a subtle mistake that may lead to an incorrect Y-shaped output.

Introduction to Confidence Ellipses

A confidence ellipse is a graphical representation of the estimated distribution of a parameter based on sample data. It provides a visual representation of the uncertainty associated with the estimate, allowing us to understand how much we can trust our results. In this post, we’ll focus on constructing a confidence ellipse for two parameters, mu1 and mu2.

The Basics of Confidence Ellipse Construction

The construction of a confidence ellipse involves calculating the variance-covariance matrix of the parameter estimates, which is then used to determine the boundaries of the ellipse. The general formula for constructing a confidence ellipse in R is:

ellipse <- function(mu, sigma) {
  # Calculate the Cholesky decomposition of the variance-covariance matrix
  chol <- chol(sigma)
  
  # Calculate the coefficients of the ellipse
  u <- sqrt(1 / (2 * pi)) ^ (n - 1) * t(chol) %*% diag(n) %*% chol
  
  # Define the limits of the ellipse
  limit <- qchisq(c(0.975, 0.025), chi^2(n))
  
  return(list(u=u, limit=limit))
}

In this formula, mu is the vector of parameter estimates, sigma is the variance-covariance matrix, n is the number of parameters, and chi^2(n) is the chi-squared distribution with n degrees of freedom.

The car Package in R

The car package provides an easy-to-use interface for constructing confidence ellipses. The ellipse() function takes two arguments: mu1 and mu2, which are the estimates of the parameters. It returns a list containing the coefficients of the ellipse (u) and the limits of the ellipse.

library(car)

Constructing a Confidence Ellipse

To construct a confidence ellipse using the car package, we need to specify the following:

  • mu1: the estimate of parameter 1
  • mu2: the estimate of parameter 2
  • sigma: the variance-covariance matrix
  • levels: the desired confidence level (default is 95%)
  • drawlabels: a logical value indicating whether to draw labels on the ellipse
mu1 <- seq(1650, 2100, 50)
mu2 <- seq(7000, 9500, 50)
sigma <- matrix(c(30 * 1.155461e-05 * (1860.500 - mu1)^2, 
                  30 * 4.11493e-07 * (8354.133 - mu2)^2, 
                  60 * 1.98504e-06 * (1860.500 - mu1) * (8354.133 - mu2)), 
                 nrow = 2)
z <- t(sapply(mu1, function(x) {ellipse(mu1[x], mu2[x])}))
contour(mu1, mu2, z, levels=6.91, drawlabels=T, axes=T, frame.plot=T, xlab="mu1 (stf)", ylab="mu2 (ben strnegth)", main = "A 95% confidence ellipse for mu = (mu1,mu2)")
points(1860.5, 8354.133)
segments(0, 8354.133, 1860.500, 8354.133)
segments(1860.500, 0, 1860.500, 8354.133)

The Issue: Y-Shaped Output

The question mentions that the ellipse came out as a Y-shaped output. Upon closer inspection of the code provided in the car package documentation, we can see that there’s a subtle mistake.

ellipse <- function(mu1,mu2) {
  ...
}

In this function definition, there’s no line break before the minus sign (-). As the OP mentions, when you compute the expression 30*4.11493e-07*(8354.133-mu2)^2, it is treated as a second R expression and returned by itself. This means that your function is returning only the cross-term (60*1.98504e-06 * (1860.500-mu1) * (8354.133-mu2)). As expected, this leads to incorrect results for the ellipse.

Solutions

The OP proposes two solutions:

Solution 1: Using parentheses around the whole expression

ellipse <- function(mu1,mu2) {
  30*1.155461e-05*(1860.500-mu1)^2 + 30*4.11493e-07*(8354.133-mu2)^2 - 
    (60*1.98504e-06 * (1860.500-mu1) * (8354.133-mu2))
}

By using parentheses around the whole expression, we ensure that R treats it as a single expression.

Solution 2: Using the ellipse() function in the car package

library(car)
sigma <- matrix(c(30 * 1.155461e-05 * (1860.500 - mu1)^2, 
                  30 * 4.11493e-07 * (8354.133 - mu2)^2), 
                 nrow = 2)
z <- t(sapply(mu1, function(x) {ellipse(mu1[x], mu2[x])}))
contour(mu1, mu2, z, levels=6.91, drawlabels=T, axes=T, frame.plot=T, xlab="mu1 (stf)", ylab="mu2 (ben strnegth)", main = "A 95% confidence ellipse for mu = (mu1,mu2)")

By using the ellipse() function directly in the car package, we avoid the issue of a line break before the minus sign.


Last modified on 2024-12-14