2
$\begingroup$

(this question I originally posted in stack overflow)

I want to know if I am interpreting the factor() function in R correctly. Suppose I have a variable with 10 levels let's say these are the different exposures of a study plan. I make it a factor using either factor() or as.factor(). When seeing the summary of my lm() (making up an example):

I know by default factor1 is treated as a reference in r so it is not usually output.

intercept: 10 factor2 = 30 factor3 = 10 factor4 = 57 factor5 = 4 factor6 = 34 factor7 = 56 factor8 = 89 factor9 = 78 factor10 = 8

Additionally, since these are 10 factors (all of the factors are dichotomous, 0 is did not get the study type, 1 is that they did and each participant can only have one study type) this would mean that in the example if someone got factor4 as their treatment/study type, that their score is multiplied by the coefficient which is in this case is 1 (which would equal 57) and added to the intercept? Just want to make sure I am interpreting this correctly.

my model formula using lm() is: test scre = intercept + factor2 + ... + factor10

$\endgroup$
5
  • 1
    $\begingroup$ It sounds like you are not interpreting it correctly but then you don't provide enough information. At the very least, what's the model formula? You mention score but score doesn't appear in your redacted output summary. $\endgroup$
    – dipetkov
    Commented Apr 13, 2022 at 18:17
  • $\begingroup$ @dipetkov sorry and thanks, I edited it $\endgroup$
    – ineedhelp
    Commented Apr 13, 2022 at 19:14
  • $\begingroup$ Did you do any pre-processing to convert the original categorical variable into the nine binary dummy variables? $\endgroup$
    – dipetkov
    Commented Apr 13, 2022 at 19:47
  • $\begingroup$ @dipetkov, no I originally did factor(x = df$StudyType, levels = c(1,2,3,4,5,6,7,8,9,10), labels = c("1","2","3","4","5","6","7","8","9,"10")) but it was not working for some odd reason so instead I used as.factor(df$StudyType) $\endgroup$
    – ineedhelp
    Commented Apr 13, 2022 at 19:55
  • 1
    $\begingroup$ Neither factor nor as.factor generates dummy variables, so the model formula test scre = intercept + factor2 + ... + factor10 doesn't make sense. You seems to have at least two questions: 1. How to properly encode my categorical variable in R? This is a programming question and you should ask it on Stack Overflow. 2. Once you have fitted the model, if you have questions about interpreting the results then that will be an appropriate question for CV. $\endgroup$
    – dipetkov
    Commented Apr 13, 2022 at 20:04

1 Answer 1

2
$\begingroup$

Let's write out exactly what's going on when you say that a subject belongs to factor level $5$.

$x_i = (0, 0, 0, 1, 0, 0, 0, 0, 0)$ to denote belonging to level $\#5$, since level $\#1$ is subsumed by the intercept. Therefore, your regression equation gives you:

$$ \hat y_i = \hat\beta_0 + 0\hat\beta_1+0\hat\beta_2+0\hat\beta_3+1\hat\beta_4+ 0\hat\beta_5+0\hat\beta_6+ 0\hat\beta_7+0\hat\beta_8+0\hat\beta_9\\\implies\\\hat y_i = \hat\beta_0 + \hat\beta_4 $$

It looks like this is what your reasoning would give.

As a quick comment, we typically refer to one factor variable having multiple levels, so it would be more consistent with common terminology to refer to your variables as level2 and level3 (etc), rather than factor2 and factor3 (etc). Your terminology makes it sound like multiple factor variables, each of which can have different numbers of levels.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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