2

我有以下结构的数据集:

> data("household", package="HSAUR2")
> household[c(1,5,10,30,40),]
   housing food goods service gender total
1      820  114   183     154 female  1271
5      721   83   176     104 female  1084
10     845   64  1935     414 female  3258
30    1641  440  6471    2063   male 10615
40    1524  964  1739    1410   male  5637

“总计”列是前四列的总和。这是一个家庭的支出,分为四类。

现在,如果我想要一个性别与总支出的条件密度图,我可以:

cdplot(gender ~ total, data=household)

我会得到这张图片:

在此处输入图像描述

我想要在 x 轴上显示“总”支出的同一张图片,但在 y 轴上显示四个类别(住房、食品、商品、服务)的条件分布。我只能想到一个非常肮脏的黑客,我在其中生成一个因子,并且对于第一条数据线,我重复“住房”820 次,然后“食物”114 次,等等。

必须有一个更简单的方法,对吧?

4

1 回答 1

3

正如我所说,您使用错误的工具来获得您想要的东西。您正在设想一个无法直接从您的数据中获得的图(见底部)。

相反,您需要对数据进行建模。具体来说,您希望将每个类别中的预期支出部分预测为总支出的函数。然后,您所设想的图显示了该模型的拟合值(即,任何区域的预测支出比例)作为总支出的函数。这是一些使用loess曲线执行此操作的代码。我绘制了原始数据和拟合值,以向您展示发生了什么。

# setup the data
data("household", package = "HSAUR2")
household$total <- rowSums(household[,1:4])
household <- within(household, {
    housing <- housing/total
    food <- food/total
    goods <- goods/total
    service <- service/total
})

# estimate loess curves
l_list <-
list(loess(housing ~ total, data = household),
     loess(food ~ total, data = household),
     loess(goods ~ total, data = household),
     loess(service ~ total, data = household))

# stack fitted curves on top of one another
ndat <- data.frame(total = seq(min(household$total), max(household$total), 100))
p <- lapply(l_list, predict, newdata = ndat)
for(i in 2:length(l_list))
    p[[i]] <- p[[i]] + p[[i-1]]

# plot
plot(NA, xlim=range(household$total), ylim = c(0,1), xlab='Total', ylab='Percent', las=1, xaxs='i')
# plot dots
with(household, points(total, housing, pch = 20, col = palette()[1]))
with(household, points(total, housing + food, pch = 20, col = palette()[2]))
with(household, points(total, housing + food + goods, pch = 20, col = palette()[3]))
with(household, points(total, housing + food + goods + service, pch = 20, col = palette()[4]))
# plot fitted lines
for(i in 1:length(p))
    lines(ndat$total, p[[i]], type = 'l', lwd = 2, col = palette()[i])

结果:

在此处输入图像描述

如果您尝试根据原始数据创建这样的图,它看起来会有些奇怪,但也许这就是您想要的:

plot(NA, xlim=range(household$total), ylim = c(0,1), xlab='Total', ylab='Percent', las=1, xaxs='i')
with(household, lines(total[order(total)], housing[order(total)], pch = 20, col = palette()[1]))
with(household, lines(total[order(total)], (housing + food)[order(total)], pch = 20, col = palette()[2]))
with(household, lines(total[order(total)], (housing + food + goods)[order(total)], pch = 20, col = palette()[3]))
with(household, lines(total[order(total)], (housing + food + goods + service)[order(total)], pch = 20, col = palette()[4]))

结果:

在此处输入图像描述

于 2014-11-10T14:02:20.720 回答