开始使用免费开始使用

拟合模型以预测自行车租赁次数

在本练习中,您将构建一个模型,根据天气、日期类型(假日、工作日或周末)以及一天中的时间,来预测某一小时内租出的自行车数量。您将基于 7 月份的数据来训练模型。

该数据框包含以下列:

  • cnt:该小时内租出的自行车数量(输出)
  • hr:一天中的小时(0-23,因子)
  • holiday:TRUE/FALSE
  • workingday:如果既不是假日也不是周末则为 TRUE,否则为 FALSE
  • weathersit:分类型,"Clear to partly cloudy"/"Light Precipitation"/"Misty"
  • temp:标准化摄氏温度
  • atemp:标准化体感摄氏温度
  • hum:标准化湿度
  • windspeed:标准化风速
  • instant:时间索引——自数据集开始以来的小时数(不是变量)
  • mnthyr:月份与年份索引(不是变量)

请记住,使用 glm()docs)拟合计数模型时,必须指定 family = poissonfamily = quasipoisson

由于输入变量较多,为了方便,我们将把输出和输入分别放在变量中,并使用 paste()docs)拼接出表示模型公式的字符串。

bikesJuly 数据框可直接使用。输出变量名和输入变量名也已分别加载为 outcomevars

本练习是课程的一部分

R 中的监督学习:回归

查看课程

练习说明

  • 填写空白,创建将 cnt 表达为输入函数的公式 fmla。打印该公式。
  • 计算 bikesJuly$cnt 的均值(mean())和方差(var())。
    • 您应该使用 poisson 还是 quasipoisson 回归?
  • 使用 glm()bikesJuly 数据上拟合模型:bike_model
  • 使用 glance() 查看模型的拟合统计量。将 glance() 的输出赋给变量 perf
  • 计算模型的伪 R 平方。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# bikesJuly is available
str(bikesJuly)

# The outcome column
outcome 

# The inputs to use
vars 

# Create the formula string for bikes rented as a function of the inputs
(fmla <- paste(___, "~", paste(___, collapse = " + ")))

# Calculate the mean and variance of the outcome
(mean_bikes <- ___)
(var_bikes <- ___)

# Fit the model
bike_model <- ___

# Call glance
(perf <- ___)

# Calculate pseudo-R-squared
(pseudoR2 <- ___)
编辑并运行代码