depmix function to fit two state gamma distribution - r

I am using depmixS4 package in R. I have a data that looks like a gamma distribution, and I am assuming that there are two states. I would like to fit two-state gamma distribution to my data in R. The following is my code:
mod <- depmix(freq ~ 1, data = mod.data, nstates = 2, family = gamma()) # use gamma
fit.mod <- fit(mod)
However, it seems like I have an error because I am not passing an argument in the family = gamma() part. It works fine if I just use family = gaussian(). Can someone help me with this problem? Thanks!

Using a gamma distribution can provide issues with unsuitable starting values, so be sure to supply suitable ones when using the gamma family.

Related

how to decompose a gamma distribution into two gamma distribution in R

Is there an algorithm available in R that can decompose a gamma distribution into two (or more) gamma distributions? If so, can you give me an example with it? Basically, I have a data set that looks like a gamma distribution if I plot it with respect to time (it's a time series data). Basically, this data contains the movement of the animal. And the animal can be in two different states: hungry, not hungry. My immediate reaction was to use the Hidden Markov Model and see if I can predict the two states. I was trying to use the depmix() function from depmixS4 library in R to see if I can see the two different states. However, I don't really know how to use this function in gamma distribution. The following is the code that I wrote, but it says that I need an argument for gamma, which I don't understand. Can someone tell me what parameter I should use and how to determine the parameter? Thanks!
mod <- depmix(freq ~ 1, data = mod.data, nstates = 2, family = gamma())
fit.mod <- fit(mod)
Thank you!

Windmeijer correction for Arellano-Bond Test in R package plm

Let´s suppose I have a simple AR(1) panel data model I estimate with the pgmm command in R - data available :
library(plm)
library(Ecdat)
data(Airline)
reg.gmm = pgmm(output ~ lag(output, 1)| lag(output, 2:99), data= Airline, Robust=TRUE)
With Robust=TRUE I use the Windmeijer(2005) correction to the variance-covariance matrix. Now I want to test for second order autocorrelation using Arrelano-Bond:
mtest(reg.gmm, order = 2, vcov = reg.gmm$vcov)
Am I using the Windmeijer-corrected variance-covariance matrix, as I intend to? If not, how can I implement it? The documentation is quite tight-lipped on that topic. Thanks for any help in advance!
Unfortunately the example with the Airline data throws an error which seems to be related to too many instruments in your GMM formula. If you are using different data which does not exhibit this problem you can use robust standard errors by using the vcovHC option in mtest. In your example the last call could then be:
mtest(reg.gmm, order = 2, vcov = vcovHC)

Can I do a mulitvariate regression with the segmented package in r?

I have FINALLY figured out how to use the segmented package with a uni-variate analysis giving results comparable to what I was expecting. Ultimately though, I have to do a GLM piece-wise regression on a multivariate analysis. The model has some variables that need to be segmented and some that do not as well as categorical variables. Is this possible with the segmented package?
If so, how?
Do I have to keep interactively keep developing models adding one variable to the segmented package at at time?
piecewise <- glm(y ~ x, family = quasipoisson(link = "log"), data = data)
piecewise_seg <- segmented(piecewise, seg.z = ~ x1, psi = 3)
piecewise_seg2 <- segmented(piecewise_seg, seg.z = ~x2 psi = 400)
Or can I do this in one go? If so, how can I set the different psi parameters for each different variable?
Wait, I think I found it towards the end of the package documentation.
2 segmented variables: starting values requested via a named list
o1<-update(o,seg.Z=~x+z,psi=list(x=c(30,60),z=.3))

rstanarm for adaptive trials

I started exploring the rstanarm package and was curious as how this package could potentially be used in an adaptive trial scenario. The example scenario given within vignette provides a posterior of -0.622 with a credible interval from -0.69 to -0.56.
What would my script look like if I wanted to use this posterior as a prior for my next model when I have additional data from the adaptive trial?
# Code from vignette
t_prior <- student_t(df = 7, location = 0, scale = 2.5)
fit1 <- stan_glm(switch ~ dist100, data = wells,
family = binomial(link = "logit"),
prior = t_prior, prior_intercept = t_prior,
chains = 10, cores = 2, seed = 3245, iter = 100)
Your question is not so easily answered within the rstanarm framework because it only offers limited choices for the priors.
It is entirely valid to use your original prior with the total data from phase I and phase II combined to obtain a posterior distribution (essentially ignoring the intermediate posterior distribution you had after phase I). Alternatively, you could do as you suggest in phase I, then call
draws <- as.matrix(fit1)
mu <- colMeans(draws)
Sigma <- cov(mu)
and use these (estimated) mu and Sigma values as the hyperparameters in a multivariate normal prior over the coefficients in phase II. Unfortunately, such a prior is not supported by rstanarm, so you would need to write your own model with a Bernoulli likelihood, a logit link, and a multivariate normal prior in the Stan language or I think you could accomplish all that using the brm function in the brms package, which generates Stan code from R syntax and draws from the corresponding posterior distribution.
Both approaches conceptually should give you the same posterior distribution after phase II. However, with a finite number of posterior draws they will differ a little bit numerically and the multivariate normal prior might not be a complete description of the posterior distribution you obtained after phase I.

Regularised discriminant analysis (RDA) in R

I am trying to apply RDA on my data in R, after some research I found that there is a package in R called "rda" which seems can do the job for me. However I looked at the description of the RDA function in that package and I'm a little confused now:
Usage given in R:
rda(x, y, xnew=NULL, ynew=NULL, prior=table(y)/length(y),alpha=seq(0, 0.99, len=10), delta=seq(0, 3, len=10), regularization="S", genelist=FALSE, trace=FALSE)
I'm not sure what do "alpha" and "delta" stand for in this case. I was taught that in RDA, there are two parameters "lambda" and "sigma", where lambda is a complexity parameter that
dictates the balance between linear and quadratic discriminant analysis and sigma is another parameter to regularise the covariance matrix further. BOTH OF THEM ARE BETWEEN 0 AND 1.
But as for this "rda" function in R, the default values of delta is between 0 and 3 which confused me.
Could anyone explain this for me please? Thanks!
You can use the package klaR which have a function rda with a parametrization of regularization parameters similar to the one you described.
detach(package:rda)
require(klaR)
data(iris)
x <- rda(Species ~ ., data = iris, gamma = 0.05, lambda = 0.2)
predict(x, iris)
Is not a good idea to mix the two packages (namespace issue for some functions), it's better to detach rda if you want to use klaR (or the opposite).

Resources