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
I've solve this problem
There is no dummies package right now,,, so we have to use dummy package like below