07. T-test analysis
Module items¶
R Script file code¶
-
[[Copy the code]] below ➜ Paste into [[RStudio console]] ➜ Hit Enter
-
source(url("https://raw.githubusercontent.com/ttezcann/ssric-reg/refs/heads/main/docs/assets/attachments/data/0-packages-data.R")); (function(f="07-ttest.R"){if(!file.exists(f)){download.file("https://raw.githubusercontent.com/ttezcann/ssric-reg/refs/heads/main/docs/assets/attachments/modules/07.-t-test-analysis/07-ttest.R",f,mode="wb");file.edit(f)}else{download.file("https://raw.githubusercontent.com/ttezcann/ssric-reg/refs/heads/main/docs/assets/attachments/modules/07.-t-test-analysis/07-ttest.R",gsub(".R","-original.R",f),mode="wb");file.edit(gsub(".R","-original.R",f))}})()- When this R script file opens in a new tab, [[Save R script file|save your previous R script file(s)]], and
- Close the previous tabs (R Script files), which you can find later in the [[Files tab]].
- When this R script file opens in a new tab, [[Save R script file|save your previous R script file(s)]], and
-
Lab assignment¶
Sample lab assignment¶
Suggested reading¶
Urdan, Timothy C. 2010. “T Tests.” Pp. 93–103 in Statistics in plain English. New York, NY: Routledge.
Learning outcomes¶
- Determine when a t-test is appropriate based on variable types (binary factor, continuous outcome)
- Learn how to generate and interpret a t-test analysis
- Learn how to use group means, the mean difference, and the p-value in the output table
T-test analysis basics¶
-
Group I and Group II
-
Is there a significant income difference between Group I and Group II?
-
Group I
(Income mean)Group II
(Income mean)$32,000 $32,000 -
Show the answer
- We can’t know the answer without seeing the distribution of the variable.
- The mean of the two groups look same, however, there are more deviations in Group II.
-
T-test will show whether there is a significant difference. Relying solely on mean scores can be misleading!
-
id Group I Group II 1 $32,000 $23,150 2 $31,000 120,300 3 $29,500 $12,100 4 $30,100 $32,000 5 $32,500 $25,000 6 $31,150 $20,000 7 $30,230 $11,000 8 $32,400 $12,200 9 $33,000 $11,100 10 $38,120 $53,150
-
-
-
-
-
The [[t-test]] helps us determine whether there is a statistically significant difference between the means (averages) of two groups ([[statistical significance]]).
- We begin with the [[null hypothesis]] that the two groups have the same mean in the population.
- We use a t-test to compare two groups on a continuous outcome variable.
- We'll check the [[p-value]] to see whether we have evidence against the null hypothesis.
- [[Is my p-value less than 0.05?]]
- T-test can answer following questions:
- Whether the average number of close friends reported by married individuals versus single individuals significantly differs.
- Whether the average hours of daily internet usage between younger (18–34) and older (65+) age cohorts significantly differs.
- Whether the average income between male and female full-time workers significantly differs.
- We'll check the [[p-value]] to see whether we have evidence against the null hypothesis.
-
For example, we would use a t-test if we wished to compare the reading achievement scores of juniors and seniors.
-
id Class standing
(1 = juniors; 2 = seniors)Reading achievement score
(0 to 100)1 2 88 2 2 79 3 1 54 4 1 22 5 1 91 ... ... ... 300 2 78 -
Reading achievement mean score of juniors: 61.20
- Reading achievement mean score of seniors: 83.40
- The difference is 22.20 points.
-
The t-test analysis below shows if this score difference is statistically significant.
- See the "Difference" column: 22.20. On average, the reading achievement score of Seniors' is 22.20 points higher than the Juniors'.
- Now check the p-value column. We have evidence that the population means differ because the p-value is less than 0.05.
- See the "Difference" column: 22.20. On average, the reading achievement score of Seniors' is 22.20 points higher than the Juniors'.
-
Outcome variable Class = Junior Class = Senior Difference 95% CI t(298) p Reading achievement score 61.20 83.40 -22.20 (-26.80, -17.60) -9.51 <0.001***
-
T-test specifics¶
- In [[t-test]],
- [[Factor variable]] should be:
- [[Binary]];
- [[Outcome variable]] should be:
- [[Continuous]].
- In the example above, class standing is the factor variable with two values: 1: Junior; 2: Senior, thus binary.
- Reading achievement score is the outcome variable ranged from 0 to 100, thus continuous.
-
The t-test table shows a statistically significant difference in mean reading achievement scores between juniors and seniors (p < 0.05).
-
flowchart LR subgraph F["Factor variable (Binary)"] A[Class standing<br/>1=Juniors; 2=Seniors] end subgraph O["Outcome variable (Continuous)"] B[Reading achievement score<br/>Range: 0-100] end A --->|Do the group <br/>means differ?| B- The opposite would not make sense: a high reading achievement score would not make a junior become a senior the next day.
- That's why class standing is the factor variable (the groups we compare), and reading achievement score is the outcome variable (what we measure and compare across the groups).
- The opposite would not make sense: a high reading achievement score would not make a junior become a senior the next day.
-
-
- Reading achievement score is the outcome variable ranged from 0 to 100, thus continuous.
- [[Factor variable]] should be:
GSS Example 1: Significant p-value¶
The relationship between (sex and conrinc)¶
-
We want to know whether mean personal income differs in a statistically significant way between male and female respondents.
-
flowchart LR subgraph F["Factor variable (Binary)"] A[Sex<br/>1=Males; 2=Females] end subgraph O["Outcome variable (Continuous)"] B[Personal income] end A --->|Do the group <br/>means differ?| B
-
Find the variables in Variables in GSS page¶
- We want to make sure that the factor variable,
sex, is binary, and the outcome variable,conrincis continuous variable. -
[[Search]] the variable names,
sexandconrinc, in Variables in GSS page.-
Variable name Variable label Variable type Question wording and response categories sexRespondents' sex Binary What's your sex?
(1: Male; 2: Female)conrincRespondents' personal income Continuous What is your income in dollars?
(Min: $281.5; Max, $123,761.9)
-
[[T-test]] #code¶
-
[[Model code]]
-
[[Working code]]
-
- Line 1: We will put
conrinchere ➜outcome_hereandsexhere ➜factor_here.- [[Outcome variable]] first, [[factor variable]] second.
- [[Find this working code in the R script file]].
- [[Highlighting and running]] this code will generate the output below (which will appear in the [[viewer tab]] of RStudio).
- [[Find this working code in the R script file]].
- [[Outcome variable]] first, [[factor variable]] second.
- Line 1: We will put
-
[[T-test]] #output¶
-
Outcome variable sex = Male sex = Female Difference 95% CI t(2222.45) p Respondents' personal income 43037.83 31301.51 11736.32 (9148.26, 14324.39) 8.89 <0.001***
[[T-test]] #interpretation significant (p < 0.05)¶
-
Significant (p < 0.05) t-test interpretation sample
The mean personal income for males is $43,037, while the mean personal income for females is $31,301.
There is a statistically significant difference in mean personal income between males and females since the p-value is less than 0.05.
-
Significant (p < 0.05) t-test interpretation template
The mean [[variable label]] (of [[outcome variable]]) for [[value label]] 1 (of [[factor variable]]) is mean 1 year/dollar/point/score, while the mean variable label (of outcome variable) for value label 2 (of factor variable) is mean 2 year/dollar/point/score.
There is a statistically significant difference in mean variable label (of outcome variable) between value label 1 and value label 2 (of factor variable) since the p-value is less than 0.05.
-
Interpretation explanation
- The [[p-value]] tells us whether the difference between the two groups is statistically significant at the chosen significance level of 0.05.
- When the p-value is less than 0.05, the test provides evidence that the population means differ.
- When the p-value is greater than 0.05, the test provides insufficient evidence that the population means differ.
- First sentence: We report the mean of the outcome variable for each value of the factor variable.
- For example: "The mean personal income for males is $43,037, while the mean personal income for females is $31,301."
- Second sentence: We explain what the significant t-test result means.
- Since this is a significant result, we say there is a statistically significant difference in the mean of the outcome variable between the two groups.
- The [[p-value]] tells us whether the difference between the two groups is statistically significant at the chosen significance level of 0.05.
GSS example 2: Nonsignificant p-value¶
The relationship between (sex and educ)¶
-
We want to know whether mean education in years differs in a statistically significant way between male and female respondents.
-
flowchart LR subgraph F["Factor variable (Binary)"] A[Sex<br/>1=Males; 2=Females] end subgraph O["Outcome variable (Continuous)"] B[Education in years<br/>Range: 0-20] end A --->|Do the group <br/>means differ?| B
-
Find the variables in Variables in GSS page¶
- We want to make sure that the factor variable,
sex, is binary, and the outcome variable,educ, is continuous variable. -
[[Search]] the variable names,
sexandeduc, in Variables in GSS page.-
Variable name Variable label Variable type Question wording and response categories sexRespondents' sex Binary What's your sex?
(1: Male; 2: Female)educRespondents' education in years Continuous What is the highest year of school you completed?
(Min: 0, Max: 20)
-
[[T-test]] #code¶
-
[[Model code]]
-
[[Working code]]
-
- Line 1: We will put
educhere ➜outcome_hereandsexhere ➜factor_here.- [[Outcome variable]] first, [[factor variable]] second.
- [[Find this working code in the R script file]].
- [[Highlighting and running]] this code will generate the output below (which will appear in the [[viewer tab]] of RStudio).
- [[Find this working code in the R script file]].
- [[Outcome variable]] first, [[factor variable]] second.
- Line 1: We will put
-
[[T-test]] #output¶
-
Outcome variable sex = Male sex = Female Difference 95% CI t(2222.45) p Respondents' education in years 14.29 14.22 0.06 (-0.12, -0.24) 8.89 0.67
[[T-test]] #interpretation nonsignificant (p ≥ 0.05)¶
-
Nonsignificant (p ≥ 0.05) t-test interpretation sample
The mean education in years for males is 14.29 years, while the mean education in years for females is 14.22 years.
There is no statistically significant difference in mean education in years between males and females since the p-value is 0.05 or greater.
-
Nonsignificant (p ≥ 0.05) t-test interpretation template
The mean [[variable label]] (of [[outcome variable]]) for [[value label]] 1 (of [[factor variable]]) is mean 1 year/dollar/point/score, while the mean variable label (of outcome variable) for value label 2 (of factor variable) is mean 2 year/dollar/point/score.
There is no statistically significant difference in mean variable label (of outcome variable) between value label 1 and value label 2 (of factor variable) since the p-value is 0.05 or greater.
-
Interpretation explanation
- The [[p-value]] tells us whether the difference between the two groups is statistically significant at the chosen significance level of 0.05.
- When the p-value is less than 0.05, the test provides evidence that the population means differ.
- When the p-value is 0.05 or greater, the test provides insufficient evidence that the population means differ.
- First sentence: We report the mean of the outcome variable for each value of the factor variable.
- For example: "The mean education in years for males is 14.29 years, while the mean education in years for females is 14.22 years."
- Second sentence: We explain what the nonsignificant t-test result means.
- Since this is a nonsignificant result, we say there is no statistically significant difference between the two group means.
- This does not mean that the population means are necessarily equal.
- Since this is a nonsignificant result, we say there is no statistically significant difference between the two group means.
- The [[p-value]] tells us whether the difference between the two groups is statistically significant at the chosen significance level of 0.05.
GSS example 3: Significant p-value¶
The relationship between (maritalbinary and tvhours)¶
-
We want to know whether mean television screen time differs in a statistically significant way between single and nonsingle respondents.
-
flowchart LR subgraph F["Factor variable (Binary)"] A[Marital status<br/>1=Single; 2=Non-single] end subgraph O["Outcome variable (Continuous)"] B[TV hours<br/>Range: 0-24] end A --->|Do the group <br/>means differ?| B
-
Find the variables in Variables in GSS page¶
- We want to make sure that the factor variable,
marital, is binary, and the outcome variable,tvhours, is continuous variable. -
[[Search]] the variable names,
maritalandtvhours, in Variables in GSS page.-
Variable name Variable label Variable type Question wording and response categories maritalRespondents' marital status Nominal Are you currently — married, widowed, divorced, separated, or have you never been married?
(1: Married; 2: Widowed; 3: Divorced; 4: Separated; 5: Never married)tvhoursTelevision screen time in hours Continuous On the average day, how many hours do you personally watch television?
-
-
The original
maritalvariable is not binary, but nominal.- That's why we first need to recode
maritaland make it a binary variable.
- That's why we first need to recode
[[Merging values]] #code¶
-
[[Model code]]
-
[[Working code]]
-
- Line 1: We put the new variable name for the new recoded variable here,
maritalbinary.- We’ll type this name. No space, no special characters. Add “groups”, “reversed”, or “recoded” at the end of the original variable name or type anything that you will remember what this variable is.
- Line 2: We put the original variable we want to recode here,
marital. The new variable will be created based on the original variable's values. - Lines 3-4 We merge values in these lines. "[...]" are the new labels for the new values. These will appear on our outputs:
- [Single] and [Nonsingle]
- Line 6: We write this new variable's variable label here "
Recoded respondents' marital status".- [[Find this working code in the R script file]].
- [[Highlighting and running]] this code will create a new variable,
maritalbinary, and will be added to GSS dataset.
- [[Highlighting and running]] this code will create a new variable,
- [[Find this working code in the R script file]].
- Line 1: We put the new variable name for the new recoded variable here,
-
[[T-test]] #code¶
-
[[Model code]]
-
[[Working code]]
-
- Line 1: We will put
tvhourshere ➜outcome_hereandmaritalbinaryhere ➜factor_here.- [[Outcome variable]] first, [[factor variable]] second.
- Find the working code in this module's R script file.
- [[Highlighting and running]] this code will create the output below.
- Line 1: We will put
-
[[T-test]] #output¶
-
Outcome variable maritalbinary = Single maritalbinary = Nonsingle Difference 95% CI t(2222.45) p Television screen time in hours 3.60 3.25 0.35 (0.05, 0.64) 2.30 0.022*
[[T-test]] #interpretation significant (p < 0.05)¶
-
Significant (p < 0.05) t-test interpretation sample
The mean television screen time in hours for single respondents is 3.60 hours, while the mean television screen time in hours for nonsingle respondents is 3.25 hours.
There is a statistically significant difference in mean television screen time in hours between single and nonsingle respondents since the p-value is less than 0.05.
-
Significant (p < 0.05) t-test interpretation template
The mean [[variable label]] (of [[outcome variable]]) for [[value label]] 1 (of [[factor variable]]) is mean 1 year/dollar/point/score, while the mean variable label (of outcome variable) for value label 2 (of factor variable) is mean 2 year/dollar/point/score.
There is a statistically significant difference in mean variable label (of outcome variable) between value label 1 and value label 2 (of factor variable) since the p-value is less than 0.05.
-
Interpretation explanation
- The [[p-value]] tells us whether the difference between the two groups is statistically significant at the chosen significance level of 0.05.
- When the p-value is less than 0.05, the test provides evidence that the population means differ.
- When the p-value is 0.05 or greater, the test provides insufficient evidence that the population means differ.
- First sentence: We report the mean of the outcome variable for each value of the factor variable.
- For example: "The mean television screen time in hours for single respondents is 3.60 hours, while the mean television screen time in hours for nonsingle respondents is 3.25 hours."
- Second sentence: We explain what the significant t-test result means.
- Since this is a significant result, we say there is a statistically significant difference in the mean of the outcome variable between the two groups.
- The [[p-value]] tells us whether the difference between the two groups is statistically significant at the chosen significance level of 0.05.