Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7507849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:34:02+00:00 2026-05-29T22:34:02+00:00

I have a data set that I use the model.matrix() function on to convert

  • 0

I have a data set that I use the model.matrix() function on to convert factor variables to dummy variables. My data has 10 columns like this each with 3 levels (2,3,4) and I’ve been creating dummy variables for each of them separately.

xFormData <- function(dataset){
    mm0 <- model.matrix(~ factor(dataset$type) , data=dataset)
    mm1 <- model.matrix(~ factor(dataset$type_last1), data = dataset)
    mm2 <- model.matrix(~ factor(dataset$type_last2), data = dataset)
    mm3 <- model.matrix(~ factor(dataset$type_last3), data = dataset)
    mm4 <- model.matrix(~ factor(dataset$type_last4), data = dataset)
    mm5 <- model.matrix(~ factor(dataset$type_last5), data = dataset)
    mm6 <- model.matrix(~ factor(dataset$type_last6), data = dataset)
    mm7 <- model.matrix(~ factor(dataset$type_last7), data = dataset)
    mm8 <- model.matrix(~ factor(dataset$type_last8), data = dataset)
    mm9 <- model.matrix(~ factor(dataset$type_last9), data = dataset)
    mm10 <- model.matrix(~ factor(dataset$type_last10), data = dataset)

    dataset <- cbind(dataset, mm0, mm1, mm2, mm3, mm4, mm5, mm6, mm7, mm8, mm9, mm10)

dataset
}

I’m wondering if this is the wrong procedure as after running a randomForest on the data, and plotting the variable importance, it was showing different dummy variable columns individually. So say columns 61-63 were the 3 dummy variables for column 10, the randomForest is seeing column 62 by itself as an important predictor.

I have 2 questions:

1) Is this ok?

2) If not, how can I group the dummy variables so that the rf knows they are together?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T22:34:03+00:00Added an answer on May 29, 2026 at 10:34 pm

    This is OK, and is what happens behind the scenes anyway if you left the factors as factors. Different levels of a factor are different features for most machine learning purposes. Think of a random example like test outcome ~ school: Maybe going to school A is very predictive of whether you pass or fail the test, but not school B or school C. Then, the school A feature would be useful, but not the others.

    This is covered in one of the caret vignette documents: http://cran.r-project.org/web/packages/caret/vignettes/caretMisc.pdf

    Also, the cars data set included with caret should be a useful example. It contains 2 factors – “manufacturer” and “car type” – that have been dummy-coded into a series of numeric features for machine learning purposes.

    data(cars, package='caret')
    head(cars)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data set that looks like this: 000 100 200 300 010
I have a data set that is around 700 rows with eight columns of
I have a set of data that is structured like this: ItemA.GroupA ItemB.GroupA ItemC.GroupB
I have a model that looks like this and stores data as key-value pairs.
I have a class that serves as a data model. I'll simplify it like
I have a data set that is organized in the following manner: Timestamp|A0001|A0002|A0003|A0004|B0001|B0002|B0003|B0004 ...
I have the following data set that I am trying to plot with ggplot2,
I have a very large possible data set that I am trying to visualize
We have this set of data that we need to get the average of
I have a set of data that models a hierarchy of categories. A root

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.