I'm learning R and having trouble with one of the questions I've been asked to answer on my course.
I need to run a linear model where I need to control for variables A and B, X is the dependent variable (e.g. Ppt's height), and the main predictor is comprised of related variables / is a variable that needs to be filtered.
For example - Controlling for A & B, Can X be predicted by having a sibling (S) that lives in the same city ( C = SC), where I have the variables: S - has a living sibling (values: True/False), C - Ppt's city (Values: Springfield, Ballamory, Hogsmead, Anvil, New Kanton) SC - Sibling's city (Values: Springfield, Ballamory, Hogsmead, Anvil, New Kanton)
I've tried this:
Model <- lm( X ~ S
+ A
+ B,
data = Dataset %>% filter(C == SC))
but I think this means that the whole dataset is being filtered, so I'm not getting results which tell me:
'How far can X be predicted by having a sibling (S) that lives in the same city'
but rather tells me :
'Of all those who have a sibling living in the same city, how far can X be predicted by having a sibling?'
So I tried this as an alternative:
Model <- lm( X ~ S %>% filter(C == SC)
+ A
+ B,
data = Dataset)
but got:
Error in UseMethod("filter") : no applicable method for 'filter' applied to an object of class "logical"
I have also tried:
`Model <- lm( X ~ S
+ A
+ B + SC
data = Dataset)`
but that doesn't feel right/ makes SC a control variable rather than a predictor.
Any guidance on what I'm doing wrong (or if I'm overthinking) would be very much appreciated!