Scoring Dataset (easy question)

hlsmith

Omega Contributor
#1
I have a dataset "Xs" and also some model coefficients. I would like to score the dataset using the coefficients. The dataset has 7 items and there are 8 coefficients counting the intercept. What is the best way to perform this task.


Dataset: Xs [602,7]

Coefficients listed below:
B0 = -0.6341
B1 = 1.968
B2 = 1.919
B3 = 0.142
B4 = -0.052
B5 = 0.300
B6 = 0.010
B7 = 0.013




So, I would like a vector from something like this:


log_odds = Bo + B1(X1) + B2(X2),...,+ B7(X7).








After I have that vector I am going to calculate predicted probabilities from it, then calculate Sensitivities, 1 - Specificities, and then the AUC and plot curve using the Y vector I have.
 
#2
There's probably a more elegant way, but since you only have 7 predictors, it's not too bad to write it out manually (also helps confirm explicitly it's doing what you want)

Code:
#Fake Coefficients
coef = data.frame(b0=-1,b1=2,b2=3)

#Fake Data
datas = data.frame(x1=c(1,2,3,4,5),x2=c(5,4,3,2,1))

#Scored Dataset
score = data.frame(score=(coef[,1]) + (coef[,2]*datas[,1]) + (coef[,3]*datas[,2]))


> coef
  b0 b1 b2
1 -1  2  3
> datas
  x1 x2
1  1  5
2  2  4
3  3  3
4  4  2
5  5  1
> score
  score
1    16
2    15
3    14
4    13
5    12
 

hlsmith

Omega Contributor
#3
Thanks JM.


Leaving for the day, but I added the following to get the ROC curve, will update more tomorrow.


Code:
odds <- exp(score)
prob <- odds / (1 + odds)
prob
install.packages("ROCR")
install.packages("pROC")
require(ROCR)
require(pROC)
predob = prediction(prob, Ys)
perf = performance(predob, "tpr", "fpr")
plot(perf)
 

hlsmith

Omega Contributor
#4
I am sure I will clean this up at some point, but the following is my current code to plot an ROC curve based on a binomial model's coefficients. I am planning in the future to compare curves for multiple modeling approaches in the same graph, but I don't currently have time to play around with it. I will update this thread if I remember and make any progress.


Code:
odds <- exp(score)
prob <- odds / (1 + odds)
prob
install.packages("ROCR")
install.packages("pROC")
require(ROCR)
require(pROC)
rocplot <- function(prob, Ys) {
predob = prediction(prob, Ys)
perf = performance(predob, "tpr", "fpr")
plot(perf)
area <- auc(Ys, prob$score)
area <- format(round(area, 4), nsmall = 4)
text(x=0.8, y=0.1, labels = paste("AUC =", area))
# the reference x=y line
segments(x0=0, y0=0, x1=1, y1=1, col="gray", lty=2)
}