model.matrix

Error in model.frame.default(object, data, xlev = xlev) : object is not a matrix

▼魔方 西西 提交于 2021-01-29 02:28:53
问题 3 days old to R and can't figure out what I'm doing wrong. I'm trying to send some columns with two way interactions into a glmnet cox model. I have some data.frame() called dtable Edit to make the code reproducible xs<-c("Col1", "Col2", "Col3") v<-c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, NA, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, NA, 25, 26, 27, 28, 29, 30) df<-data.frame(matrix(v,ncol=3)) dm<-as.matrix(df) dm<-matrix(dm[complete.cases(dm)], ncol=3) colnames(dm)<-xs dfdata<-data.frame(dm) f

sparseMatrix with numerical and categorical data

半世苍凉 提交于 2019-12-25 06:23:19
问题 I am trying to create a sparse matrix with numerical and categorical data which will be used as an input to cv.glmnet. When only numerical data is involved, I can create a sparseMatrix using the following syntax sparseMatrix(i=c(1,3,5,2), j=c(1,1,1,2), x=c(1,2,4,3), dims=c(5,2)) For categorical variables, the following approach seems to work: sparse.model.matrix(~-1+automobile, data.frame(automobile=c("sedan","suv","minivan","truck","sedan"))) My VERY sparse instance has 1,000,000

Warning message - dummy from dummies package

一个人想着一个人 提交于 2019-12-24 11:23:50
问题 I am using the dummies package to generate dummy variables for categorical variables, some with more than two categories. testdf<- data.frame( "A" = as.factor(c(1,2,2,3,3,1)), "B" = c('A','B','A','B','C','C'), "C"= c('D','D','E','D','D','E')) # #Generate dummy variables: # testdf<- cbind(testdf, dummy(testdf$C, sep='_')) testdf<- cbind(testdf, dummy(testdf$B, sep='_')) For both commands I get: Warning message: In model.matrix.default(~x - 1, model.frame(~x - 1), contrasts = FALSE) : non-list

creating a matrix of indicator variables

故事扮演 提交于 2019-12-24 07:56:27
问题 I would like to create a matrix of indicator variables. My initial thought was to use model.matrix, which was also suggested here: Automatically expanding an R factor into a collection of 1/0 indicator variables for every factor level However, model.matrix does not seem to work if a factor has only one level. Here is an example data set with three levels to the factor 'region': dat = read.table(text = " reg1 reg2 reg3 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 0 1 0 0 1 0 0 1 0 0 0 1 0 0 1 0 0 1 0 0

R: Expanding an R factor into dummy columns for every factor level

南笙酒味 提交于 2019-12-20 03:08:13
问题 I have a quite big data frame in R with two columns. I am trying to make out of the Code column ( factor type with 858 levels) the dummy variables. The problem is that the R Studio always crashed when I am trying to do that. > str(d) 'data.frame': 649226 obs. of 2 variables: $ User: int 210 210 210 210 269 317 317 317 317 326 ... $ Code : Factor w/ 858 levels "AA02","AA03",..: 164 494 538 626 464 496 435 464 475 163 ... The User column is not unique, meaning that there can be several rows

Speed up this loop to create dummy columns with data.table and set in R [duplicate]

南楼画角 提交于 2019-12-19 08:28:52
问题 This question already has an answer here : Creating dummy variables in R data.table (1 answer) Closed 3 years ago . I have a data table and I want to create a new column for each unique day, and then assign a 1 in each row where the day matches the column name I have done this using a for loop but I was wondering if there was any way to optimise it using data.table and set? Here is an example dt <- data.table(Week_Day = c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",

predict.glmnet: Some Factors Have Only One Level in New Data

白昼怎懂夜的黑 提交于 2019-12-13 04:25:17
问题 I've trained an elastic net model in R using glmnet and would like to use it to make predictions off of a new data set. But I'm having trouble producing the matrix to use as an argument in the predict() method because some of my factor variables (dummy variables indicating the presence of comorbidities) in the new data set only have one level (the comorbidities were never observed), which means I can't use model.matrix(RESPONSE ~ ., new_data) because it gives me the (expected) Error in

predict and model.matrix give different predicted means within levels of a factor variable

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-13 01:46:57
问题 This question arose as a result of another question posted here: non-conformable arguments error from lmer when trying to extract information from the model matrix When trying to obtain predicted means from an lmer model containing a factor variable, the output varies depending on how the factor variable is specified. I have a variable agegroup, which can be specified using the groups "Children <15 years", "Adults 15-49 years", "Elderly 50+ years" or "0-15y", "15-49y", "50+y". My choice

R model.matrix using same factor set among all columns

依然范特西╮ 提交于 2019-12-12 19:21:16
问题 I have a set of basketball lineup data with five columns, each sharing the same factor, like so: head(dat) V1 V2 V3 V4 V5 1 MILES,KEATON KINGSLEY,MOSES BELL,ANTHLON HANNAHS,DUSTY DURHAM,JABRIL 2 MILES,KEATON KINGSLEY,MOSES BELL,ANTHLON HANNAHS,DUSTY DURHAM,JABRIL 3 KINGSLEY,MOSES BELL,ANTHLON HANNAHS,DUSTY DURHAM,JABRIL THOMPSON,TREY 4 KINGSLEY,MOSES BELL,ANTHLON HANNAHS,DUSTY THOMPSON,TREY BEARD,ANTON 5 THOMPSON,TREY BEARD,ANTON KOUASSI,WILLY WHITT,JIMMY WATKINS,MANUALE 6 THOMPSON,TREY BEARD

R change categorical data to dummy variables

纵然是瞬间 提交于 2019-12-12 04:58:00
问题 I have a multi-variant data frame and want to convert the categorical data inside to dummy variables, I used model.matrix but it does not quite work. Please refer to the example below: age = c(1:15) #numeric sex = c(rep(0,7),rep(1,8)); sex = as.factor(sex) #factor bloodtype = c(rep('A',2),rep('B',8),rep('O',1),rep('AB',4));bloodtype = as.factor(bloodtype) #factor bodyweight = c(11:25) #numeric wholedata = data.frame(cbind(age,sex,bloodtype,bodyweight)) model.matrix(~.,data=wholedata)[,-1] The