computeR2 {HTRX} | R Documentation |
Compute variance explained by models
Description
Compute the variance explained by a linear or generalized linear model.
Usage
mypredict(model, newdata)
computeR2(pred, outcome, usebinary = 1)
Arguments
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 |
Details
The variance explained by a linear model is based on the conventional R2. As for logistic regression, we use McFadden's R2.
Value
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).
References
McFadden, Daniel. "Conditional logit analysis of qualitative choice behavior." (1973).
Examples
## 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)