# Calculate separate Cohen's D for large set of participants

I have been trying to find a way to calculate Cohen's D separately for each of 176 participants to compare the dependent variable (mean JOL) across 2 levels of the independent variable (aesthetics: high_aesthetic, or low_aesthetic). "pp_code" is the participant identifier in both datasets (high_aesthetic_data, low_aesthetic_data).

The code I have here seems to work but only for participants 1-87, not for participants 88-176. I have checked for missing and infinite values and there is no issue there, I have also tried to run this same code but for a different variable and the same issue persists. I have tried to specifically run only participants 88-176 but it will not run these participants even though it recognises their existence in both datasets.

Here is the code:

# Cohen's D for Individual cue integration with 0.2 or above counted as reliable effect for integration for each participant

high_aesthetic_data <- MEANJOLxCONDITION[MEANJOLxCONDITION\$aesthetics == "high_aesthetic", ]
low_aesthetic_data <- MEANJOLxCONDITION[MEANJOLxCONDITION\$aesthetics == "low_aesthetic", ]

# Initialize an empty vector to store Cohen's d values

cohen_d_results <- list()

# Loop through each unique participant code

unique_pp_codes <- unique(high_aesthetic_data\$pp_code)
for (pp_code in unique_pp_codes) {

# Subset the "jol" data for the current participant code from high aesthetic data

high_aesthetic_jol <- high_aesthetic_data\$jol[high_aesthetic_data\$pp_code == pp_code]

# Subset the "jol" data for the current participant code from low aesthetic data

low_aesthetic_jol <- low_aesthetic_data\$jol[low_aesthetic_data\$pp_code == pp_code]

# Check if both subsets contain valid data

if (!is.null(high_aesthetic_jol) && !is.null(low_aesthetic_jol) && length(high_aesthetic_jol) > 1 && length(low_aesthetic_jol) > 1) {
# Calculate Cohen's d
cohen_d <- cohens_d(high_aesthetic_jol, low_aesthetic_jol)

``````# Store the Cohen's d value with the participant code as the name
cohen_d_results[[as.character(pp_code)]] <- cohen_d
``````

} else {
# If data is not valid, store NA
cohen_d_results[[as.character(pp_code)]] <- NA
}
}

# Print the results

print(cohen_d_results)

Hi Sebastian,

Could you copy your code again and paste it here, between a pair of triple backticks, like this? And could you include the code or package for the `cohen_d()` function?

````````` r
[<-- paste code here]
```
``````

That will help folks be able to understand your code more easily.

Hi,

Sorry I wasn't aware the formatting would be like this. The package for Cohen's D that I am using is library(effectsize). Here is the code again:

````````` r
# Cohen's D for Individual cue integration with 0.2 or above counted as reliable effect for integration for each participant

high_aesthetic_data <- MEANJOLxCONDITION[MEANJOLxCONDITION\$aesthetics == "high_aesthetic", ]
low_aesthetic_data <- MEANJOLxCONDITION[MEANJOLxCONDITION\$aesthetics == "low_aesthetic", ]

# Initialize an empty vector to store Cohen's d values

cohen_d_results <- list()

# Loop through each unique participant code

unique_pp_codes <- unique(high_aesthetic_data\$pp_code)
for (pp_code in unique_pp_codes) {

# Subset the "jol" data for the current participant code from high aesthetic data

high_aesthetic_jol <- high_aesthetic_data\$jol[high_aesthetic_data\$pp_code == pp_code]

# Subset the "jol" data for the current participant code from low aesthetic data

low_aesthetic_jol <- low_aesthetic_data\$jol[low_aesthetic_data\$pp_code == pp_code]

# Check if both subsets contain valid data

if (!is.null(high_aesthetic_jol) && !is.null(low_aesthetic_jol) && length(high_aesthetic_jol) > 1 && length(low_aesthetic_jol) > 1) {
# Calculate Cohen's d
cohen_d <- cohens_d(high_aesthetic_jol, low_aesthetic_jol)

``````

# Store the Cohen's d value with the participant code as the name

cohen_d_results[[as.character(pp_code)]] <- cohen_d

``````
} else {
# If data is not valid, store NA
cohen_d_results[[as.character(pp_code)]] <- NA
}
}

# Print the results

print(cohen_d_results)
``````

That's all right ā it's new to many folks who come with questions. However, it looks like you didn't catch all the code with the code block, and included the line ``` r twice. Could you edit your code block to fix that?

Thanks, that's helpful, and having access to representative data would help, too, but this might be enough for folks to get started with for the moment.

``````# Cohen's D for Individual cue integration with 0.2 or above counted as reliable effect for integration for each participant

high_aesthetic_data <- MEANJOLxCONDITION[MEANJOLxCONDITION\$aesthetics == "high_aesthetic", ]
low_aesthetic_data <- MEANJOLxCONDITION[MEANJOLxCONDITION\$aesthetics == "low_aesthetic", ]

# Initialize an empty vector to store Cohen's d values

cohen_d_results <- list()

# Loop through each unique participant code

unique_pp_codes <- unique(high_aesthetic_data\$pp_code)
for (pp_code in unique_pp_codes) {

# Subset the "jol" data for the current participant code from high aesthetic data

high_aesthetic_jol <- high_aesthetic_data\$jol[high_aesthetic_data\$pp_code == pp_code]

# Subset the "jol" data for the current participant code from low aesthetic data

low_aesthetic_jol <- low_aesthetic_data\$jol[low_aesthetic_data\$pp_code == pp_code]

# Check if both subsets contain valid data

if (!is.null(high_aesthetic_jol) && !is.null(low_aesthetic_jol) && length(high_aesthetic_jol) > 1 && length(low_aesthetic_jol) > 1) {
# Calculate Cohen's d
cohen_d <- cohens_d(high_aesthetic_jol, low_aesthetic_jol)

# Store the Cohen's d value with the participant code as the name

cohen_d_results[[as.character(pp_code)]] <- cohen_d

} else {
# If data is not valid, store NA
cohen_d_results[[as.character(pp_code)]] <- NA
}
}

# Print the results

print(cohen_d_results)
``````
1 Like

Just realized there's link for that!

OK, it looks like you may need to share representative data, but first, could you say what this means?

When you say "will not run", does that mean you get unexpected results? Or do you get errors? If you do get errors, could you post them here, too?

Thanks for your help but I have figured it out now.

You should check the indexing of participant codes in both datasets (high_aesthetic_data and low_aesthetic_data) to ensure they align correctly for all participants (1-176).