1
$\begingroup$

This question already has an answer here:

I'm using Clayton copula model to generate bivariate observations. x= poisson and y=gamma with, of course, certain dependence among them.

I have generated observations (X,Y) ~ f(x,y) using copula package in r but now I need to generate observation Y|X=x ~ f(Y|X=x) ¿Is there any way/algorithm to generate samples from conditional distribution that involves the copula? For example, like this, found in Nelsen:

  1. Generate two independent uniform (0,1) variates u and t;
  2. Set v = cu(-1)(t), where cu(-1) denotes a quasi-inverse of cu .
  3. The desired pair is (u,v).

I have the next code in r where I model the observations according to Nelsen,using the quasi-inverse of cu from cCopula function:

    t<-runif(1700)
    u_t<-cbind(t,runif(1700))
    V<-cCopula(u_t,claytonCopula(param=0.14,dim=2),inverse=TRUE)
    x<-qpois(u_t[,1],lambda=d.poisson$estimate[1])
    y<-qgamma(V[,2],shape=claim_size.gamma$estimate[1],rate=claim_size.gamma$estimate[2])
    plot(data_sinout$d,data_sinout$claim_size,main='Test dataset x and y',col='blue')
    points(x,y,col='red')
  legend('topright', c('Observed', 'Simulated'), col = c('blue', 'red'), pch=21)

The result is:

enter image description here

which is quite reasonable being one of my first tries.

Now I want to generate observations of Y when X=15, it means f(Y|X=15), then according to the data I expect to have low values of y, in the range of [0,50k].So, I have this code:

t_x<-15
t<-ppois(t_x,lambda=d.poisson$estimate[1])
u_t<-cbind(t,runif(1700))
V<-cCopula(u_t,claytonCopula(param=0.14,dim=2),inverse=TRUE)
x<-qpois(u_t[,1],lambda=d.poisson$estimate[1])
y<-qgamma(V[,2],shape=claim_size.gamma$estimate[1],rate=claim_size.gamma$estimate[2])
plot(data_sinout$d,data_sinout$claim_size,main='Test dataset x and y',col='blue')
points(x,y,col='red')
legend('topright', c('Observed', 'Simulated'), col = c('blue', 'red'), pch=21)

but instead I have:

enter image description here

which is not what I was expecting. Any help would be really appreciate it.

$\endgroup$

marked as duplicate by kjetil b halvorsen, Michael R. Chernick, mdewey, jbowman, Peter Flom - Reinstate Monica Sep 28 '17 at 11:18

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ Thank you Ben. Indeed, it was helpful but I can't install the spcopula package, instead I use the cCopula function from copula package that results in the same: conditional copula and its inverse but I have rare results. Can you have a look at my code, please? $\endgroup$ – statcbr Sep 27 '17 at 17:32
  • $\begingroup$ You could try to install spcopula from gitHub: https://github.com/BenGraeler/spcopula However, your copula has a very small correlation (0.06), hence, the conditioning does not have "any" effect und what you get is a plain sample from the marginal distribution. $\endgroup$ – Ben Sep 28 '17 at 11:58
  • $\begingroup$ Thank you for your answer Ben,that was quite concise, clear and reasonable ¡¡ so, just to finish with this. If I had a data with an "adequate" correlation thus my copula, my code would correctly work and I would see the effect of the conditional, right? $\endgroup$ – statcbr Sep 30 '17 at 0:15