R错误 predictor not in model

  统计/机器学习 回归分析 R    浏览次数:4133        分享
0

setwd('c:/myr')

getwd()

data = read.csv(file='c:/myr/Wage.csv')

age.logit = data$age

wage.logit = vector(length = length(age.logit))

for (i in 1:length(age.logit))

{

  if(data$wage[i] > 200)

    wage.logit[i] = 1

  else

    wage.logit[i] = 0

}

#data for logistic model

data.logit = data.frame(age.logit, wage.logit)

###################################################

library(splines)

###################################################

#generate 3 pieces of spline basis , plot

###################################################

bspline = bs(age.logit, df = 3)

data.plot = cbind(age.logit, bspline)

data.plot.sort = data.plot[order(data.plot[, 1]), ]

matplot(age.logit, bspline)

matplot(data.plot.sort[,1], data.plot.sort[, 2:4], type = "l")

###################################################

#logistic cubic spline regression

###################################################

fit = glm(wage.logit ~ bs(age.logit, df = 3), family = binomial(link = "logit"))

summary(fit)

fit=update(fit)

#logit prediction

newdata = seq(20,60)

newdata1 = list(age.logit = newdata)

n<-1000

age<-50+12*rnorm(n)

pred = predict.glm(fit, newdata = list(age.logit = newdata),se.fit = TRUE)


HRpred = Predict(fit,newdata,fun=exp, ref.zero = TRUE)   #报错


Error in Predict(fit, newdata, fun = exp, ref.zero = TRUE) :

  predictors(s) not in model: newdata


为什么使用Predict就不行呢

 

constant007   2020-04-05 17:28



   1个回答 
1

将fit = glm(wage.logit ~ bs(age.logit, df = 3), family = binomial(link = "logit")) 改成fit = lrm(wage.logit ~ bs(age.logit, df = 3), family = binomial(link = "logit"))就可以了

SofaSofa数据科学社区DS面试题库 DS面经

玉面小嫣然   2020-08-23 21:41



  相关讨论

R用lm做线性回归的时候,interaction term是冒号还是乘号?

LOWESS回归

R里什么library有ElasticNet模型?

R里怎么对程序运行计时?

怎么对R的data.frame新增加一列?

R里paste0函数是什么意思?

在R里面怎么把一系列类似的data.frame放在一起

R里面dir函数是什么意思?

R里glmnet的lambda.min.ratio参数是什么意思?

R里线性回归结果中最后的F-statistic什么意思?有什么用?

  随便看看

怎么把pandas dataframe中一列英文文本全部转成小写?

逻辑回归模型中变量的系数怎么理解?

AIC可以是负数吗?

tsne被忽视?tsne有什么缺点?

线性回归是机器学习算法吗?