Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

r - Basic - T-Test -> Grouping Factor Must have Exactly 2 Levels

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...