SlideShare ist ein Scribd-Unternehmen logo
1 von 39
Downloaden Sie, um offline zu lesen
Week 2.2: Data Processing in R
! Filtering
! Basic Filtering
! Advanced Filtering
! Mutate
! Basic Variable Creation and Editing
! if_else()
! Variable Types
! Other Functions & Packages
Filtering Data
! We didn’t see a big difference between
conditions
! But, some RTs look
like outliers—we
may want to
exclude them
Filtering Data
! Often, we want to examine or use just part of
a dataframe
! filter() lets us retain only certain
observations
! experiment %>%
filter(RT < 2000) %>%
group_by(Condition) %>%
summarize(M=mean(RT))
Inclusion criterion: We
want to keep RTs less
than 2000 ms
As we saw last time, this gets the mean RT
for each condition
Filtering Data
! Often, we want to examine or use just part of
a dataframe
! filter() lets us retain only certain
observations
! experiment %>%
filter(RT < 2000) %>%
group_by(Condition) %>%
summarize(M=mean(RT))
Inclusion criterion: We
want to keep RTs less
than 2000 ms
Filtering Data
! This only temporarily filtered the data
! If we want to
Filtering Data
! This only temporarily filtered the data
! If we want to run a lot of analyses with this
filter, we may want to save the filtered data as
a new dataframe
! experiment %>%
filter(RT < 2000)
-> experiment.filtered
-> is the assignment
operator. It stores results
or data in memory.
Name of the new dataframe
(can be whatever you want)
Filtering Data
! This only temporarily filtered the data
! If we want to run a lot of analyses with this
filter, we may want to save the filtered data as
a new dataframe
Writing Data
! Note that this is just creating a new
dataframe in R
! If you want to save to a folder on your
computer, use write.csv():
! write.csv(experiment.filtered,
file='experiment_filtered.csv')
Filtering Data
! Why not just delete the bad RTs from the
spreadsheet?
Filtering Data
! Why not just delete the bad RTs from the
spreadsheet?
! Easy to make a mistake / miss some of them
! Faster to have the computer do it
! We’d lose the original data
! No documentation of how we subsetted the data
Week 2.2: Data Processing in R
! Filtering
! Basic Filtering
! Advanced Filtering
! Mutate
! Basic Variable Creation and Editing
! if_else()
! Variable Types
! Other Functions & Packages
Filtering Data: AND and OR
! What if we wanted only RTs between 200
and 2000 ms?
- experiment %>%
filter(RT >= 200 & RT <= 2000)
! | means OR:
- experiment %>%
filter(RT < 200 | RT > 2000) ->
experiment.outliers
- Logical OR (“either or both”)
Filtering Data: == and !=
! Get a match / equals:
- experiment %>%
filter(TrialsRemaining == 0)
! Words/categorical variables need quotes:
- experiment %>%
filter(Condition=='Implausible')
! != means “not equal to”:
- experiment %>%
filter(Subject != 'S23’)
- Drops Subject “S23”
Note DOUBLE
equals sign
Filtering Data: %in%
! Sometimes our inclusion criteria aren't so
mathematical
! Suppose I just want the “Ducks” and “Panther”
items
! We can check against any arbitrary list:
- experiment %>%
filter(ItemName %in%
c('Ducks', 'Panther'))
! Or, keep just things that aren't in a list:
- experiment %>%
filter(Subject %in%
c('S10', 'S23') == FALSE)
Logical Operators Review
! Summary
- > Greater than
- >= Greater than or equal to
- < Less than
- <= Less than or equal to
- & AND
- | OR
- == Equal to
- != Not equal to
- %in% Is this included in a list?
Week 2.2: Data Processing in R
! Filtering
! Basic Filtering
! Advanced Filtering
! Mutate
! Basic Variable Creation and Editing
! if_else()
! Variable Types
! Other Functions & Packages
Mutate
! The last tidyverse function we’ll look at is
mutate()
! Add new variables
! Transform variables
! Recode or rescore variables
Mutate
! We can use mutate() to create new
columns in our dataframe:
- experiment %>%
mutate(ExperimentNumber = 1)
-> experiment
We are creating a column
named ExperimentNumber,
and assigning the value 1 for
every observation
Then, we need to store the
updated data back into our
experiment dataframe
Mutate
! We can use mutate() to create new
columns in our dataframe:
- experiment %>%
mutate(ExperimentNumber = 1)
-> experiment
Mutate
! A more interesting example is where the
assigned value is based on a formula
! experiment %>%
mutate(RTinSeconds = RT/1000)
-> experiment
! For each row, finds the RT in seconds for that
specific trial and saves that into RTinSeconds
- Similar to an Excel formula
• If we wanted to alter the original RT column,
we could instead do:
mutate(RT = RT/1000)
Mutate
! We can even use other functions in
calculating new columns
! experiment %>%
mutate(logRT = log(RT))
-> experiment
! Applies the logarithmic transformation to each
RT and saves that as logRT
Week 2.2: Data Processing in R
! Filtering
! Basic Filtering
! Advanced Filtering
! Mutate
! Basic Variable Creation and Editing
! if_else()
! Variable Types
! Other Functions & Packages
if_else()
IF YOU WANT
DESSERT, EAT
YOUR PEAS
… OR ELSE!
if_else()
! if_else(): A function that uses a test to
decide which of two values to assign:
! experiment %>% mutate(
Half=
if_else(
TrialsRemaining >= 15,
1,
2)
) -> experiment
Function name
If 15 or more
trials remain…
“Half” is 1
If NOT, “Half” is 2
A new column
called “Half”--what
value are we going
to assign ?
Which do you like better?
- experiment %>% mutate(
Half=if_else(TrialsRemaining >= 15,
1, 2)) -> experiment
! vs:
- TrialsPerSubject <- 30
- experiment %>% mutate(
Half=if_else(TrialsRemaining >=
TrialsPerSubject / 2, 1, 2)) ->
experiment
Which do you like better?
- experiment %>% mutate(
Half=if_else(TrialsRemaining >= 15,
1, 2)) -> experiment
! vs:
- TrialsPerSubject <- 30
- experiment %>% mutate(
Half=if_else(TrialsRemaining >=
TrialsPerSubject / 2, 1, 2)) ->
experiment
- Explains where the 15 comes from—helpful if we come back
to this script later
- We can also refer to CriticalTrialsPerSubject
variable later in the script & this ensure it’s consistent
- Easy to update if we change the number of trials
if_else()
! Instead of comparing to specific numbers (like
15), we can use other columns or a formula:
! experiment %>% mutate(
RT.Fenced =
if_else(RT < 200, 200, RT))
-> experiment
! What is this doing?
if_else()
! Instead of comparing to specific numbers (like
15), we can use other columns or a formula:
! experiment %>% mutate(
RT.Fenced =
if_else(RT < 200, 200, RT))
-> experiment
! Creates an RT.Fenced column where:
! Where RTs are less than 200 ms, replace them
with 200
! Otherwise, use the original RT value
! i.e., replace all RTs less than 200 ms with the
value 200
if_else()
! Instead of comparing to specific numbers (like
15), we can use other columns or a formula:
! experiment %>% mutate(
RT.Fenced =
if_else(RT < 200, 200, RT))
-> experiment
! For even more complex rescoring, use
case_when()
Week 2.2: Data Processing in R
! Filtering
! Basic Filtering
! Advanced Filtering
! Mutate
! Basic Variable Creation and Editing
! if_else()
! Variable Types
! Other Functions & Packages
Types
! R treats continuous & categorical variables
differently:
! These are different data types:
- Numeric
- Character: Freely entered text (e.g.,
open response question)
- Factor: Variable w/ fixed set of
categories (e.g., treatment vs. placebo)
Types
! R’s current heuristic when reading in data:
- No letters, purely numbers → numeric
- Letters anywhere in the column →
character
Types: as.factor()
! For variables with a fixed set of categories, we
may want to convert to factor
! experiment %>%
mutate(Condition=as.factor(Condition))
-> experiment
Types: as.numeric()
! Age was read as a character variable because
some people “Declined to report”
! But, we may want to treat it as numeric despite
this
Types: as.numeric()
! Age was read as a character variable because
some people “Declined to report”
- experiment %>%
mutate(AgeNumeric=as.numeric(Age)) ->
experiment
• We now get quantitative
information on Age
• Values that couldn’t be turned
into numbers are listed as NA
• NA means missing data--we’ll
discuss that more later in the term
Week 2.2: Data Processing in R
! Filtering
! Basic Filtering
! Advanced Filtering
! Mutate
! Basic Variable Creation and Editing
! if_else()
! Variable Types
! Other Functions & Packages
Other Functions
! Some built-in analyses:
! aov() ANOVA
! lm() Linear regression
! glm() Generalized linear models (e.g., logistic)
! cor.test() Correlation
! t.test() t-test
Other Packages
! Some other relevant packages:
! lavaan Latent variable analysis and
structural equation modeling
! psych Psychometrics (scale construction, etc.)
! party Random forests
! stringr Working with character variables
! lme4: Package for linear mixed-effects models
! Get this one for next week
Getting Help
! Get help on a specific known function:
- ?t.test
- Lists all
arguments
! Try to find a
function on a
particular topic:
- ??logarithm

Weitere ähnliche Inhalte

Was ist angesagt?

Mixed Effects Models - Introduction
Mixed Effects Models - IntroductionMixed Effects Models - Introduction
Mixed Effects Models - IntroductionScott Fraundorf
 
Mixed Effects Models - Growth Curve Analysis
Mixed Effects Models - Growth Curve AnalysisMixed Effects Models - Growth Curve Analysis
Mixed Effects Models - Growth Curve AnalysisScott Fraundorf
 
Mixed Effects Models - Random Slopes
Mixed Effects Models - Random SlopesMixed Effects Models - Random Slopes
Mixed Effects Models - Random SlopesScott Fraundorf
 
Mixed Effects Models - Model Comparison
Mixed Effects Models - Model ComparisonMixed Effects Models - Model Comparison
Mixed Effects Models - Model ComparisonScott Fraundorf
 
Mixed Effects Models - Post-Hoc Comparisons
Mixed Effects Models - Post-Hoc ComparisonsMixed Effects Models - Post-Hoc Comparisons
Mixed Effects Models - Post-Hoc ComparisonsScott Fraundorf
 
Mixed Effects Models - Orthogonal Contrasts
Mixed Effects Models - Orthogonal ContrastsMixed Effects Models - Orthogonal Contrasts
Mixed Effects Models - Orthogonal ContrastsScott Fraundorf
 
Mixed Effects Models - Autocorrelation
Mixed Effects Models - AutocorrelationMixed Effects Models - Autocorrelation
Mixed Effects Models - AutocorrelationScott Fraundorf
 
Quantum Geometry: A reunion of math and physics
Quantum Geometry: A reunion of math and physicsQuantum Geometry: A reunion of math and physics
Quantum Geometry: A reunion of math and physicsRafa Spoladore
 
What is an independent samples-t test?
What is an independent samples-t test?What is an independent samples-t test?
What is an independent samples-t test?Ken Plummer
 
What We (Don't) Know About the Beginning of the Universe
What We (Don't) Know About the Beginning of the UniverseWhat We (Don't) Know About the Beginning of the Universe
What We (Don't) Know About the Beginning of the UniverseSean Carroll
 
Reporting Phi Coefficient test in APA
Reporting Phi Coefficient test in APAReporting Phi Coefficient test in APA
Reporting Phi Coefficient test in APAKen Plummer
 
What is Regression Testing? | Edureka
What is Regression Testing? | EdurekaWhat is Regression Testing? | Edureka
What is Regression Testing? | EdurekaEdureka!
 
Repeated Measures ANOVA - Overview
Repeated Measures ANOVA - OverviewRepeated Measures ANOVA - Overview
Repeated Measures ANOVA - OverviewSr Edith Bogue
 
Potential Solutions to the Fundamental Problem of Causal Inference: An Overview
Potential Solutions to the Fundamental Problem of Causal Inference: An OverviewPotential Solutions to the Fundamental Problem of Causal Inference: An Overview
Potential Solutions to the Fundamental Problem of Causal Inference: An OverviewEconomic Research Forum
 
Application of ordinal logistic regression in the study of students’ performance
Application of ordinal logistic regression in the study of students’ performanceApplication of ordinal logistic regression in the study of students’ performance
Application of ordinal logistic regression in the study of students’ performanceAlexander Decker
 
Survival analysis
Survival analysisSurvival analysis
Survival analysisIbraahimAli3
 
Mixed between-within groups ANOVA
Mixed between-within groups ANOVAMixed between-within groups ANOVA
Mixed between-within groups ANOVAMahsa Farahanynia
 
Logistic Ordinal Regression
Logistic Ordinal RegressionLogistic Ordinal Regression
Logistic Ordinal RegressionSri Ambati
 
Wayfair's Data Science Team and Case Study: Uplift Modeling
Wayfair's Data Science Team and Case Study: Uplift ModelingWayfair's Data Science Team and Case Study: Uplift Modeling
Wayfair's Data Science Team and Case Study: Uplift ModelingPatricia Stichnoth
 

Was ist angesagt? (20)

Mixed Effects Models - Introduction
Mixed Effects Models - IntroductionMixed Effects Models - Introduction
Mixed Effects Models - Introduction
 
Mixed Effects Models - Growth Curve Analysis
Mixed Effects Models - Growth Curve AnalysisMixed Effects Models - Growth Curve Analysis
Mixed Effects Models - Growth Curve Analysis
 
Mixed Effects Models - Random Slopes
Mixed Effects Models - Random SlopesMixed Effects Models - Random Slopes
Mixed Effects Models - Random Slopes
 
Mixed Effects Models - Model Comparison
Mixed Effects Models - Model ComparisonMixed Effects Models - Model Comparison
Mixed Effects Models - Model Comparison
 
Mixed Effects Models - Post-Hoc Comparisons
Mixed Effects Models - Post-Hoc ComparisonsMixed Effects Models - Post-Hoc Comparisons
Mixed Effects Models - Post-Hoc Comparisons
 
Mixed Effects Models - Orthogonal Contrasts
Mixed Effects Models - Orthogonal ContrastsMixed Effects Models - Orthogonal Contrasts
Mixed Effects Models - Orthogonal Contrasts
 
Mixed Effects Models - Autocorrelation
Mixed Effects Models - AutocorrelationMixed Effects Models - Autocorrelation
Mixed Effects Models - Autocorrelation
 
Quantum Geometry: A reunion of math and physics
Quantum Geometry: A reunion of math and physicsQuantum Geometry: A reunion of math and physics
Quantum Geometry: A reunion of math and physics
 
What is an independent samples-t test?
What is an independent samples-t test?What is an independent samples-t test?
What is an independent samples-t test?
 
What We (Don't) Know About the Beginning of the Universe
What We (Don't) Know About the Beginning of the UniverseWhat We (Don't) Know About the Beginning of the Universe
What We (Don't) Know About the Beginning of the Universe
 
Reporting Phi Coefficient test in APA
Reporting Phi Coefficient test in APAReporting Phi Coefficient test in APA
Reporting Phi Coefficient test in APA
 
Ch08(1)
Ch08(1)Ch08(1)
Ch08(1)
 
What is Regression Testing? | Edureka
What is Regression Testing? | EdurekaWhat is Regression Testing? | Edureka
What is Regression Testing? | Edureka
 
Repeated Measures ANOVA - Overview
Repeated Measures ANOVA - OverviewRepeated Measures ANOVA - Overview
Repeated Measures ANOVA - Overview
 
Potential Solutions to the Fundamental Problem of Causal Inference: An Overview
Potential Solutions to the Fundamental Problem of Causal Inference: An OverviewPotential Solutions to the Fundamental Problem of Causal Inference: An Overview
Potential Solutions to the Fundamental Problem of Causal Inference: An Overview
 
Application of ordinal logistic regression in the study of students’ performance
Application of ordinal logistic regression in the study of students’ performanceApplication of ordinal logistic regression in the study of students’ performance
Application of ordinal logistic regression in the study of students’ performance
 
Survival analysis
Survival analysisSurvival analysis
Survival analysis
 
Mixed between-within groups ANOVA
Mixed between-within groups ANOVAMixed between-within groups ANOVA
Mixed between-within groups ANOVA
 
Logistic Ordinal Regression
Logistic Ordinal RegressionLogistic Ordinal Regression
Logistic Ordinal Regression
 
Wayfair's Data Science Team and Case Study: Uplift Modeling
Wayfair's Data Science Team and Case Study: Uplift ModelingWayfair's Data Science Team and Case Study: Uplift Modeling
Wayfair's Data Science Team and Case Study: Uplift Modeling
 

Ă„hnlich wie Mixed Effects Models - Data Processing

Week 4 Lecture 12 Significance Earlier we discussed co.docx
Week 4 Lecture 12 Significance Earlier we discussed co.docxWeek 4 Lecture 12 Significance Earlier we discussed co.docx
Week 4 Lecture 12 Significance Earlier we discussed co.docxcockekeshia
 
Programming in C Session 1
Programming in C Session 1Programming in C Session 1
Programming in C Session 1Prerna Sharma
 
Machine Learning.pdf
Machine Learning.pdfMachine Learning.pdf
Machine Learning.pdfBeyaNasr1
 
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arraysUnit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arraysDevaKumari Vijay
 
Data weave (MuleSoft)
Data weave (MuleSoft)Data weave (MuleSoft)
Data weave (MuleSoft)Nandu List5
 
Parameter Estimation User Guide
Parameter Estimation User GuideParameter Estimation User Guide
Parameter Estimation User GuideAndy Salmon
 
Introduction to golang
Introduction to golangIntroduction to golang
Introduction to golangwww.ixxo.io
 
Workshop 4
Workshop 4Workshop 4
Workshop 4eeetq
 
Java Unit Test - JUnit
Java Unit Test - JUnitJava Unit Test - JUnit
Java Unit Test - JUnitAktuÄź Urun
 
Introduction to R Short course Fall 2016
Introduction to R Short course Fall 2016Introduction to R Short course Fall 2016
Introduction to R Short course Fall 2016Spencer Fox
 
Random Forest / Bootstrap Aggregation
Random Forest / Bootstrap AggregationRandom Forest / Bootstrap Aggregation
Random Forest / Bootstrap AggregationRupak Roy
 
Chapter 02-logistic regression
Chapter 02-logistic regressionChapter 02-logistic regression
Chapter 02-logistic regressionRaman Kannan
 
Base sas interview questions
Base sas interview questionsBase sas interview questions
Base sas interview questionsSunil0108
 
Predicting US house prices using Multiple Linear Regression in R
Predicting US house prices using Multiple Linear Regression in RPredicting US house prices using Multiple Linear Regression in R
Predicting US house prices using Multiple Linear Regression in RSotiris Baratsas
 
Machine learning Mind Map
Machine learning Mind MapMachine learning Mind Map
Machine learning Mind MapAshish Patel
 
Base sas interview questions
Base sas interview questionsBase sas interview questions
Base sas interview questionsDr P Deepak
 

Ă„hnlich wie Mixed Effects Models - Data Processing (20)

Week 4 Lecture 12 Significance Earlier we discussed co.docx
Week 4 Lecture 12 Significance Earlier we discussed co.docxWeek 4 Lecture 12 Significance Earlier we discussed co.docx
Week 4 Lecture 12 Significance Earlier we discussed co.docx
 
Programming in C Session 1
Programming in C Session 1Programming in C Session 1
Programming in C Session 1
 
Machine Learning.pdf
Machine Learning.pdfMachine Learning.pdf
Machine Learning.pdf
 
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arraysUnit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
 
Data weave (MuleSoft)
Data weave (MuleSoft)Data weave (MuleSoft)
Data weave (MuleSoft)
 
Parameter Estimation User Guide
Parameter Estimation User GuideParameter Estimation User Guide
Parameter Estimation User Guide
 
Introduction to golang
Introduction to golangIntroduction to golang
Introduction to golang
 
Workshop 4
Workshop 4Workshop 4
Workshop 4
 
INTRODUCTION TO STATA.pptx
INTRODUCTION TO STATA.pptxINTRODUCTION TO STATA.pptx
INTRODUCTION TO STATA.pptx
 
Java Unit Test - JUnit
Java Unit Test - JUnitJava Unit Test - JUnit
Java Unit Test - JUnit
 
Md04 flow control
Md04 flow controlMd04 flow control
Md04 flow control
 
Introduction to R Short course Fall 2016
Introduction to R Short course Fall 2016Introduction to R Short course Fall 2016
Introduction to R Short course Fall 2016
 
Random Forest / Bootstrap Aggregation
Random Forest / Bootstrap AggregationRandom Forest / Bootstrap Aggregation
Random Forest / Bootstrap Aggregation
 
Chapter 02-logistic regression
Chapter 02-logistic regressionChapter 02-logistic regression
Chapter 02-logistic regression
 
Base sas interview questions
Base sas interview questionsBase sas interview questions
Base sas interview questions
 
Python_Module_2.pdf
Python_Module_2.pdfPython_Module_2.pdf
Python_Module_2.pdf
 
Predicting US house prices using Multiple Linear Regression in R
Predicting US house prices using Multiple Linear Regression in RPredicting US house prices using Multiple Linear Regression in R
Predicting US house prices using Multiple Linear Regression in R
 
TDD Training
TDD TrainingTDD Training
TDD Training
 
Machine learning Mind Map
Machine learning Mind MapMachine learning Mind Map
Machine learning Mind Map
 
Base sas interview questions
Base sas interview questionsBase sas interview questions
Base sas interview questions
 

Mehr von Scott Fraundorf

Mixed Effects Models - Signal Detection Theory
Mixed Effects Models - Signal Detection TheoryMixed Effects Models - Signal Detection Theory
Mixed Effects Models - Signal Detection TheoryScott Fraundorf
 
Mixed Effects Models - Power
Mixed Effects Models - PowerMixed Effects Models - Power
Mixed Effects Models - PowerScott Fraundorf
 
Mixed Effects Models - Effect Size
Mixed Effects Models - Effect SizeMixed Effects Models - Effect Size
Mixed Effects Models - Effect SizeScott Fraundorf
 
Mixed Effects Models - Missing Data
Mixed Effects Models - Missing DataMixed Effects Models - Missing Data
Mixed Effects Models - Missing DataScott Fraundorf
 
Mixed Effects Models - Logit Models
Mixed Effects Models - Logit ModelsMixed Effects Models - Logit Models
Mixed Effects Models - Logit ModelsScott Fraundorf
 
Mixed Effects Models - Level-2 Variables
Mixed Effects Models - Level-2 VariablesMixed Effects Models - Level-2 Variables
Mixed Effects Models - Level-2 VariablesScott Fraundorf
 
Scott_Fraundorf_Resume
Scott_Fraundorf_ResumeScott_Fraundorf_Resume
Scott_Fraundorf_ResumeScott Fraundorf
 

Mehr von Scott Fraundorf (7)

Mixed Effects Models - Signal Detection Theory
Mixed Effects Models - Signal Detection TheoryMixed Effects Models - Signal Detection Theory
Mixed Effects Models - Signal Detection Theory
 
Mixed Effects Models - Power
Mixed Effects Models - PowerMixed Effects Models - Power
Mixed Effects Models - Power
 
Mixed Effects Models - Effect Size
Mixed Effects Models - Effect SizeMixed Effects Models - Effect Size
Mixed Effects Models - Effect Size
 
Mixed Effects Models - Missing Data
Mixed Effects Models - Missing DataMixed Effects Models - Missing Data
Mixed Effects Models - Missing Data
 
Mixed Effects Models - Logit Models
Mixed Effects Models - Logit ModelsMixed Effects Models - Logit Models
Mixed Effects Models - Logit Models
 
Mixed Effects Models - Level-2 Variables
Mixed Effects Models - Level-2 VariablesMixed Effects Models - Level-2 Variables
Mixed Effects Models - Level-2 Variables
 
Scott_Fraundorf_Resume
Scott_Fraundorf_ResumeScott_Fraundorf_Resume
Scott_Fraundorf_Resume
 

KĂĽrzlich hochgeladen

Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 

KĂĽrzlich hochgeladen (20)

Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
CĂłdigo Creativo y Arte de Software | Unidad 1
CĂłdigo Creativo y Arte de Software | Unidad 1CĂłdigo Creativo y Arte de Software | Unidad 1
CĂłdigo Creativo y Arte de Software | Unidad 1
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 

Mixed Effects Models - Data Processing

  • 1. Week 2.2: Data Processing in R ! Filtering ! Basic Filtering ! Advanced Filtering ! Mutate ! Basic Variable Creation and Editing ! if_else() ! Variable Types ! Other Functions & Packages
  • 2. Filtering Data ! We didn’t see a big difference between conditions ! But, some RTs look like outliers—we may want to exclude them
  • 3. Filtering Data ! Often, we want to examine or use just part of a dataframe ! filter() lets us retain only certain observations ! experiment %>% filter(RT < 2000) %>% group_by(Condition) %>% summarize(M=mean(RT)) Inclusion criterion: We want to keep RTs less than 2000 ms As we saw last time, this gets the mean RT for each condition
  • 4. Filtering Data ! Often, we want to examine or use just part of a dataframe ! filter() lets us retain only certain observations ! experiment %>% filter(RT < 2000) %>% group_by(Condition) %>% summarize(M=mean(RT)) Inclusion criterion: We want to keep RTs less than 2000 ms
  • 5. Filtering Data ! This only temporarily filtered the data ! If we want to
  • 6. Filtering Data ! This only temporarily filtered the data ! If we want to run a lot of analyses with this filter, we may want to save the filtered data as a new dataframe ! experiment %>% filter(RT < 2000) -> experiment.filtered -> is the assignment operator. It stores results or data in memory. Name of the new dataframe (can be whatever you want)
  • 7. Filtering Data ! This only temporarily filtered the data ! If we want to run a lot of analyses with this filter, we may want to save the filtered data as a new dataframe
  • 8. Writing Data ! Note that this is just creating a new dataframe in R ! If you want to save to a folder on your computer, use write.csv(): ! write.csv(experiment.filtered, file='experiment_filtered.csv')
  • 9. Filtering Data ! Why not just delete the bad RTs from the spreadsheet?
  • 10. Filtering Data ! Why not just delete the bad RTs from the spreadsheet? ! Easy to make a mistake / miss some of them ! Faster to have the computer do it ! We’d lose the original data ! No documentation of how we subsetted the data
  • 11. Week 2.2: Data Processing in R ! Filtering ! Basic Filtering ! Advanced Filtering ! Mutate ! Basic Variable Creation and Editing ! if_else() ! Variable Types ! Other Functions & Packages
  • 12. Filtering Data: AND and OR ! What if we wanted only RTs between 200 and 2000 ms? - experiment %>% filter(RT >= 200 & RT <= 2000) ! | means OR: - experiment %>% filter(RT < 200 | RT > 2000) -> experiment.outliers - Logical OR (“either or both”)
  • 13. Filtering Data: == and != ! Get a match / equals: - experiment %>% filter(TrialsRemaining == 0) ! Words/categorical variables need quotes: - experiment %>% filter(Condition=='Implausible') ! != means “not equal to”: - experiment %>% filter(Subject != 'S23’) - Drops Subject “S23” Note DOUBLE equals sign
  • 14. Filtering Data: %in% ! Sometimes our inclusion criteria aren't so mathematical ! Suppose I just want the “Ducks” and “Panther” items ! We can check against any arbitrary list: - experiment %>% filter(ItemName %in% c('Ducks', 'Panther')) ! Or, keep just things that aren't in a list: - experiment %>% filter(Subject %in% c('S10', 'S23') == FALSE)
  • 15. Logical Operators Review ! Summary - > Greater than - >= Greater than or equal to - < Less than - <= Less than or equal to - & AND - | OR - == Equal to - != Not equal to - %in% Is this included in a list?
  • 16. Week 2.2: Data Processing in R ! Filtering ! Basic Filtering ! Advanced Filtering ! Mutate ! Basic Variable Creation and Editing ! if_else() ! Variable Types ! Other Functions & Packages
  • 17. Mutate ! The last tidyverse function we’ll look at is mutate() ! Add new variables ! Transform variables ! Recode or rescore variables
  • 18. Mutate ! We can use mutate() to create new columns in our dataframe: - experiment %>% mutate(ExperimentNumber = 1) -> experiment We are creating a column named ExperimentNumber, and assigning the value 1 for every observation Then, we need to store the updated data back into our experiment dataframe
  • 19. Mutate ! We can use mutate() to create new columns in our dataframe: - experiment %>% mutate(ExperimentNumber = 1) -> experiment
  • 20. Mutate ! A more interesting example is where the assigned value is based on a formula ! experiment %>% mutate(RTinSeconds = RT/1000) -> experiment ! For each row, finds the RT in seconds for that specific trial and saves that into RTinSeconds - Similar to an Excel formula • If we wanted to alter the original RT column, we could instead do: mutate(RT = RT/1000)
  • 21. Mutate ! We can even use other functions in calculating new columns ! experiment %>% mutate(logRT = log(RT)) -> experiment ! Applies the logarithmic transformation to each RT and saves that as logRT
  • 22. Week 2.2: Data Processing in R ! Filtering ! Basic Filtering ! Advanced Filtering ! Mutate ! Basic Variable Creation and Editing ! if_else() ! Variable Types ! Other Functions & Packages
  • 23. if_else() IF YOU WANT DESSERT, EAT YOUR PEAS … OR ELSE!
  • 24. if_else() ! if_else(): A function that uses a test to decide which of two values to assign: ! experiment %>% mutate( Half= if_else( TrialsRemaining >= 15, 1, 2) ) -> experiment Function name If 15 or more trials remain… “Half” is 1 If NOT, “Half” is 2 A new column called “Half”--what value are we going to assign ?
  • 25. Which do you like better? - experiment %>% mutate( Half=if_else(TrialsRemaining >= 15, 1, 2)) -> experiment ! vs: - TrialsPerSubject <- 30 - experiment %>% mutate( Half=if_else(TrialsRemaining >= TrialsPerSubject / 2, 1, 2)) -> experiment
  • 26. Which do you like better? - experiment %>% mutate( Half=if_else(TrialsRemaining >= 15, 1, 2)) -> experiment ! vs: - TrialsPerSubject <- 30 - experiment %>% mutate( Half=if_else(TrialsRemaining >= TrialsPerSubject / 2, 1, 2)) -> experiment - Explains where the 15 comes from—helpful if we come back to this script later - We can also refer to CriticalTrialsPerSubject variable later in the script & this ensure it’s consistent - Easy to update if we change the number of trials
  • 27. if_else() ! Instead of comparing to specific numbers (like 15), we can use other columns or a formula: ! experiment %>% mutate( RT.Fenced = if_else(RT < 200, 200, RT)) -> experiment ! What is this doing?
  • 28. if_else() ! Instead of comparing to specific numbers (like 15), we can use other columns or a formula: ! experiment %>% mutate( RT.Fenced = if_else(RT < 200, 200, RT)) -> experiment ! Creates an RT.Fenced column where: ! Where RTs are less than 200 ms, replace them with 200 ! Otherwise, use the original RT value ! i.e., replace all RTs less than 200 ms with the value 200
  • 29. if_else() ! Instead of comparing to specific numbers (like 15), we can use other columns or a formula: ! experiment %>% mutate( RT.Fenced = if_else(RT < 200, 200, RT)) -> experiment ! For even more complex rescoring, use case_when()
  • 30. Week 2.2: Data Processing in R ! Filtering ! Basic Filtering ! Advanced Filtering ! Mutate ! Basic Variable Creation and Editing ! if_else() ! Variable Types ! Other Functions & Packages
  • 31. Types ! R treats continuous & categorical variables differently: ! These are different data types: - Numeric - Character: Freely entered text (e.g., open response question) - Factor: Variable w/ fixed set of categories (e.g., treatment vs. placebo)
  • 32. Types ! R’s current heuristic when reading in data: - No letters, purely numbers → numeric - Letters anywhere in the column → character
  • 33. Types: as.factor() ! For variables with a fixed set of categories, we may want to convert to factor ! experiment %>% mutate(Condition=as.factor(Condition)) -> experiment
  • 34. Types: as.numeric() ! Age was read as a character variable because some people “Declined to report” ! But, we may want to treat it as numeric despite this
  • 35. Types: as.numeric() ! Age was read as a character variable because some people “Declined to report” - experiment %>% mutate(AgeNumeric=as.numeric(Age)) -> experiment • We now get quantitative information on Age • Values that couldn’t be turned into numbers are listed as NA • NA means missing data--we’ll discuss that more later in the term
  • 36. Week 2.2: Data Processing in R ! Filtering ! Basic Filtering ! Advanced Filtering ! Mutate ! Basic Variable Creation and Editing ! if_else() ! Variable Types ! Other Functions & Packages
  • 37. Other Functions ! Some built-in analyses: ! aov() ANOVA ! lm() Linear regression ! glm() Generalized linear models (e.g., logistic) ! cor.test() Correlation ! t.test() t-test
  • 38. Other Packages ! Some other relevant packages: ! lavaan Latent variable analysis and structural equation modeling ! psych Psychometrics (scale construction, etc.) ! party Random forests ! stringr Working with character variables ! lme4: Package for linear mixed-effects models ! Get this one for next week
  • 39. Getting Help ! Get help on a specific known function: - ?t.test - Lists all arguments ! Try to find a function on a particular topic: - ??logarithm