Question

In: Statistics and Probability

I am trying to find HPD credible set using R -- Sample values for X: 1,...

I am trying to find HPD credible set using R --

Sample values for X: 1, 2, 0, 1

Poisson Distribution, posterior is a gamma distribution with alpha = 0.5 and beta = 0.4

What is 95% HPD credible set using R?

Solutions

Expert Solution

Tried this concept for the first time. Let me know if it helped.

R-code

===============================================================================================

x <- c(1, 2, 0, 1)
lambda_true <- var(x)
n <- length(x)
alpha <- 0.5
beta <- 0.5
alpha_conf <- 0.05
# A posterior distribution for the parameter λ is Gamma ( ny'+α, n+β)
alpha_1 <- sum(x) + alpha
beta_1 <- n + beta

#compute 0.025- and 0.975-quantiles
q_lower <- qgamma(alpha_conf / 2, alpha_1, beta_1)
q_upper <- qgamma(1 - alpha_conf / 2, alpha_1, beta_1)
c(q_lower, q_upper)
# [1] 0.3000433 2.1136409 upper and lower bound)
# set up grid for plotting

# set up grid for plotting

lambda <- seq(0,7, by = 0.001)
lambda_true

plot(lambda, dgamma(lambda, alpha_1, beta_1), type = 'l', lwd = 2, col = 'violet',
     ylim = c(0, 1.5), xlab = expression(lambda),
     ylab = expression(paste('p(', lambda, '|y)')))

y_val <- dgamma(lambda, alpha_1, beta_1)
x_coord <- c(q_lower, lambda[lambda >= q_lower & lambda <= q_upper], q_upper)
y_coord <- c(0, y_val[lambda >= q_lower & lambda <= q_upper], 0)
polygon(x_coord, y_coord, col = 'pink', lwd = 2, border = 'violet')
abline(v = lambda_true, lty = 2)

lines(lambda, dgamma(lambda, alpha, beta),
      type = 'l', lwd = 2, col = 'orange')
legend('topright', inset = .02, legend = c('prior', 'posterior'),
       col = c('orange', 'violet'), lwd = 2)

===============================================================================================

0.3000433 2.1136409 upper and lower bound respectively,


Related Solutions

I am trying to find an appropriate statistical test to run for a research study using...
I am trying to find an appropriate statistical test to run for a research study using someone else's gathered data (so that no IRB process is needed). In their data they present: Likelihood of Falling Asleep: Never 17 Seldom 22 Moderate 15 High 12 Use of napping during duty: Never 27 Rarely 19 Sometimes 16 Often 4 Both of these seem to be independent variables, but is there a way to show a relationship (or lack thereof) without a dependent...
I am trying to find an appropriate statistical test to run for a research study using...
I am trying to find an appropriate statistical test to run for a research study using someone else's gathered data (so that no IRB process is needed). In their data they present: Likelihood of Falling Asleep: Never 17 Seldom 22 Moderate 15 High 12 Use of napping during duty: Never 27 Rarely 19 Sometimes 16 Often 4 To simplify I think that it would probably be beneficial to group these as: Likelihood of Falling Asleep Never: 17 Yes: 49 Use...
I am asked to find the square roots using the bisection method for x * x...
I am asked to find the square roots using the bisection method for x * x - a = 0. I was wondering how the bisection method is performed. Let's suppose a = 9, so I would need to find the roots of x * x - 9 = 0. Also, from the 1st equation, when would the bisection method NOT output a root?
I am trying to run a regression in R with a continuous dependent variable and a...
I am trying to run a regression in R with a continuous dependent variable and a binary independent variable? The binary one refers to whether a person received treatment or not. how do I interpret the regression outputs since I dont get results based 0 or 1 buf the whole variable?
I am using the phbirths data in the faraway package in R. I want to: 1)...
I am using the phbirths data in the faraway package in R. I want to: 1) create a plot of the birth weight vs the gestational age and I want to colour code the points based on the mothers smoking status to determine whether or not smoking affects the babies. 2) fit a simple model (one regression line) along with both the main effects (parallel lines) and interaction (non parallel lines) ANCOVA model to the data and find out which...
I need to set up and run a goodness of fit test. I am trying to...
I need to set up and run a goodness of fit test. I am trying to learn more about the type of novels that people prefer to read. Please include null and alternative hypotheses. Show all work and please explain if you use any excel tests. Total number of responses is 30. Romance: 18 Sci - Fi: 7 Fiction: 5
Hey I am trying to find a way to find the position of a Red Dwarf...
Hey I am trying to find a way to find the position of a Red Dwarf star that is in an elliptical orbit at a certain time during it's orbit. I know the Red Dwarf's semi major axis which is 3 AU (astronomical unit) and it's period is 1896 days. I've tried to do a simple using its orbit velocity and doing distance=velocity x time, but I wasn't getting answers that made sense. If anyone could set me on the...
This is Using MATLAB: I am trying to store the solution of this matrix because I...
This is Using MATLAB: I am trying to store the solution of this matrix because I want to do something with the result like find the norm of that answer however I am stuck and cannot seem to be able to. Help would be appreciated! --------------------------------------------- MATLAB CODE: close all clear clc A = [1 -1 2 -1; 2 -2 2 -3; 1 1 1 0; 1 -1 4 5]; b = [-8 -20 -2 4]'; x = gauss_elim(A,b) function...
I am trying to implement a search function for a binary search tree. I am trying...
I am trying to implement a search function for a binary search tree. I am trying to get the output to print each element preceding the the target of the search. For example, in the code when I search for 19, the output should be "5-8-9-18-20-19" Please only modify the search function and also please walk me through what I did wrong. I am trying to figure this out. Here is my code: #include<iostream> using namespace std; class node {...
1. I am trying to determine the level of measurement of my data type? I am...
1. I am trying to determine the level of measurement of my data type? I am looking for advice on Nominal, Ordinal, Interval, and Ratio 2. Does the data set have any categorical variables? I am trying to Describe the data set below in very general terms? This data consist of 8 variables: Which are GRE Scores, TOEFL Scores, University Rating, Statement of Purpose, Letter of Recommendation Strength, Undergraduate GPA, . Research Experience, and Chance of Admit. Name Type Description...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT