model.matrix

non-conformable arguments error from lmer when trying to extract information from the model matrix

梦想与她 提交于 2019-12-11 12:03:43
问题 I have some longitudinal data from which I'd like to get the predicted means at specified times. The model includes 2 terms, their interaction and a spline term for the time variable. When I try to obtain the predicted means, I get "Error in mm %*% fixef(m4) : non-conformable arguments" I've used the sleep data set from lmer to illustrate my problem. First, I import the data and create a variable "age" for my interaction sleep <- as.data.frame(sleepstudy) #get the sleep data # create fake

model.matrix from lists in r

落花浮王杯 提交于 2019-12-08 00:12:14
问题 I am trying to convert list of factors to matrix for example: myLists: [[1]] [1] "RA" "FZFG" "BR" [[2]] [1] "RA" [[3]] [1] "" [[4]] [1] "" to RA FZFG BR 1 1 1 1 0 0 0 0 0 0 0 0 I tried to do the following: allFactors<-c("RA","FZFG","BR") mat<-model.matrix(~allFactors, data =myLists) but have ths error: Error in data.frame(c("RA", "FZFG", "BR"), "RA", "", "", "", "", c("RA", : arguments imply differing number of rows: 3, 1, 2, 4, 5, 7, 6, 8, 9 Any help on this is appreciated. 回答1: One option

Rownames for data.table in R for model.matrix

一个人想着一个人 提交于 2019-12-07 12:30:14
问题 I have a data.table DT and I want to run model.matrix on it. Each row has a string ID, which is stored in the ID column of DT . When I run model.matrix on DT , my formula excludes the ID column. The problem is, model.matrix drops some rows because of NAs. If I set the rownames of DT to the ID column, before calling model.matrix , then the final model matrix has rownames, and I'm all set. Otherwise, I can't figure out what rows I end up with. I'm setting the rownames with rownames(DT) = DT$ID

model.matrix from lists in r

一笑奈何 提交于 2019-12-06 05:23:25
I am trying to convert list of factors to matrix for example: myLists: [[1]] [1] "RA" "FZFG" "BR" [[2]] [1] "RA" [[3]] [1] "" [[4]] [1] "" to RA FZFG BR 1 1 1 1 0 0 0 0 0 0 0 0 I tried to do the following: allFactors<-c("RA","FZFG","BR") mat<-model.matrix(~allFactors, data =myLists) but have ths error: Error in data.frame(c("RA", "FZFG", "BR"), "RA", "", "", "", "", c("RA", : arguments imply differing number of rows: 3, 1, 2, 4, 5, 7, 6, 8, 9 Any help on this is appreciated. One option is library(qdapTools) mtabulate(myLists)[-1] Or using base R table(stack(setNames(myLists, seq_along(myLists)

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

拥有回忆 提交于 2019-12-02 01:55:48
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 with the same User . Doesn't matter if in the end the amount of rows remains the same or the rows with the

R missing levels in a model.matrix

拥有回忆 提交于 2019-12-02 00:58:56
问题 I am trying to convert a data frame with categorical variables to a model.matrix but am losing levels of variables. Here's my code: df1 <- data.frame(id = 1:200, y =rbinom(200, 1, .5), var1 = factor(rep(c('abc','def','ghi','jkl'),50))) df1$var2 <- factor(rep(c('ab c','ghi','jkl','def'),50)) df1$var3 <- factor(rep(c('abc','ghi','nop','xyz'),50)) df1$var2 <- as.character(df1$var2) df1$var2 <- gsub('\\s','',df1$var2) df1$var2 <- factor(df1$var2) sapply(df1, levels) mm1 <- model.matrix(~ 0+.,df1)

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

一世执手 提交于 2019-12-01 06:31:30
This question already has an answer here: Creating dummy variables in R data.table 1 answer 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", "Sunday")) Day <- unique(dt$Week_Day) for (i in 1:length(Day)) { if (Day[i] != "Sunday") { dt[, Day[i] := ifelse(Week_Day == Day[i