0

I am trying to write my own predict() function for a logistic regression to understand its function better. Right now I have figured out how to write my own predict function for nominal data. However, I am struggling when it comes to categorical data: i.e., I'd like to type in own_predict_function(object, newdata = data.frame(are = 400, gpa = 2.5, rank = '4')) and get a result. In this example, rank is the categorical variable with levels 1,2,3,4.

I have already built my own logistic regression that gives the following output for y ~ gre + gpa + rank

enter image description here

If I use the inbuilt function predict(), I only have to factorize the variable rank and then the inbuilt function automatically knows that predict(object, new data = data.frame(rank = "4")) corresponds to the regression coefficient of rank4. Do you have any advice/hint on how to write your own code for that? I really appreciate your help.

Nicki
  • 157
  • 1
  • 1
  • 8
  • 1
    Why are you writing your own predict? I mean, you could read the [source code of the built in one](https://stackoverflow.com/questions/19226816/how-can-i-view-the-source-code-for-a-function) if you want to know how it works. This really isn't a place for "advice/hints". Ideally you would phrase this as a specific question that can be answered below. Where exactly are you getting stuck? – MrFlick Mar 15 '19 at 18:14
  • @MrFlick thanks for your answer! I am stuck on how to calculate the linear predictor if I have a categorical variable like rank. – Nicki Mar 16 '19 at 17:35

1 Answers1

0
newdata = data.frame( gre= 300, gpa = 2, rank = "2")

newdata$rank <- factor(newdata$rank, levels = 1:4)
mat <- model.matrix(~ gre + gpa + rank, newdata)
mat

For me the following code generated the right matrix so that I can multiply it with the coefficients and calculate the linear predictor.

Nicki
  • 157
  • 1
  • 1
  • 8