Created
January 11, 2016 18:19
-
-
Save Martin-Jung/33ca6591193cb59f0a88 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Arguments: | |
# - model = a clmm model | |
# - newdata = a dataframe of new data to apply the model to | |
# Returns a dataframe of predicted probabilities for each row and response level | |
fake.predict.clmm <- function(model, newdata) { | |
# Actual prediction function | |
pred <- function(eta, theta, cat = 1:(length(theta) + 1), inv.link = plogis) { | |
Theta <- c(-1000, theta, 1000) | |
sapply(cat, function(j) inv.link(Theta[j + 1] - eta) - inv.link(Theta[j] - eta)) | |
} | |
# Multiply each row by the coefficients | |
coefs <- c(model$beta, unlist(model$ST)) | |
xbetas <- sweep(newdata, MARGIN=2, coefs, `*`) | |
# Make predictions | |
pred.mat <- data.frame(pred(eta=rowSums(xbetas), theta=model$Theta)) | |
colnames(pred.mat) <- levels(model$model[,1]) | |
pred.mat | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does this do what it suggests it does..i.e. predict probabilitirs from a clmm model? I am abit confused by the 'fake' in the function name. I have posted question here [http://stackoverflow.com/questions/42150143/probability-predictions-with-model-averaged-cumulative-link-mixed-models-fitted] do you reckon you could revise your code to address this? thanks