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/r-scripts/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/r-scripts/07-ttest.R",f,mode="wb");file.edit(f)}else{download.file("https://raw.githubusercontent.com/ttezcann/ssric-reg/refs/heads/main/docs/assets/r-scripts/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.
-
RStudio 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]] is used to determine if:
- There is a significant difference ([[statistical significance]]) between the means (average scores) of two groups.
- We conduct t-test, when we want to compare two means (the scores must be continuous).
- We'll check the [[p-value]].
- [[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]].
-
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. Based on the t-test analysis, this 22.20 points difference is statistically significant 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.000***
-
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 that students' class standing (being junior or senior) has a significant effect on their reading achievement score (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 ==>|Has an effect on <br/> Significant effect; p<0.05| B- The opposite couldn't be the case. Just because a student's reading achievement score is high, this wouldn't make them a senior next day.
- That's why, class standing is the factor, and the reading achievement score is the outcome.
- The opposite couldn't be the case. Just because a student's reading achievement score is high, this wouldn't make them a senior 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 wonder if respondents' sex have a statistically significant influence on their personal income.
-
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 ==>|May have an effect on| 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.000***
[[T-test]] #interpretation significant (p < 0.05)¶
-
Significant (p < 0.05) t-test interpretation sample
The average personal income of males is $43,037, The average personal income of females is $31,301.
Personal income differs by respondents’ sex in a statistically significant way since the p-value is less than 0.05.
-
Significant (p < 0.05) t-test interpretation template
The average [[variable label]] of [[outcome variable]] of [[value label]] 1 of [[factor variable]] is mean year/dollar/point/score, while variable label of outcome variable of value label 1 of factor variable is mean year/dollar/point/score.
Variable label of outcome variable differs by variable label of factor variable in a statistically significant way 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.
- When the p-value is less than 0.05, we conclude that the two groups have different averages on the outcome variable.
- When the p-value is greater than 0.05, we conclude that the two groups have similar averages on the outcome variable.
- First sentence: We report the average of the outcome variable for each value of the factor variable.
- For example: "The average personal income of males is $43,037, while the average personal income of females is $31,301."
- Second sentence: We explain what the significant t-test result means.
- Since this is a significant result, we say the outcome variable differs by the factor variable in a statistically significant way.
- The [[p-value]] tells us whether the difference between the two groups is statistically significant.
GSS example 2: Nonsignificant p-value¶
The relationship between (sex and educ)¶
-
We wonder if respondents' sex have a statistically significant influence on their education.
-
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 ==>|May have an effect on| 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 average education in years of males is 14.29 years, the average education in years of females is 14.22 years.
Education in years does not differ by respondents’ sex in a statistically significant way since the p-value is higher than 0.05.
-
Nonsignificant (p > 0.05) t-test interpretation template
The average [[variable label]] of [[outcome variable]] of [[value label]] 1 of [[factor variable]] is mean year/dollar/point/score, while variable label of outcome variable of value label 1 of factor variable is mean year/dollar/point/score.
Variable label of outcome variable does not differ by variable label of factor variable in a statistically significant way since the p-value is higher than 0.05.
-
Interpretation explanation
- The [[p-value]] tells us whether the difference between the two groups is statistically significant.
- When the p-value is less than 0.05, we conclude that the two groups have different averages on the outcome variable.
- When the p-value is greater than 0.05, we conclude that the two groups have similar averages on the outcome variable.
- First sentence: We report the average of the outcome variable for each value of the factor variable.
- For example: "The average education in years of males is 14.29 years, the average education in years of females is 14.22 years."
- Second sentence: We explain what the nonsignificant t-test result means.
- Since this is a nonsignificant result, we say the outcome variable does not differ by the factor variable in a statistically significant way.
- The [[p-value]] tells us whether the difference between the two groups is statistically significant.
GSS example 3: Significant p-value¶
The relationship between (maritalbinary and tvhours)¶
-
We wonder if respondents' marital status have a statistically significant influence on how many hours they watch television.
-
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 ==>|May have an effect on| 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,
maritalgroups, 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 average television screen time in hours of single respondents is 3.60 hours, while the average television screen time in hours of nonsingle respondents is 3.25 hours.
Television screen time in hours differs by respondents’ marital status in a statistically significant way since the p-value is less than 0.05.
-
Significant (p < 0.05) t-test interpretation template
The average [[variable label]] of [[outcome variable]] of [[value label]] 1 of [[factor variable]] is mean year/dollar/point/score, while variable label of outcome variable of value label 1 of factor variable is mean year/dollar/point/score.
Variable label of outcome variable differs by variable label of factor variable in a statistically significant way 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.
- When the p-value is less than 0.05, we conclude that the two groups have different averages on the outcome variable.
- When the p-value is greater than 0.05, we conclude that the two groups have similar averages on the outcome variable.
- First sentence: We report the average of the outcome variable for each value of the factor variable.
- For example: "The average television screen time in hours of single respondents is 3.60 hours, while the average television screen time in hours of 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 the outcome variable differs by the factor variable in a statistically significant way.
- The [[p-value]] tells us whether the difference between the two groups is statistically significant.