how to decompose a gamma distribution into two gamma distribution in R - 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!

Related

How to transform data after fitting a distribution with gamlss?

I have a data set where observations come from highly distinct groups. Each group may have a wildly different distribution, so I am trying to find the best distribution using fitdist from fitdistrplus, then use gamlssML from the gamlss package to find the best parameters.
My issue is with transforming the data after this step. For some of the distributions, like the Box-Cox t, I can find the equation for normalizing the data using the BCT coefficients, but for many of these distributions I cannot.
Does gamlss have a function that normalizes the data after fitting? Their documentation only provides the transformations for a small number of distributions https://www.gamlss.com/wp-content/uploads/2018/01/DistributionsForModellingLocationScaleandShape.pdf
Thanks a lot
The normalised data values (for any distribution) are exactly equal to the residuals from a gamlss fit,
m1 <- gamlss()
which can be accessed by
residuals(m1) or
m1$residuals

How to use gamma family in GLMMTMB

I have a variable called "duration_bout" which corresponds to a duration in seconds. Here's what the distribution looks like:
It looks like a Poisson distribution, but my durations are a continuous variable. If I transform my data to integers, Poisson fits rather well:
I think what I really have here is rather a gamma distribution, but I can't figure out how to use this family in glmmTMB! Several questions:
family=gamma() asks me to define an "x" parameter; what is it?
How come Poisson model fits? How correct is it to tranform my data to integers in order to use it?
Thanks!

Fitting an inhomogeneous Cox LGCP to a replicated point process using mppm

My recent foray into spatial point patterns has brought me to examining LGCP Cox processes. In my case I actually have a series of point patterns that I want to fit a single model to. One of my previous inquiries brought me to using mppm to train such models( thanks Adrian Baddeley!). My next question relates to using this type of Cox model in the context of mppm.
Is this possible to fit an inhomogeneous LGCP Cox process (or other type of Cox process) to a replicated point pattern using mppm? I see some info on fitting Gibbs processes, but not really for Cox processes.
It seems like the answer may be "possibly" through some creative use of the "random" argument.
For the sake of example, lets say I'm fitting a using point pattern Y with a single covariate X (which is a single im). The call to kppm would be:
myModel = kppm(Y ~ X,"LGCP")
If I were fitting a simple inhomogeneous Poisson process to a replicated point pattern and associated covariate in hyperframe G, I believe the call would look like the following:
myModel = mppm(Y ~ X, data=G)
After going through Chapter 16 of the SpatStat book I think that fitting a replicated LGCP Cox model might be accomplished by using the simulated intensities from calls to rLGCP, maybe like this...
myLGCP = rLGCP(model="exp",mu=0,saveLambda=TRUE,nsim=2,win=myWindow)
myIntensity = lapply(myLGCP,function(x) attributes(x)$Lambda)
G$Z = myIntensity
myModel = mppm(Y ~ X, data=G, random=~Z|id)
The above approach "runs" without errors... but I have no idea if I'm even remotely close to actually accomplishing what I wanted to do. It's also a little unclear how to use the fitted object to then simulate a realization of the model, since simulate.kppm requires a kppm object.
Thoughts and suggestions appreciated.
mppm does not currently support Cox processes.
You could do the following
Fit the trend part of the model to your replicated point pattern data using mppm, for example m <- mppm(Y ~ X, data=G)
Extract the fitted intensities for each point pattern using predict.mppm
For each point pattern, using the corresponding intensity obtained from the model, compute the inhomogeneous K function using Kinhom (with argument ratio=TRUE)
Combine the K functions using pool
Estimate the cluster parameters of the LGCP by applying lgcp.estK to the pooled K function.
Optionally after step 4 you could convert the pooled K function to a pair correlation function using pcf.fv and then fit the cluster parameters using lgcp.estpcf.
This approach assumes that the same cluster parameters will apply to each point pattern. If your data consist of several distinct groups of patterns, and you want the model to assign different cluster parameter values to the different groups of patterns, then just apply steps 4 and 5 separately to each group.

student-t distribution for portfolio optimization in R

How can i use a student-t distribution for portfolio optimization in R?
I would fit the data via the estimated parameter and then throw my new distribution into a portfolio optimization package.
From the beginning: I'm trying to do a Portfolio Optimization via the Entropy Pooling Approach by Meucci. As a Basis (Reference Model) i would like to use historical data fitted by a multivariate skewed t-distribution.
Basics: The Entropy Pooling Approach is build upon Black-Litterman - simply said: you can incorporate Views (absolute or relative) into your Model/Portfolio Optimization. The difference compared to the BL is, that you can use a non-normal distribution (not even returns), non-linear Views and views on a variety of parameters. (returns, cor, sd etc.) Therefore, you can put any random data into your model as a reference model. The following step is to blend this model with your individual selected views.
So now, i have a distribution object, but how do i get the distribution into my optimizer. (optimize.portfolio - package 'PortfolioAnalytics'). The requirement therefore is "an xts, vector, matrix, data frame, timeSeries or zoo object of asset returns". The gap in my knowledge is at the transition from the distribution to the new data set.
Thx in Advance!
My code subsequent:
return_distribution = sn::mst.mple(y=returns[,-1])
xi = c(return_distribution[['dp']]$beta)
omega = return_distribution[['dp']]$Omega
alpha = return_distribution[['dp']]$alpha
df = return_distribution[['dp']]$nu
marketDistribution = BLCOP::mvdistribution('mst', xi = xi, Omega = omega,
alpha = alpha, nu = df)
You should look for scenario optimisation, see e.g. https://quant.stackexchange.com/questions/31818/optimize-portfolio-of-non-normal-binary-return-assets/31847#31847 . For an implementaion in R, see for instance https://quant.stackexchange.com/questions/42339/target-market-correlation-for-long-short-equity-portfolio/50622#50622 (though it does not use PortfolioAnalytics).

fit weibull modified in R 3 parameter

I want to fit the following data to a Weibull distribution multiplied by a.
datos: enter link description here
y=b1*(1-exp(-(x/b2)^b3)
However, I could not find a solution using the nls function in R.
Could someone guide me down the path to follow in order to find a solution?
The code used is the following:
ajuste_cg<-nls(y~b1*(-exp(-((x/b2)^b3))),data=d,start=list(b1=1000,b2=140,b3=20), trace=T,control = list(maxiter=10000000))
Thanks!
I suggest you to use the package survival. It is made for implementing parametric survival regressions (Weibull models included, of course). Here's the code:
library(survival)
weibull_model = survreg(Surv(time, event) ~ expalatory_variables, dist="weibull")
The Surv() object that you see instead of a y is an R "survival object", and works like your dependent variable in a survival regression. The time and event variables must represent duration and event occurrence (0 or 1), respectively.
Please replace explanatory_variables with your appropriate set of variables.

Resources