I have the following summation that I want to compute in R:
$\sum^K_{k=1} [(1-z_i) * log[y_i|\lambda_0] + z_i * log[y_i|\lambda_1]]$, where [y_i|\lambda_{1/2}] follows a Poisson(\lambda_{1/2}) distribution.
The latent indicator variable z_i is equal to either 0 or 1, which is provided in the vector z.
I currently have this code in my R script:
sum(dpois(y,lambda0,log=TRUE)[z==0]+dpois(y,lambda1,log=TRUE)[z==1])
I am receiving the following error,
longer object length is not a multiple of shorter object length
which I do not believe is really my issue in this case. I just do not know how to condition my summation. I am relatively new to R and would really appreciate your help.
If your y
and z
are of equal length, then you can try the code below, and I believe it is mathematically equivalent to your objective
sum(dpois(y,lambda0,log=TRUE)*(1-z)+dpois(y,lambda1,log=TRUE)*z)