在glm中使用predict()函数 [英] Using the predict() function with glm

查看:162
本文介绍了在glm中使用predict()函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我有以下数据集,并且正在R中使用glm运行回归模型.我有系数,但是我想预测接下来的几个月"值(访问).在本示例中,我将如何处理.

Let's say that I have the following data set and am running a regression model using glm in R. I have the coefficients, but I want to predict "next months" value (visits). How would I go about that in this example.

d <- data.frame(month = c("jan", "feb", "mar", "apr", "may", "june"),
                visit =  c( 1,  2,  4,  8, 16, 32),
                click =  c(64, 62, 36,  5,  6,  3),
                conv =  c(1, 3, 6, 2, 3, 8))
d
dFit <- glm(visit ~ click + conv, data=d)

对于7月,我如何使用R中的predict()函数来预测访问次数(响应变量)?

For July, how can I use the predict() function in R to predict the number of visits (response variable)?

我最终想要得到的是我所拥有的输出

What I'm trying to eventually get is an out put where I have

Mon   Pred_clicks
jan   20
feb   25
mar   21
apr   31
may   15
june  21 
july  50

这不是我想要的输出

> predict(dFit)
        1         2         3         4         5         6 
-3.452974  1.223969 13.533457 12.235771 14.113888 25.345890 

推荐答案

由于您使用包含列monthclickconv列的data.frame训练了模型,因此必须提供data.frame也可以预测值:

Since you trained the model with a data.frame which contained the columns month, click and conv, you will have to provide such a data.frame to predict the values as well:

 predict(dFit, data.frame(month="july", conv=mean(d$conv), click=mean(d$click)))

mean(d$conv)mean(d$click)是7月份相应数量的预测值.如果您具有7月份的convclick的实际值,请将其替换为语句以获取预测.

The mean(d$conv) and mean(d$click) are the predicted values for the respective quantities for the month of July. If you have the actual values of conv and click for the month of July, substitute them in the statement to get your prediction.

但是,这可能并不是您要查找的,并且GLM回归可能不是此类时间序列数据的最佳模型.我认为您可能希望使用 VAR 作为预测模型.

However, that is probably not what you are looking for and GLMs regression may not be the best model to for this sort of time series data. I think you would want to use VAR as your predictive model.

这篇关于在glm中使用predict()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆