SlideShare a Scribd company logo
1 of 39
Download to read offline
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

More Related Content

What's hot

Mixed Effects Models - Random Slopes
Mixed Effects Models - Random SlopesMixed Effects Models - Random Slopes
Mixed Effects Models - Random SlopesScott Fraundorf
 
Mixed Effects Models - Autocorrelation
Mixed Effects Models - AutocorrelationMixed Effects Models - Autocorrelation
Mixed Effects Models - AutocorrelationScott 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 - Post-Hoc Comparisons
Mixed Effects Models - Post-Hoc ComparisonsMixed Effects Models - Post-Hoc Comparisons
Mixed Effects Models - Post-Hoc ComparisonsScott Fraundorf
 
Mixed Effects Models - Crossed Random Effects
Mixed Effects Models - Crossed Random EffectsMixed Effects Models - Crossed Random Effects
Mixed Effects Models - Crossed Random EffectsScott Fraundorf
 
Mixed Effects Models - Empirical Logit
Mixed Effects Models - Empirical LogitMixed Effects Models - Empirical Logit
Mixed Effects Models - Empirical LogitScott 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
 
Linear regression
Linear regressionLinear regression
Linear regressionCarlo Magno
 
Logistic regression
Logistic regressionLogistic regression
Logistic regressionVARUN KUMAR
 
Logistic regression
Logistic regressionLogistic regression
Logistic regressionDrZahid Khan
 
Mixed Effects Models - Effect Size
Mixed Effects Models - Effect SizeMixed Effects Models - Effect Size
Mixed Effects Models - Effect SizeScott Fraundorf
 
Cramer row inequality
Cramer row inequality Cramer row inequality
Cramer row inequality VashuGupta8
 
T Test For Two Independent Samples
T Test For Two Independent SamplesT Test For Two Independent Samples
T Test For Two Independent Samplesshoffma5
 
What is a single sample t test?
What is a single sample t test?What is a single sample t test?
What is a single sample t test?Ken Plummer
 
Checking for normality (Normal distribution)
Checking for normality (Normal distribution)Checking for normality (Normal distribution)
Checking for normality (Normal distribution)Rizwan S A
 
multiple regression
multiple regressionmultiple regression
multiple regressionPriya Sharma
 

What's hot (20)

Mixed Effects Models - Random Slopes
Mixed Effects Models - Random SlopesMixed Effects Models - Random Slopes
Mixed Effects Models - Random Slopes
 
Mixed Effects Models - Autocorrelation
Mixed Effects Models - AutocorrelationMixed Effects Models - Autocorrelation
Mixed Effects Models - Autocorrelation
 
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 - Post-Hoc Comparisons
Mixed Effects Models - Post-Hoc ComparisonsMixed Effects Models - Post-Hoc Comparisons
Mixed Effects Models - Post-Hoc Comparisons
 
Mixed Effects Models - Crossed Random Effects
Mixed Effects Models - Crossed Random EffectsMixed Effects Models - Crossed Random Effects
Mixed Effects Models - Crossed Random Effects
 
Mixed Effects Models - Empirical Logit
Mixed Effects Models - Empirical LogitMixed Effects Models - Empirical Logit
Mixed Effects Models - Empirical Logit
 
Mixed Effects Models - Signal Detection Theory
Mixed Effects Models - Signal Detection TheoryMixed Effects Models - Signal Detection Theory
Mixed Effects Models - Signal Detection Theory
 
Poisson regression models for count data
Poisson regression models for count dataPoisson regression models for count data
Poisson regression models for count data
 
Linear regression
Linear regressionLinear regression
Linear regression
 
Logistic regression
Logistic regressionLogistic regression
Logistic regression
 
Mixed models
Mixed modelsMixed models
Mixed models
 
Logistic regression
Logistic regressionLogistic regression
Logistic regression
 
Mixed Effects Models - Effect Size
Mixed Effects Models - Effect SizeMixed Effects Models - Effect Size
Mixed Effects Models - Effect Size
 
Cramer row inequality
Cramer row inequality Cramer row inequality
Cramer row inequality
 
T Test For Two Independent Samples
T Test For Two Independent SamplesT Test For Two Independent Samples
T Test For Two Independent Samples
 
Statistical power
Statistical powerStatistical power
Statistical power
 
What is a single sample t test?
What is a single sample t test?What is a single sample t test?
What is a single sample t test?
 
Checking for normality (Normal distribution)
Checking for normality (Normal distribution)Checking for normality (Normal distribution)
Checking for normality (Normal distribution)
 
Correlation
CorrelationCorrelation
Correlation
 
multiple regression
multiple regressionmultiple regression
multiple regression
 

Similar to 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
 

Similar to 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
 

More from Scott Fraundorf

Mixed Effects Models - Power
Mixed Effects Models - PowerMixed Effects Models - Power
Mixed Effects Models - PowerScott 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 - Centering and Transformations
Mixed Effects Models - Centering and TransformationsMixed Effects Models - Centering and Transformations
Mixed Effects Models - Centering and TransformationsScott 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
 

More from Scott Fraundorf (6)

Mixed Effects Models - Power
Mixed Effects Models - PowerMixed Effects Models - Power
Mixed Effects Models - Power
 
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 - Centering and Transformations
Mixed Effects Models - Centering and TransformationsMixed Effects Models - Centering and Transformations
Mixed Effects Models - Centering and Transformations
 
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
 

Recently uploaded

Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 

Recently uploaded (20)

Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 

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