Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. Sample_ypred <- function(fit,NDraws=10){
  2. #extract the original data from the model (fixed and random effects)
  3. X <- fit$X
  4. M <- fit$M
  5. Samples <- list()
  6. #iterate for the number of draws required
  7. for (i in 1:NDraws){
  8. #samples the parameters from the posterior distributions of the fitted model
  9. CoeffFix <- apply(fit$beta.sample,2,function(x){return(sample(x,size=nrow(X)))})
  10. CoefRandom <- apply(fit$gamma.sample,2,function(x){return(sample(x,size=nrow(X)))})
  11. #Add all the valeus to get a prediction
  12. FixedPred <- CoeffFix*X
  13. RandomPred <- CoefRandom*M
  14. Pred <- rowSums(FixedPred)+rowSums(RandomPred)
  15. #Store it in a list
  16. Samples[[length(Samples)+1]]<-Pred
  17. }
  18. #Return the NDraws
  19. Preds <- do.call(cbind,Samples)
  20. return(Preds)
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement