harrelfe / rms

Regression Modeling Strategies
https://hbiostat.org/R/rms
Other
170 stars 48 forks source link

Predict(): Error in reformulate(attr(termobj, "term.labels")[-dropx], response = if (keep.response) termobj[[2L]] #151

Closed hhh988 closed 1 month ago

hhh988 commented 1 month ago

code

n <- 1000 # define sample size set.seed(17) # so can reproduce the results age <- rnorm(n, 50, 10) blood.pressure <- rnorm(n, 120, 15) cholesterol <- rnorm(n, 200, 25) sex <- factor(sample(c('female','male'), n,TRUE)) label(age) <- 'Age' # label is in Hmisc label(cholesterol) <- 'Total Cholesterol' label(blood.pressure) <- 'Systolic Blood Pressure' label(sex) <- 'Sex' units(cholesterol) <- 'mg/dl' # uses units.default in Hmisc units(blood.pressure) <- 'mmHg'

Specify population model for log odds that Y=1

L <- .4(sex=='male') + .045(age-50) + (log(cholesterol - 10)-5.2)(-2(sex=='female') + 2*(sex=='male'))

Simulate binary y to have Prob(y=1) = 1/[1+exp(-L)]

y <- ifelse(runif(n) < plogis(L), 1, 0)

ddist <- datadist(age, blood.pressure, cholesterol, sex) options(datadist='ddist')

fit <- lrm(y ~ blood.pressure + sex * (age + rcs(cholesterol,4)))

Predict(fit, age, cholesterol, np=4)

error

Error in reformulate(attr(termobj, "term.labels")[-dropx], response = if (keep.response) termobj[[2L]], : 'termlabels'必需是长度至少为一的字节矢量

The version of the R is 4.3.1. The version of the rms package is 6.8-1. The version of Hmisc package is 5.1-3. I'm getting this error when I run the example in the help. Does anyone have any idea what this is about?

arani11 commented 1 month ago

Similar error message: "Error in reformulate(attr(termobj, "term.labels")[-dropx], response = if (keep.response) termobj[[2L]], : 'termlabels' must be a character vector of length at least one"

Code: [ I have mutate sex to display 0 or 1 as I thought it would help with the "labels" issue]

run lrm with age and sex

train = train %>% mutate(sex2 = ifelse(train$sex == "Male",0,1)) fm <- lrm(train$psa_specific_outcome ~ age + sex2, data= train, x=TRUE, y=TRUE)

Select variables to include in prediction for test set dataframe

create a matrix with only the variables to be included

test$sex2 <- ifelse(test$sex == "Male",0,1) test2 <- test %>% select(psa_specific_outcome,age,sex2) test2 <- data.frame(test2);

Predict outcome in TEST DATA

pred.logit <- predict(fm, test2)

harrelfe commented 1 month ago

To fix this install the very latest version of R as well as rms.