Skip to content

Commit 2814ae7

Browse files
committed
Minor fixes and renv dependencies
1 parent 39d23bf commit 2814ae7

9 files changed

Lines changed: 8030 additions & 30 deletions

File tree

.Rprofile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
source("renv/activate.R")

Code/02_extract.R

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,15 @@ for (i in seq(1, length(PIDs))) {
4141
basins <- earlywarnings::livpotential_ews(y3)
4242

4343
# Identify local minima (valleys) in the potential function
44-
lm <- photobiology::get_valleys(
45-
basins$grid.points, basins$pot,
46-
ignore_threshold = -0.2,
44+
valley_idx <- photobiology::find_valleys(
45+
basins$pot,
4746
strict = TRUE,
4847
span = 3
4948
)
5049

51-
# Extract indices of local minima (valleys)
52-
basinsind <- which(basins$grid.points %in% lm$x)
53-
basinlocs <- lm$x # Coordinates of basin locations
50+
# Extract indices and coordinates of local minima (valleys)
51+
basinsind <- which(valley_idx)
52+
basinlocs <- basins$grid.points[valley_idx]
5453

5554
# Check if exactly one local minimum exists in the range [-5, 5]
5655
values_in_range <- sum(basinlocs >= -5 & basinlocs <= 5)

Code/07_lasso.R

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ for (i in 1:length(rows)) {
4141
lres[i, res$DV_lasso$predictor] <- res$DV_lasso$coefficient # Store coefficients in result table
4242
}
4343

44-
# Handle 'Type' column separately
45-
lres$Type <- lres$Type1
46-
lres <- lres[, -ncol(lres)] # Remove the last column (Type1 placeholder)
44+
# Handle 'Type' dummy columns: model.matrix creates Type0 and Type1
45+
if ("Type1" %in% colnames(lres)) lres$Type <- lres$Type1
46+
if ("Type0" %in% colnames(lres)) lres$Type0 <- NULL
47+
if ("Type1" %in% colnames(lres)) lres$Type1 <- NULL
4748

4849
## Study 2 ####
4950
# Subset the data for Study 2 and keep the first row per participant
@@ -72,9 +73,10 @@ for (i in 1:length(rows)) {
7273
lres2[i, res$DV_lasso$predictor] <- res$DV_lasso$coefficient # Store coefficients
7374
}
7475

75-
# Handle 'Type' column separately for Study 2
76-
lres2$Type <- lres2$Type1
77-
lres2 <- lres2[, -ncol(lres2)] # Remove the last column (Type1 placeholder)
76+
# Handle 'Type' dummy columns for Study 2
77+
if ("Type1" %in% colnames(lres2)) lres2$Type <- lres2$Type1
78+
if ("Type0" %in% colnames(lres2)) lres2$Type0 <- NULL
79+
if ("Type1" %in% colnames(lres2)) lres2$Type1 <- NULL
7880

7981
# Transpose and round results for both studies
8082
lres1 <- t(round(lres, 2)) # Transpose and round results for Study 1
@@ -85,14 +87,6 @@ lres <- cbind(lres1, lres2)
8587
rownames(lres)[2:3] <- c("P2N-ASR", "N2P-ASR") # Rename rows for clarity
8688
rownames(lres)[8:11] <- c("mP2N-ASM", "sdP2N-ASM", "mN2P-ASM", "sdN2P-ASM")
8789

88-
# Create and save LASSO table (if needed)
89-
source("./code/table_lasso.R")
90-
# Uncomment below to save results as a table:
91-
# gtsave(tbl, "./tables/table_lasso.html")
92-
# gtsave(tbl, file = "./tables/table_lasso.pdf")
93-
# gtsave(tbl, file = "./tables/table_lasso.png")
94-
# gtsave(tbl, file = "./tables/table_lasso.docx")
95-
9690
# Visualization: Create a data frame for plotting
9791
data <- data.frame(t(lres)) # Convert results to a data frame
9892
colnames(data)[2:3] <- c("P2N-ASR", "N2P-ASR") # Rename columns for clarity

Code/08_stepwiseRegression.R

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,6 @@ rownames(lres)[5] <- "P2N-ASR" # Rename row for clarity
6969
# Optional: Save the results to a CSV file
7070
# write.csv(lres, file = "./tables/stepwiseData.csv")
7171

72-
# Create and save Stepwise regression table (optional)
73-
source("./code/table_stepwise.R")
74-
# Uncomment the lines below to save the table in different formats:
75-
# gtsave(tbl, "./tables/table_stepwise.html")
76-
# gtsave(tbl, file = "./tables/table_stepwise.pdf")
77-
# gtsave(tbl, file = "./tables/table_stepwise.png")
78-
# gtsave(tbl, file = "./tables/table_stepwise.docx")
79-
8072
# Visualization: Create a data frame for plotting
8173
data <- data.frame(t(lres)) # Convert results to a data frame
8274
colnames(data)[5] <- "P2N-ASR" # Rename column for clarity

README.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,36 @@
11
## Code and Data for the article "Bistability and Affect Shift Dynamics in the Prediction of Psychological Well-Being"
2-
CITATION
3-
Goicoechea, C., Dakos, V., Sanabria, D., Heshmati, S., Westhoff, M., Banos, O., Pomares, H., Hofmann, S. G., & Perakakis, P. (2024). Bistability and affect shift dynamics in the prediction of psychological well-being. Emotion. Advance online publication. https://dx.doi.org/10.1037/emo0001454
2+
3+
### Citation
4+
5+
Goicoechea, C., Dakos, V., Sanabria, D., Heshmati, S., Westhoff, M., Banos, O., Pomares, H., Hofmann, S. G., & Perakakis, P. (2024). Bistability and affect shift dynamics in the prediction of psychological well-being. Emotion, 25(4), 982–996. https://dx.doi.org/10.1037/emo0001454
46

57
[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.15001651.svg)](https://doi.org/10.5281/zenodo.15001651)
8+
9+
### Repository Structure
10+
11+
```
12+
Data/ # EMA and survey data for Spanish and German samples
13+
Code/
14+
01_preprocess.R # Data cleaning and preparation
15+
02_extract.R # Bistability metrics extraction (basins, affect shifts, residence times)
16+
05_PCA.R # Principal Component Analysis
17+
06_corrplot.R # Correlation plots
18+
07_lasso.R # LASSO regression
19+
08_stepwiseRegression.R
20+
09_relativeImportance.R
21+
auxFunctions.R # Helper functions (LASSO, regression, nested models)
22+
predictor_colors.R
23+
```
24+
25+
### Reproducibility
26+
27+
This project uses [`renv`](https://rstudio.github.io/renv/) to manage R package dependencies. To reproduce the analysis environment:
28+
29+
```r
30+
# install.packages("renv") # if not already installed
31+
renv::restore()
32+
```
33+
34+
This will install all required packages at the exact versions recorded in `renv.lock` (R 4.5.2).
35+
36+
Scripts are intended to be run from the project root directory.

0 commit comments

Comments
 (0)