我正在使用图书馆steuth2的ex0622数据

library(Sleuth2)

library(lattice)

attach(ex0622)

#Using the 'rep()' function to create a vector for the sexual preference variable ('Hetero' or 'Homo')
sex.pref=as.factor(c(rep("Hetero", 16), rep("Homo", 19), rep("Hetero", 6)))


#Using the 'rep()' function to create a vector for the Type of Death variable ('AIDS' or 'Non-AIDS')

death.type=c(rep("Aids",6), rep("Non-Aids",10), rep("Aids", 19), "Aids", rep("Non-Aids", 5))

#creating a vector of gender variable
gender=(c(rep("Male", 35), rep("Female", 6)))

length(death.type)

ex0622_alt=as.data.frame(cbind(ex0622, gender, sex.pref, death.type))
ex0622_alt
.

我运行前述代码以向数据集添加一些因素。然后我想用晶格包显示某些变量的组合

histogram(~Volume[sex.pref=="Hetero"]|gender, data=ex0622_alt, main="Heterosexuals")
dotplot(Volume[sex.pref=="Hetero"]~gender,  col=1)
.

这些尝试在不应该的时候产生因素和性别的因素的空虚组合。我不知道发生了什么。

将欣赏任何帮助!

谢谢!

有帮助吗?

解决方案

Your problem is in the histogram call: Within the ex0622_alt data-frame, you're subsetting the Volume variable by sex.pref == "Hetero", but you're not subsetting the gender variable at all, so the Volume subvector and the gender variable don't have the same length, so results will be strange. It works if you do:

histogram(~Volume[sex.pref=="Hetero"] | 
           gender[sex.pref=='Hetero'], data=ex0622_alt, main="Heterosexuals")

Or you could just use the subset arg, which would be more natural:

histogram(~Volume | gender, 
          data = ex0622_alt, subset = sex.pref == 'Hetero', main="Heterosexuals")

Same comment (and fix) applies to the dotplot command.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top