1
$\begingroup$

I am learning this post, in which, the author gives the piece of code:

outcome<-dataf1$outcome
dataf1$outcome<-NULL
datafd<-dummy.data.frame(dataf1)
hdata<-cbind(data,datafd,outcome)
head(str(hdata))
data<-hdata

Other parts are easy to understand though, what does this line: dummy.data.frame(dataf1) do?

I constructed a tiny dataset to figure this out:

title <- c('engineer','manager','sales','engineer','sales')
salary <- c(51000, 33700, 26800, 53700, 36800)
df = data.frame(title, salary)
df
dummy.data.frame(df)

Which produces:

  titleengineer titlemanager titlesales salary
1             1            0          0  51000
2             0            1          0  33700
3             0            0          1  26800
4             1            0          0  53700
5             0            0          1  36800

I cannot imagine what this is used for.

$\endgroup$
1
$\begingroup$

The original df looks like:

     title salary
1 engineer  51000
2  manager  33700
3    sales  26800
4 engineer  53700
5    sales  36800

Using dummy.data.frame(df), the character column "title" is recoded as "one-hot" (aka as dummy variable). Each column =1 if title=TRUE (or zero otherwise).

  titleengineer titlemanager titlesales salary
1             1            0          0  51000
2             0            1          0  33700
3             0            0          1  26800
4             1            0          0  53700
5             0            0          1  36800

An alternative encoding would be to use "title" as a factor with three levels df$title = as.factor(df$title).

However, in many cases "one hot" can be digested better by ML algorithms.

Simple example:

title <- c('engineer','manager','sales','engineer','sales')
salary <- c(51000, 33700, 26800, 53700, 36800)
df = data.frame(title, salary)
df

library(dummies)
df2 = dummy.data.frame(df)
df2

OLS model with "factor":

df$title = as.factor(df$title)
ols = lm(salary~., data=df)
summary(ols)

Output:

Call:
lm(formula = salary ~ ., data = df)

Residuals:
         1          2          3          4          5 
-1.350e+03  5.684e-14 -5.000e+03  1.350e+03  5.000e+03 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)   
(Intercept)     52350       3662  14.295  0.00486 **
titlemanager   -18650       6343  -2.940  0.09882 . 
titlesales     -20550       5179  -3.968  0.05804 . 
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 5179 on 2 degrees of freedom
Multiple R-squared:  0.8992,    Adjusted R-squared:  0.7983 
F-statistic: 8.918 on 2 and 2 DF,  p-value: 0.1008

OLS model with "dummies":

ols2 = lm(salary~titlemanager+titlesales, data=df2)
summary(ols2)

Output:

Call:
lm(formula = salary ~ titlemanager + titlesales, data = df2)

Residuals:
         1          2          3          4          5 
-1.350e+03  5.684e-14 -5.000e+03  1.350e+03  5.000e+03 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)   
(Intercept)     52350       3662  14.295  0.00486 **
titlemanager   -18650       6343  -2.940  0.09882 . 
titlesales     -20550       5179  -3.968  0.05804 . 
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 5179 on 2 degrees of freedom
Multiple R-squared:  0.8992,    Adjusted R-squared:  0.7983 
F-statistic: 8.918 on 2 and 2 DF,  p-value: 0.1008

Summary:

Results are the same. Dummies are a different representation of R's factors. Sometimes you need to pass content explicity as dummies. In this case model.matrix() often is useful. https://www.rdocumentation.org/packages/stats/versions/3.6.0/topics/model.matrix

|improve this answer|||||
$\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.