Basic - T-Test -> Grouping Factor Must have Exactl

2019-02-07 10:18发布

问题:

I am relatively new to R. For my assignment I have to start by conducting a T-Test by looking at the effect of a politician's (Conservative or Labour) wealth on their real gross wealth and real net wealth. I have to attempt to estimate the effect of serving in office wealth using a simple t-test.

The dataset is called takehome.dta

Labour and Tory are binary where 1 indicates that they serve for that party and 0 otherwise.

The variables for wealth are lnrealgross and lnrealnet.

I have imported and attached the dataset, but when I attempt to conduct a simple t-test. I get the following message "grouping factor must have exactly 2 levels." Not quite sure where I appear to be going wrong. Any assistance would be appreciated!

回答1:

are you doing this:

t.test(y~x)

when you mean to do this

t.test(y,x)

In general use the ~ then you have data like

y <- 1:10
x <- rep(letters[1:2], each = 5)

and the , when you have data like

y <- 1:5
x <- 6:10

I assume you're doing something like:

y <- 1:10
x <- rep(1,10)
t.test(y~x) #instead of t.test(y,x)

because the error suggests you have no variation in the grouping factor x



回答2:

The differences between ~ and , is the type of statistical test you are running. ~ gives you the mean differences. This is for dependent samples (e.g. before and after). , gives you the difference in means. This is for independent samples (e.g. treatment and control). These two tests are not interchangeable.