# How to perform a logistic regression for more than 2 response classes in R

I want to predict the impact of oil price over a Colombian oil company's stock price. I plan to use a logistic regression for this with a categorical variable (Up or Down given the direction of the stock price). Here is part of my dataset:

Minute  ecopet  profit  sum_profit   direccion  cl1_chg   sum_cl1    direccion_cl1
571     2160     0       10           Up         -0.03     0.00      Down
572     2160     0        0           Neutral     0.07    -0.03      Down
573     2160     0       -5           Down       -0.08     0.04      Up
574     2160     0       -5           Down       -0.07    -0.04      Down
575     2160     5       -5           Down       -0.08    -0.11      Down
576     2165     0       -25          Down        0.00    -0.19      Down
577     2165     0       -25          Down       -0.05    -0.19      Down
578     2165     0       -15          Down       -0.17    -0.24      Down
579     2165     5       -15          Down       -0.06    -0.41      Down
580     2170     0       -20          Down        0.03    -0.47      Down
581     2170    -10       0           Neutral     0.04    -0.44      Down


My dependent variable is 'direccion'. But as you can see it has 3 response classes. I know that to implement a binary logistic regression in R the code is:

glm.fit=glm(direccion~direccion_cl1, data=datos, family=binomial)


I am working with intraday information and plan to predict what happens when the oil moves up/ down (in the previous 10 minutes) and how it impacts the stock price in the next 10 minutes.

Could anyone tell me how could I perform this? I don't really know how to perform the logistic regression with 3 response classes. Thanks!

• Instead of logistic regression, you might want to consider linear discriminant analysis or quadratic discriminant analysis depending on the distribution of your data. – States.the.Obvious Oct 7 '15 at 2:15
• by definition logistic regression has two outcomes so you can (1) combine outcomes until you have two outcomes or (2) use an alternative method such as multinomial logistic regression available in multinom function from the nnet : ats.ucla.edu/stat/r/dae/mlogit.htm – charles Oct 7 '15 at 2:21

Take a look at the multinom function of the package nnet in R:

glm.fit=multinom(direccion~., data=datos)
summary(glm.fit)
#Prediction
predict(glm.fit, newdata, "probs")


You should also consider separating you data set into 2 sets: training and testing, build your model based on training set, and test it on the testing set:

alpha=0.7
d = sort(sample(nrow(datos), nrow(datos)*alpha))
train = datos[d,]
test = datos[-d,]
glm.fit=multinom(direccion~., data=train)
predict(glm.fit, test, "probs")


And then measuring the performance of your model by comparing the predicted trends on your testing set with the actual trends.

You might need also to choose the relevant threshold to make the decision from your predicted probabilities.