-3

we tried to create dummies for oecd, oil and inter from package "AER" data "GROWTHDJ". We Know which command to use. i mean with(rep). But we could not create dummies. can anyone help? thanks.

  • 1
    Welcome on SO, please provide a minimal working example for your problem. – David Heckmann Mar 05 '16 at 17:10
  • 1
    Please, edit your question with a small example dataset, and the expected outcome. It will result in a more useful question and it will be easier to get help. Otherwise we are guessing. http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example will help for the example – user20650 Mar 05 '16 at 17:20
  • newdata<-na.omit(GrowthDJ) then we have to create dummy for oecd. – kamil ahmedov Mar 05 '16 at 17:28
  • dummy<-rep(1:0, yes:no, data=oecd) – kamil ahmedov Mar 05 '16 at 17:54
  • All three variables mentioned are factor variables. Therefore, when including these variables in a regression in R (e.g., `lm()`) will automatically set up the desired dummies. You don't have to do this by hand in advance. If you are really determined to create the dummies beforehand, you can do something like `GrowthDJ$oecddummy <- as.numeric(GrowthDJ$oecd) - 1`. This will do what you want because internally the factor `oecd` is represented by integers `1` and `2`. Subtracting -1 will thus yield a 0/` dummy. – Achim Zeileis Mar 06 '16 at 22:57
  • thanks a lot. and i have done model.matrix(~oecd+oil+inter,data=mydata) mydata after omitting the GrowthDJ. then i have included this dummies(i called "mydummies") into my regression. i guess i did not do anything wrong. if so then notice me. – kamil ahmedov Mar 06 '16 at 23:21

1 Answers1

1

is this what you are looking for?:

library( "AER" )
data("GrowthDJ")
model.matrix( ~ oil + inter + oecd , data = GrowthDJ)
David Heckmann
  • 2,899
  • 2
  • 20
  • 29
  • 1
    could you be more specific? How does the answer differ from your expected result? – David Heckmann Mar 05 '16 at 17:49
  • we used data GrowthDJ. then we create new data frame which excludes observations with missing data using na.omit command. and now we have to create dummie variable for oecd, oil and inter. variables from data(Growth). then we will have to use these dummies for our regression. – kamil ahmedov Mar 05 '16 at 18:05
  • If you want all levels of the factors represented in the matrix, you can do this by defining `contrasts.arg`. ie `contrasts.arg = lapply(GrowthDJ[c('oil', 'inter', 'oecd')], contrasts, contrasts=FALSE)`. Of course, you cant use this in a ordinary linear regression due to linear dependence - you would use what David has provided. – user20650 Mar 05 '16 at 18:14