plyr
Likelihood over subgroups
I saw many messages regarding the message error "arguments imply differing number of rows" but here it's slightly different. I would like to maximize a likelihood function, which includes cumulative products over subgroups. However at the end I get the message "arguments imply differing number of rows: 2, 413". I have 413 lines in my dataset. This message also comes when I do it with some random variables from my dataset. Here is the code : fcontriloglik = function(para){ S_t<<-NA ## I try to create a global variable Define parameters : betarisk1 = para[1:2] sigmarisk1 = para[3:21] Treat1 = para[22] Compute parts of the likelihood : Xb1 = x%*%betarisk1 Xsig1 = sigma%*%sigmarisk1 XbT1 = treatment*Treat1 exbTreat1 = exp(Xb1 + Xsig1 + XbT1) Contributions : h1 = 1 - exp(-exbTreat1) S_t <<- (1 - h1) I want to make a cumulative product over individuals : datatest<-ddply(newdata, .(id), transform, cumSt=cumprod(S_t)) cumulativesum<-data.frame(datatest$cumSt) Compute the likelihood : l1 <- (h1/(1-h1))^risk * cumulativesum loglik = log(l1) return(l1) } Return the value of the likelihood with initial values I set at the beginning : fcontriloglik(stval) The error comes from "cumSt=cumprod(S_t)" which it is not able to compute. Thanks a lot !
Related Links
Likelihood over subgroups
Error in Changing Factors with PLYR in R
Frequency table in R
Installation of plyr in R on Ubuntu does not work
Issue installing library(plyr) in R (Windows)