I am trying to build a linear regression model for my data which has following variables.

[1] "Productcode"   "Category"    "Month"   "Mode.of.operations" "sales"   "profit.margin"     
 [7] "Name"         "Packaging.content"  "Specifications"     "Unit       "Origin"   

Now as some of my variables are non numeric e.g Origin has values which are names of cities and countries, Mode of operation has values (joint venture, reseller, distributor). In non numeric variable i dont know how to represent it in my linear regression model. One way I can think is to assign numeric values to these variables e.g (Joint venture =1 , Reseller = 2 and Distribution =3) but then it won't be right because it implies Distribution is better or 3 times higher than Joint venture.

Can anyone guide me how to solve this problem in R.

  • 2
    $\begingroup$ You treat them as factors. If you leave them as text labels R should convert them to factors when you read them in. $\endgroup$ – Glen_b -Reinstate Monica Aug 18 '15 at 7:44
  • $\begingroup$ I agree with @Glen_b (above) and Heroka (below). No need to worry as R will create dummy variables based on the values (names) of your "Origin" column/variable. One thing you might need to take into account is the number of different values you have for "Origin". If you have many cities (eg. 200 cities in a dataset with 1000 rows) you might need to find a way to group/cluster those cities. Or if you have some cities represented by 1-2 rows in your dataset. $\endgroup$ – AntoniosK Aug 18 '15 at 8:49
  • $\begingroup$ You might benefit from reading chapter 4 in 'An introduction to R' which ships with your copy of R. $\endgroup$ – mdewey Jul 11 '16 at 12:59
  • $\begingroup$ If you end up converting your variable into 0, 1, or 2 (as suggested below), you should factorize them. You can use "as.factor()" function. But the best approach would be to leave them as characters. $\endgroup$ – Zamir Akimbekov Jul 11 '16 at 15:04

[I am assuming this is an issue with one of your independent variables. If it's in your dependent variable, linear regression is not the way to go]

You are right that assigning numeric variables is the wrong way to go. In linear regression with non-numeric (or categorical) independent variables, you want a coefficient for each category (except a default one). You need the variable to be a factor. You can either let R do this for you, by just adding the variable as-is to the model, or convert it to a factor yourself. That way, you can set which mode of operation is the default.


You can change the categorical variables to numeric based on the type

Categorical variables - Create column for each unique & create binary flag for them

Ordinal variables (categories with logical order e.g., high low medium) can be converted to values 1,2,3 etc

Language - You can use Word2Vec or Document Term matrix

Cities - You can create actual distance from respect to one city

So all of them will be numeric & you can use Linear regression

Hope this helps.


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.