T-test with nested data I want to test gender differences in an outcome variable called the willingness to pay. My data consists of records with following three fields: person_id, person_gender, and willingness_to_pay (in dollars). Willingness to pay was obtained from some users multiple times. So I have multiple records for some users.
I understand that following would be incorrect:
t.test(willingness_to_pay ~ person_gender, paired = FALSE, data = myData)

How do I account for nesting with person_id? Please help.
 A: It is hard to tell what you actually want to do. In order to test the differences in willingness to pay and formulate the model you have to construct the model in a way that it meets your research question. The central question is: Why do you have repeated measures of willingness to pay? You could for example either compare changes between the groups or within the groups.
If you want to compare between the groups lmer(willingness_to_pay ~ person_gender + (1|person_id), data=myData) is fine and the Random Effect can account for the repeated measures from the same individuals. However, you should consider that it may be necessary to include a time variable that indicates the "measurement moments" because your willingness to pay may be dependent on the moment respondents stated it.
If you interested in within-group-comparison you can also use a paired t-testt.test(x=wtp.moment.1, y=wtp.moment2, alternative = c("two.sided"),var.equal = T, conf.level=0.95) to see if there were significant changes in willingness to pay within the groups (gender) at different moments of measurement. Of course you can also construct a mixed-effect model for this by including a variable that accounts for the "moment of measurement" and its interaction with participants' gender lmer(willingness_to_pay ~ person_gender * moment + (1|person_id), data=myData).
