This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Please solve this problem " requires numeric/complex matrix/vector arguments"

I am trying to run open source R code. (https://atm.amegroups.com/article/view/16442/17558)

But when I tried, there was some error in this code.

library(dummy)
set.seed(666)
n <- 1500
lac<- round(rnorm(n,mean=5,sd=1.1),1)
age<- round(rnorm(n,mean=67,sd=10))
smoking<- as.factor(sample(x=c("never","ever","smoking"), 
                       size=n, 
                       replace=TRUE, 
                       prob=c(0.5,0.3,0.2)))
smoking<-relevel(smoking,ref="never")
gender<- as.factor(sample(x = c("male","female"), 
                      size = n, 
                      replace = TRUE, 
                      prob = c(60,40)))


##1##
lp<-cbind(1,dummy(smoking)[,-1]) %*% c(0.07,1.5,3.2)+ 
cbind(1, dummy(gender)[, -1]) %*% c(0.07,1.5)-
0.2*age+0.003*age^2+ #17
3*lac-0.25*lac^2-11
pi.x<- exp(lp) /(1 + exp(lp))
mort.y <- rbinom( n = n, size = 1, prob = pi.x)
df <- data.frame(mort.y, smoking, gender,lac,age)
df$dataset<-sample(x=c("train","validate"), size=n, replace=TRUE, prob=c(0.75,0.25))

In the code where I marked as ##1## above, the error message was seen when I ran this code.

Error in `colnames<-`(`*tmp*`, value = make.names(colnames(x), TRUE)) :
attempt to set 'colnames' on an object with less than two dimensions

I think, it is necessary to run cbind function with dataframe form, so I transformed 'smoking' and 'gender' to dataframe

smoking = as.data.frame(smoking)
gender = as.data.frame(gender)

After I transformed 'smoking' and 'gender' variables to dataframe, there was another error was seen.

Error in cbind(1, dummy(smoking)[, -1]) %*% c(0.07, 1.5, 3.2) : 
requires numeric/complex matrix/vector arguments

It is necessary to run %*% function with data of matrix, so I transformed this variables to matrix form, but same error message was seen after transforming

Please help

r matrix

I've solve this problem

There is no dummies package right now,,, so we have to use dummy package like below

library(dummy) 
set.seed(666) 
n <- 1500 
lac<- round(rnorm(n,mean=5,sd=1.1),1) 
age<- round(rnorm(n,mean=67,sd=10)) 
smoking<- as.factor(sample(x=c("never","ever","smoking"), 
                           size=n, 
                           replace=TRUE, 
                           prob=c(0.5,0.3,0.2))) 
smoking<-relevel(smoking,ref="never") 
gender<- as.factor(sample(x = c("male","female"), 
                          size = n, 
                          replace = TRUE, 
                          prob = c(60,40))) 

A = dummy(as.data.frame(smoking))
A$smoking_never = as.numeric(A$smoking_never)
A$smoking_ever = as.numeric(A$smoking_ever)
A$smoking_smoking = as.numeric(A$smoking_smoking)
A = as.matrix(A)
mode(A)

B = dummy(as.data.frame(gender))
B$gender_female = as.numeric(B$gender_female)
B$gender_male = as.numeric(B$gender_male)
B = as.matrix(B)
mode(B)

lp<-A %*% c(0.07,1.5,3.2)+ 
     B %*% c(0.07,1.5)- 
0.2*age+0.003*age^2+ 
3*lac-0.25*lac^2-11 
pi.x<- exp(lp) /(1 + exp(lp)) 
mort.y <- rbinom( n = n, size = 1, prob = pi.x) 
df <- data.frame(mort.y, smoking, gender,lac,age) 
df$dataset<-sample(x=c("train","validate"), 
                   size=n,
                   replace=TRUE, 
                   prob=c(0.75,0.25))

df.cont<- df[df$dataset=="train", c("mort.y","lac","age")]
df.cat<- df[df$dataset=="train", c("smoking","gender")]
ymark<-seq(0,1,0.1)

1 answer

You made a copying error, the function dummy is not from dummy package but from dummies package, which has been removed from the CRAN. You can still access to the function here : https://github.com/cran/dummies/blob/master/R/dummy.R

How can I use this package from current version of R studio??

I would just copy paste the function in your R session

I've solve this problem

There is no dummies package right now,,, so we have to use dummy package like below

library(dummy) 
set.seed(666) 
n <- 1500 
lac<- round(rnorm(n,mean=5,sd=1.1),1) 
age<- round(rnorm(n,mean=67,sd=10)) 
smoking<- as.factor(sample(x=c("never","ever","smoking"), 
                           size=n, 
                           replace=TRUE, 
                           prob=c(0.5,0.3,0.2))) 
smoking<-relevel(smoking,ref="never") 
gender<- as.factor(sample(x = c("male","female"), 
                          size = n, 
                          replace = TRUE, 
                          prob = c(60,40))) 

A = dummy(as.data.frame(smoking))
A$smoking_never = as.numeric(A$smoking_never)
A$smoking_ever = as.numeric(A$smoking_ever)
A$smoking_smoking = as.numeric(A$smoking_smoking)
A = as.matrix(A)
mode(A)

B = dummy(as.data.frame(gender))
B$gender_female = as.numeric(B$gender_female)
B$gender_male = as.numeric(B$gender_male)
B = as.matrix(B)
mode(B)

lp<-A %*% c(0.07,1.5,3.2)+ 
     B %*% c(0.07,1.5)- 
0.2*age+0.003*age^2+ 
3*lac-0.25*lac^2-11 
pi.x<- exp(lp) /(1 + exp(lp)) 
mort.y <- rbinom( n = n, size = 1, prob = pi.x) 
df <- data.frame(mort.y, smoking, gender,lac,age) 
df$dataset<-sample(x=c("train","validate"), 
                   size=n,
                   replace=TRUE, 
                   prob=c(0.75,0.25))

df.cont<- df[df$dataset=="train", c("mort.y","lac","age")]
df.cat<- df[df$dataset=="train", c("smoking","gender")]
ymark<-seq(0,1,0.1)

Log in to answer this question.