展示结果
创建模型的最后一步,也是最重要的一步之一,就是分享结果。
在本练习中,您将提取县级估计值,并使用 ggplot2 进行可视化。县级随机效应的斜率需要与固定效应的斜率相加,才能得到每个县的斜率估计。
除了相加之外,代码还会按照犯罪率(斜率估计)对县进行排序,以便更清晰地展示数据。
您之前拟合的模型 glmer_out 已为您加载。
本练习是课程的一部分
R 中的分层与混合效应模型
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Extract out the fixed-effect slope for Year2
Year2_slope <- fixef(___)['Year2']
# Extract out the random-effect slopes for county
county_slope <- ranef(___)$County
# Create a new column for the slope
county_slope$slope <- county_slope$Year2 + Year2_slope
# Use the row names to create a county name column
county_slope$county <- rownames(county_slope)
# Create an ordered county-level factor based upon slope values
county_slope$county_plot <- factor(county_slope$county,
levels = county_slope$county[order(county_slope$slope)])