绘制经验似然置信区域

时间:2014-03-22 06:51:59

标签: r

我在经验可能性方面徘徊,我想绘制数据均值的95%和50%置信区域。

我有两个小编:

                GDP        Unemployment  
1995-06-30 0.003493484          0.2  
1995-09-29 0.008564845          0.0 
1995-12-29 0.007099640          0.0 
1996-03-29 0.006553003         -0.1  
1996-06-28 0.017460870         -0.2 
1996-09-30 0.009250896         -0.1 

是否有任何套餐可以用来策划这个?我一直在寻找' emplik'其中但没有运气。

谢谢,

涓。

2 个答案:

答案 0 :(得分:1)

你看过ellipse包了吗?

# assumes OP's data is in a timeseries object ts
df <- data.frame(ts)
library(ellipse)
#plot using base R
plot(ellipse(cov(df),centre=colMeans(df),level=0.95),type="l", lty=2, col="blue")
lines(ellipse(cov(df),centre=colMeans(df),level=0.50),type="l", lty=2, col="green")
points(df)

# using ggplot
library(ggplot2)
ggplot(df, aes(x=GDP,y=Unemployment))+
  geom_point()+
  geom_path(data=data.frame(ellipse(cov(df),centre=colMeans(df),level=0.95)),
            aes(colour="95%"), linetype=2)+
  geom_path(data=data.frame(ellipse(cov(df),centre=colMeans(df),level=0.50)),
            aes(colour="50%"), linetype=2)+
  scale_color_manual("Conf. Limit",values=c("red","blue"))

答案 1 :(得分:0)

rm(list = ls())

library(emplik)

num_grids <- 100

gdp   <- c(0.003493484, 0.008564845, 0.007099640, 0.006553003, 0.017460870, 0.009250896)
unemp <- c(0.2, 0, 0, -0.1, -0.2, -0.1)
df <- cbind(gdp,unemp)

mu_x <- seq(min(gdp), max(gdp), length.out = num_grids)
mu_y <- seq(min(unemp), max(unemp),length.out = num_grids)

myresult <- matrix(NA, ncol = num_grids, nrow = num_grids)

for(i in 1:num_grids)
  for(j in 1:num_grids) 
    myresult[i,j] <- el.test(x= df, mu = c(mu_x[i], mu_y[j]))$"-2LLR"

plot(gdp, unemp, xlab = "gdp", ylab = "unemployment", 
     main = "Empirical Likelihood Contours")
points(mean(gdp), mean(unemp), cex = 2, col = "brown", pch = 19)
par(new=TRUE)
contour(x=mu_x, y=mu_y, z=myresult, levels = 0.5, col="red")
par(new=TRUE)
contour(x=mu_x, y=mu_y, z=myresult, levels = 0.99, col="blue")

enter image description here