-1
$\begingroup$

I have the following data,

Cond.1 <- c(2.9, 3.0, 3.1, 3.1, 3.1, 3.3, 3.3, 3.4, 3.4, 3.4, 3.5, 3.5, 3.6, 3.7, 3.7,
        3.8, 3.8, 3.8, 3.8, 3.9, 4.0, 4.0, 4.1, 4.1, 4.2, 4.4, 4.5, 4.5, 4.5, 4.6,
        4.6, 4.6, 4.7, 4.8, 4.9, 4.9, 5.5, 5.5, 5.7)
Cond.2 <- c(2.3, 2.4, 2.6, 3.1, 3.7, 3.7, 3.8, 4.0, 4.2, 4.8, 4.9, 5.5, 5.5, 5.5, 5.7,
        5.8, 5.9, 5.9, 6.0, 6.0, 6.1, 6.1, 6.3, 6.5, 6.7, 6.8, 6.9, 7.1, 7.1, 7.1,
        7.2, 7.2, 7.4, 7.5, 7.6, 7.6, 10, 10.1, 12.5)

and I want to apply Logistical Regression,

library(caret)
dat  = stack(list(cond1=Cond.1, cond2=Cond.2))
lr.model = glm(ind~values+I(values^2), dat, family="binomial")
lr.preds = predict(lr.model, type="response")

After using this code, I get value of lr.preds to be

0.4195376 0.3559950 0.3022795 0.3022795 0.3022795 0.2226140 0.2226140 0.1945281 0.1945281 0.1945281 0.1726781 0.1726781 0.1560078 0.1436538 0.1436538 0.1349528 0.1349528 0.1349528 0.1349528 0.1294300 0.1267819 0.1267819 0.1268630 0.1268630 .....

but if change variable names from cond1 to cond1 and cond2 to con2 and run the same code i get different result of lr.preds.

dat  = stack(list(uc=Cond.1, c=Cond.2))
lr.model = glm(ind~values+I(values^2), dat, family="binomial")
lr.preds = predict(lr.model, type="response")

In this case values of lr.preds are

5.804624e-01 6.440050e-01 6.977205e-01 6.977205e-01 6.977205e-01 7.773860e-01 7.773860e-01 8.054719e-01 8.054719e-01 8.054719e-01 8.273219e-01 8.273219e-01 8.439922e-01 8.563462e-01 8.563462e-01 8.650472e-01 8.650472e-01 8.650472e-01 

Why are the values changing just by changing the variable?

$\endgroup$
0

1 Answer 1

2
$\begingroup$

This is a software question, but I will answer it before I vote to move it.

R takes the first level when ordered alphanumerically of a factor as the default. In the first case this is cond1 and in the second case c (corresponds to cond2!).

This means that caret switches the positive and the negative class in your logistic regression. If you look at the probabilities, you can see that they add up to 1. This means the second one just gives 1 - the output of the first model.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.