Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

model.matrix using multiple columns

I'm trying to use multiple columns from a data.frame in a model.matrix.

The data frame looks like this:

    df1 <- data.frame(id=seq(1,10,1), zip1=(round(runif(10)*100000,0)), zip2=(round(runif(10)*100000,0))
    ,var1=round(runif(10)*100,1),Sales=round(runif(10)* 10000,2))
df1$zip1 <- ifelse(nchar(as.character(df1$zip1)) < 5,paste("0",as.character(df1$zip1),sep=""),df1$zip1)
df1$zip2 <- ifelse(nchar(as.character(df1$zip2)) < 5,paste("0",as.character(df1$zip2),sep=""),df1$zip2)

df1$zip1 <- factor(df1$zip1)
df1$zip2 <- factor(df1$zip2)
dput(df1)


> dput(df1)
structure(list(id = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), zip1 = structure(c(5L, 
1L, 8L, 3L, 7L, 9L, 2L, 6L, 10L, 4L), .Label = c("16667", "21922", 
"29100", "54398", "55447", "72607", "84667", "96562", "97012", 
"99125"), class = "factor"), zip2 = structure(c(7L, 4L, 2L, 6L, 
3L, 8L, 5L, 9L, 1L, 10L), .Label = c("0451", "0644", "14040", 
"17184", "18838", "42308", "48507", "50496", "64851", "89748"
), class = "factor"), var1 = c(94.4, 39.6, 47.1, 74, 67.3, 53.4, 
28.7, 91.6, 47.1, 44.8), Sales = c(6394.03, 5575.65, 773.58, 
3181.43, 4992.56, 6627.01, 3313.13, 1585.1, 5080.98, 499.86)), .Names = c("id", 
"zip1", "zip2", "var1", "Sales"), row.names = c(NA, -10L), class = "data.frame")

and looks like this:

    > df1
   id  zip1  zip2 var1   Sales
1   1 55447 48507 94.4 6394.03
2   2 16667 17184 39.6 5575.65
3   3 96562  0644 47.1  773.58
4   4 29100 42308 74.0 3181.43
5   5 84667 14040 67.3 4992.56
6   6 97012 50496 53.4 6627.01
7   7 21922 18838 28.7 3313.13
8   8 72607 64851 91.6 1585.10
9   9 99125  0451 47.1 5080.98
10 10 54398 89748 44.8  499.86

I'd like to do something like:

df2 <- model.matrix(~df1[,-c(1,5)])
glmnet1 <- cv.glmnet(df2, df1[,"Sales"]
    , family="gaussian", alpha=.95, nfolds=10, standardize = FALSE, type.measure="deviance")

However I can't get the model.matrix to take more than one variable:

> f1 <- formula(df1$Sales ~ df1[,c("zip1","zip2")])
> df2 <- model.matrix(f1)
  Error in model.frame.default(object, data, xlev = xlev) : 
  invalid type (list) for variable 'df1[, c("zip1", "zip2")]'

Any suggestions?

EDITS:

> f1 <- formula(Sales ~., data= df1[,-c(1,5)])
> df2 <- model.matrix(f1)
Error in terms.formula(object) : '.' in formula and no 'data' argument
like image 635
screechOwl Avatar asked Dec 11 '22 20:12

screechOwl


1 Answers

You can specify multiple variables to your formula like:

model.matrix(Sales ~ zip1 + zip2, data=df1)

If you want to save typing things out, try:

formdf1 <- as.formula(paste("Sales ~ ", paste(names(df1)[2:3],collapse="+")))
formdf1
#Sales ~ zip1 + zip2

and then run:

model.matrix(formdf1,data=df1)
like image 176
thelatemail Avatar answered Dec 28 '22 07:12

thelatemail