computeR2 | R Documentation |
Compute the variance explained by a linear or generalized linear model.
mypredict(model, newdata)
computeR2(pred, outcome, usebinary = 1)
model |
a fitted model, which is the output of |
newdata |
a data frame which contains all the variables included in the model. This data frame is used to make prediction on. |
pred |
a vector of the predicted outcome. |
outcome |
a vector of the actual outcome. |
usebinary |
a non-negative number representing different models.
Use linear model if |
The variance explained by a linear model is based on the conventional R2. As for logistic regression, we use McFadden's R2.
mypredict
returns a vector of the predicted outcome.
computeR2
returns a positive number of the variance explained by the
linear model (conventional R2) or
the generalized linear model (McFadden's R2).
McFadden, Daniel. "Conditional logit analysis of qualitative choice behavior." (1973).
## create datasets
x=matrix(runif(100,-2,2),ncol=5)
outcome=(0.5*x[,2] - 0.8*x[,4] + 0.3*x[,5])>runif(100,-2,2)
## create binary outcome
outcome[outcome]=1
data=data.frame(outcome,x)
## compute the variance explained by features
model=themodel(outcome~.,data[1:80,],usebinary=1)
outcome_predict=mypredict(model,data[81:100,])
computeR2(outcome_predict,data[81:100,'outcome'],usebinary=1)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.