rlinear-regressiondummy-variable

Adding dummy variables in panel linear model (regression) in R


I have a categorical independent variable (with options of "yes" or "no") that I want to add to my panel linear model. According to the answer here: After generating dummy variables?, the lm function automatically creates dummy variables for you for categorical variables.

Does this mean that creating dummy variables through i.e. dummy.data.frame is unnecessary, and I can just add in my variable in the plm function and it will automatically be treated like a dummy variable (even if the data is not numerical)? And is this the same for the plm function?

Also, I don't have much data to begin with. Would it hurt if I manually turned the data into numbers (i.e. "yes"=1, "no"=0) without creating a dummy variable?


Solution

  • It is unnecessary to create dummy variables (i.e., to write code to convert factor variables to dummy variables in a data frame) for use with the lm() function. To illustrate, we'll run a regression model on the mtcars data set, using am (0 = automatic, 1 = manual transmission) as a factor variable.

    summary(lm(mpg ~ wt + factor(am),data=mtcars))
    

    ...and the output:

    > summary(lm(mpg ~ wt + factor(am),data=mtcars))
    
    Call:
    lm(formula = mpg ~ wt + factor(am), data = mtcars)
    
    Residuals:
        Min      1Q  Median      3Q     Max 
    -4.5295 -2.3619 -0.1317  1.4025  6.8782 
    
    Coefficients:
                Estimate Std. Error t value Pr(>|t|)    
    (Intercept) 37.32155    3.05464  12.218 5.84e-13 ***
    wt          -5.35281    0.78824  -6.791 1.87e-07 ***
    factor(am)1 -0.02362    1.54565  -0.015    0.988    
    ---
    Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
    
    Residual standard error: 3.098 on 29 degrees of freedom
    Multiple R-squared:  0.7528,    Adjusted R-squared:  0.7358 
    F-statistic: 44.17 on 2 and 29 DF,  p-value: 1.579e-09
    

    We can make the answer even more obvious if we use cyl as the independent variable since it has three levels and should be modeled as factor variable, not a continuous (interval / ratio level of measurement) variable.

    summary(lm(mpg ~ wt + factor(cyl),data=mtcars))  
    

    ...and the output:

    > summary(lm(mpg ~ wt + factor(cyl),data=mtcars))
    
    Call:
    lm(formula = mpg ~ wt + factor(cyl), data = mtcars)
    
    Residuals:
        Min      1Q  Median      3Q     Max 
    -4.5890 -1.2357 -0.5159  1.3845  5.7915 
    
    Coefficients:
                 Estimate Std. Error t value Pr(>|t|)    
    (Intercept)   33.9908     1.8878  18.006  < 2e-16 ***
    wt            -3.2056     0.7539  -4.252 0.000213 ***
    factor(cyl)6  -4.2556     1.3861  -3.070 0.004718 ** 
    factor(cyl)8  -6.0709     1.6523  -3.674 0.000999 ***
    ---
    Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
    
    Residual standard error: 2.557 on 28 degrees of freedom
    Multiple R-squared:  0.8374,    Adjusted R-squared:   0.82 
    F-statistic: 48.08 on 3 and 28 DF,  p-value: 3.594e-11
    

    We interpret the results for the factor variables as:

    The original poster's question essentially asks whether this code is needed:

    # alternative where you explicitly create dummy variables
    mtcars$is6cyl = ifelse(mtcars$cyl == 6,1,0)
    mtcars$is8cyl = ifelse(mtcars$cyl == 8,1,0)
    summary(lm(mpg ~ wt + is6cyl + is8cyl,data=mtcars))
    

    Notice that the output where we manually create factor variables for 6 or 8 cylinder cars (establishing 4 cylinders is the base value), and in particular the slope output for is6cyl and is8cyl exactly matches the output above.

    > summary(lm(mpg ~ wt + is6cyl + is8cyl,data=mtcars))
    
    Call:
    lm(formula = mpg ~ wt + is6cyl + is8cyl, data = mtcars)
    
    Residuals:
        Min      1Q  Median      3Q     Max 
    -4.5890 -1.2357 -0.5159  1.3845  5.7915 
    
    Coefficients:
                Estimate Std. Error t value Pr(>|t|)    
    (Intercept)  33.9908     1.8878  18.006  < 2e-16 ***
    wt           -3.2056     0.7539  -4.252 0.000213 ***
    is6cyl       -4.2556     1.3861  -3.070 0.004718 ** 
    is8cyl       -6.0709     1.6523  -3.674 0.000999 ***
    ---
    Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
    
    Residual standard error: 2.557 on 28 degrees of freedom
    Multiple R-squared:  0.8374,    Adjusted R-squared:   0.82 
    F-statistic: 48.08 on 3 and 28 DF,  p-value: 3.594e-11