R预测使用多个模型

时间:2017-03-24 13:45:33

标签: r regression glm

我是R的新手并尝试使用4种不同的GLM来预测数据集的结果。我试过作为一个大型模型运行,虽然我得到结果,模型没有正确收敛,我最终得到了N / A.因此,我有四个模型:

model_team <- glm(mydata$OUT ~ TEAM + OPPONENT, family = "binomial",data =      mydata )

model_conf <- glm(mydata$OUT ~ TCONF + OCONF, family = "binomial",data = mydata)

model_tstats <- glm(mydata$OUT ~ TPace + TORtg + TFTr + T3PAr + TTS. + TTRB. + TAST. + TSTL. + TBLK. + TeFG. + TTOV. + TORB. + TFT.FGA, family = "binomial",data = mydata)

model_ostats<- glm(mydata$OUT ~ OPace + OORtg + OFTr + O3PAr + OTS. + OTRB. + OAST. + OSTL. + OBLK. + OeFG. + OTOV. + OORB. + OFT.FGA, family = "binomial",data = mydata)

然后,我想使用四种模型使用不同的数据集来预测结果

predict(model_team, model_conf, model_tstats, model_ostats, fix, level = 0.95, type = "probs")

有没有办法将所有四个模型加入一个大型集合中?

1 个答案:

答案 0 :(得分:-3)

我真的不明白你为什么要做你正在做的事情。我也没有任何示例数据表示您正在使用的数据。但是,下面是如何使用结果系数将多个GLM组合成一个GLM的示例。请注意,如果数据集中的变量之间存在多重共线性,则无法正常工作。

# I used the iris dataset for my example
head(iris)

# Run several models
model1 <- glm(data = iris, Sepal.Length ~ Sepal.Width)
model2 <- glm(data = iris, Sepal.Length ~ Petal.Length)
model3 <- glm(data = iris, Sepal.Length ~ Petal.Width)

# Get combined intercept
intercept <- mean(
  coef(model1)['(Intercept)'],
  coef(model2)['(Intercept)'],
  coef(model3)['(Intercept)'])

# Extract coefficients
coefs <- as.matrix(
  c(coef(model1)[2],
    coef(model2)[2],
    coef(model3)[2])

# Get the feature values for the predictions
ds <- as.matrix(iris[,c('Sepal.Width', 'Petal.Length', 'Petal.Width')])
# Linear algebra: Matrix-multiply values with coefficients
prediction <- ds %*% coefs + intercept

# Let's look at the results
plot(iris$Petal.Length, prediction)