# SAS and T-Tests - Degrees of Freedom

Why is it that in a normal t-test we have n-2 degrees of freedom while in the SAS output, and consequently the confidence interval constructed from this output, we end up using our 'error' degrees of freedom. As an example say we have 1 DF for our linear model and 14 DF 'error'. Then there are 15 DF in total. However, we take t with 14 degrees of freedom = 15 - 1 rather than 15 - 2. This is n-1 to my knowledge, not n-2. Moreover, in a regular t-test we are taking n-2 since we have two degrees removed for b0 and b1 as parameters. Why is the linear model only given 1 degree of freedom in SAS?

## 2 Answers

First hint: show explicit examples of output (on the same data) for all your statements

Why is it that in a normal t-test we have n-2 degrees of freedom while in the SAS output, and consequently the confidence interval constructed from this output, we end up using our 'error' degrees of freedom.

The $n-2$ in the two-sample $t$-test is the degrees of freedom for error. The sample has $n$ d.f. and you lose one for every estimated mean parameter (variance parameters aren't usually counted in this for linear models); you have two samples, and estimate two means (or the overall mean and the difference in means, or the first mean and the difference in means), losing 2 d.f.

As an example say we have 1 DF for our linear model and 14 DF 'error'

The intercept costs you a d.f. (which is normally why the d.f. count starts from n-1) and you lose one for each predictor after that.

Then there are 15 DF in total.

Well, the "total" d.f. (the error d.f. that goes with the intercept-only model) might be 15 if there are 16 observations (and 16 d.f.) and you subtract one for the intercept.

However, we take t with 14 degrees of freedom = 15 - 1 rather than 15 - 2.

14 = 16 - 2

for a two sample $t$.

Why is the linear model only given 1 degree of freedom in SAS?

because it subtracted 1 for the intercept right at the start, which is why the total d.f. is only 15 and not 16.

• Sorry I will add output in the future. So SAS essentially begins assuming that we are at n-1? That's very helpful, I would not have seen that. – user26091 May 28 '13 at 14:17
• It's not always necessary to add output (it's sometimes useful, sometimes not) - just in this case, what you were saying didn't sound right. SAS doesn't assume the df for the total is $n-1$ - that's precisely what it should do, and it's what stats packages pretty much all do. The regression model is compared with a null (intercept-only model or 'mean model') which has 1 df. – Glen_b -Reinstate Monica May 28 '13 at 15:06
• Thanks, it sounds like we're on the same page. There always has to be some null case, where we just have an intercept (would it be fair to call this a kind of scalar model vs. the linear model?) – user26091 May 28 '13 at 17:33
• I can't say I've ever seen it called a scalar model. I get why you'd want to call it that, though to me that would be perhaps also suggestive of a line through the origin. I'd be more likely to call it a 'constant model', but terms tend to differ in different application areas. – Glen_b -Reinstate Monica May 28 '13 at 22:52

If you have a one sample t-test, df = n-1. If you have a two sample t-test, df = n-2 (assuming equal variance)

In R:

set.seed(20283212)
x1 <- rnorm(100, 10, 5)
x2 <- rnorm(100, 10, 5)
t.test(x1, x2, var.equal = TRUE)
t.test(x1)

in SAS

one sample (n = 5, df = 4)

data junk;
input var1 var2 @@;
datalines;
1 2 4 -1 2 /*NOTE N = 5*/
run;

title 'Ons sample';
proc ttest data = junk;
var  var1;
run;

Two samples (n = 14, df = 12):

data junk;
input class \$ var1 @@;
datalines;
A 7  A 6  A 10  A 7  A 8  A 7  A 7
B 8  B 8  B 6  B 8  B 7  B 8  B 8
; /*note n = 14*/

title 'Two samples';
proc ttest data = junk;
class class;
var var1;
run;
• Oh! So in short does SAS run a one sample t-test instead of a two sample test? – user26091 May 28 '13 at 14:13
• No. SAS does what you tell it to; just like R does. Look at the code I gave. It includes one sample and two sample tests for both R and SAS. – Peter Flom May 28 '13 at 19:32