含分类变量的建模
在之前的练习中,您已经拟合了一个以 color 和 width 为解释变量的逻辑回归模型,其中将 color 当作定量变量处理。本练习中,您将把 color 视为分类变量;当构建设计矩阵时,会将 color 编码为 3 个使用 0/1 编码的变量。
回顾:dmatrix() 的默认编码使用第一个组作为参照组。若要将模型矩阵以 dataframe 形式查看,需要在 dmatrix() 中设置额外参数 return_type 为 'dataframe'。
color 变量具有如下的自然排序:
1:medium light
2:medium
3:medium dark
4:dark
工作区中已预加载 crab 数据集。
本练习是课程的一部分
Python 中的广义线性模型
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Construct model matrix
model_matrix = ____('C(____, ____(____))' , data = ____,
return_type = 'dataframe')
# Print first 5 rows of model matrix dataframe
print(____.____)
# Fit and print the results of a glm model with the above model matrix configuration
model = ____('____ ~ ____(____, ____(____))', data = ____,
family = ____).____
print(____.____)