0
$\begingroup$

Im running a multiple regression model and therefore need to create dummy variables for a categorical predictor variable. This variable is 'YSK87' and its values in the dataset correspond to the following:

VALUE LABEL 1 = 1 Person 2 = 2 Persons 3 = 3 Persons 4 = 4 or more Persons

Since almost all the values - apart from '4' - represent that same number of persons, would I have to create a dummy variable for YSK87? I understand that R would have noticed if it needed a dummy variable and would have created them if not. However, once I ran the regression model, looking at the output, I couldnt see any new variables that were supposed to be dummies.

$\endgroup$
4
  • 4
    $\begingroup$ Did you make the variable a "factor" in R? If not, R would have assumed it was numeric, not something it needed to create dummy variables for. $\endgroup$
    – jbowman
    Dec 26, 2017 at 21:41
  • $\begingroup$ I didnt not. That seems to be the best thing to do at the moment. Would that be an acceptable option though? (changing it to a factor and then carrying out the regression?) $\endgroup$ Dec 26, 2017 at 21:44
  • 1
    $\begingroup$ Seems reasonable. Just be aware R will select a referent group automatically for dummy coding, when you code the variable as a factor. You might be happy with its default selection of referent group, or you might not (in which case you will need to manually [re]code it). $\endgroup$
    – jsakaluk
    Dec 26, 2017 at 22:09
  • $\begingroup$ variable <- factor(YSK87,labels=c("label1","label2","labeln")) $\endgroup$ Dec 27, 2017 at 4:35

1 Answer 1

1
$\begingroup$

You can try to use the R function ifelse(test_expression,x,y). (reference: https://www.programiz.com/r-programming/ifelse-function)

In your case, refer to the following code (person is a variable name): ifelse(person==1,1,0)->new1 ifelse(person==2,1,0)->new2 ifelse(person==3,1,0)->new3 ifelse(person=>4,1,0)->new4

Or if you want to carry out the regression, try to use this code according to the above comment,
lm(Y~factor(person), data=XXX).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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