Michael
Michael

Reputation: 1575

R question. Unexplainably empty factor combinations when plotting response

I am using the ex0622 data from library Sleuth2

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

I run the preceeding code to add some factors to the data set. Then I want to display certain combinations of variables with the lattice package

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

Both those attempts produce empty combinations of the factors gender and sex.pref when they should not. I have no idea what is going on.

Any help would be appreciated!

Thanks!

Upvotes: 2

Views: 229

Answers (1)

Prasad Chalasani
Prasad Chalasani

Reputation: 20282

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.

Upvotes: 3

Related Questions