0

我想在 y 轴上绘制连续 BMI,在 x 轴上绘制家庭收入的分类变量,我希望该图可以绘制每个类别的平均 BMI。但是,我不确定如何找到家庭收入每个因素的平均 BMI。

Dataset nh  (5994 total IDs with Observations) (Parts of the 2009-2010 NHANES Dataset)
> dput(head(nh))
structure(list(SeqN = c(51624L, 51628L, 51629L, 51630L, 51633L, 51635L), 
Gender = c(1L, 2L, 1L, 2L, 1L, 1L), Age = c(34L, 60L, 26L, 49L, 80L, 80L), 
Ethnicity = c(3L, 4L, 1L, 3L, 3L, 3L), FamSize = c(4L, 2L, 5L, 3L, 2L, 1L),
RatioIncomePoverty = c(1.36, 0.69, 1.01, 1.91, 1.27, 1.69), 
MECWgt2 = c(81528.77201, 21000.33872, 22633.58187, 74112.48684, 12381.11532, 22502.50666),
BMI = c(32.22, 42.39, 32.61, 30.57, 26.04, 27.62), 
LengthUS = c(NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,NA_integer_),
Education = c(3L, 3L, 2L, 4L, 4L, 2L), LocationBorn = c(1L, 1L, 1L, 1L, 1L, 1L), 
FamIncome = c(6L, 3L, 6L, 7L, 4L, 4L)), .Names = c("SeqN", 
"Gender", "Age", "Ethnicity", "FamSize", "RatioIncomePoverty", 
"MECWgt2", "BMI", "LengthUS", "Education", "LocationBorn", "FamIncome"), 
row.names = c(NA, 6L), class = "data.frame")

faminc <- as.character(nhanes$FamIncome)
faminc

任何有关如何对数据建模以实现此目标的建议将不胜感激。

4

2 回答 2

2

这可能有效:

    library(plyr)
    nhh<-ddply(nh,.(famIncome), summarise, mean.bmi=mean(bmi)) # find mean bmi
    with(nhh, plot(famIncome,mean.bmi)) # simple plot
于 2013-04-20T19:18:17.607 回答
2

这是使用的基本解决方案aggregate

a <- aggregate(BMI ~ FamIncome, data=nh, FUN=mean)
barplot(a$BMI, names.arg=a$FamIncome)

在此处输入图像描述

于 2013-04-21T12:53:43.193 回答