Source code for this document is found here.

# emadata <- readr::read_rds("./data/EMA_data_Moti_P10.csv")
# readr::write_csv(emadata, "./data/EMA_data_Moti_P10.csv")
emadata <- readr::read_csv("./data/EMA_data_Moti_P10.csv")

emadata_nested <- emadata %>% dplyr::group_by(User) %>% 
                  dplyr::select(#autonomy, competence, relatedness,
                                pleasure, interest, importance,
                                situation_requires = required, 
                                anxiety_guilt_avoidance = anxiety_guilt, 
                                another_wants = for_others,
                                Date = dateTime,
                                User,
                                task) %>% 
                  na.omit() %>% ### NOTE! NAs removed!
  tidyr::nest() 

# Show sample size for each participant:
# emadata_nested %>% 
#   mutate(n = map_dbl(data, nrow))

set.seed(1000)

emadata_nested_wrangled <- emadata_nested %>%
  dplyr::mutate(data = purrr::map(data, ~dplyr::mutate(.x, 
                                                       date = as.Date(Date),
                                                       timediff = c(NA, diff(Date))))) %>% 
  # Filter out answers less than 15 minutes from the last one, then remove the difference variable
  dplyr::mutate(data = purrr::map(data, ~dplyr::filter(.x, timediff > 15))) %>% 
  dplyr::mutate(data = purrr::map(data, ~dplyr::select(.x, -timediff))) %>% 
  # Create three datasets, where one daily observation is randomly selected from those available:
  dplyr::mutate(sample1 = purrr::map(.x = data, 
                                     .f = ~dplyr::group_by(.x, date) %>% 
                                       dplyr::sample_n(., size = 1, replace = FALSE) %>% 
                                       dplyr::ungroup())) %>% 
  dplyr::mutate(sample2 = purrr::map(.x = data, 
                                     .f = ~dplyr::group_by(.x, date) %>% 
                                       dplyr::sample_n(., size = 1, replace = FALSE) %>% 
                                       dplyr::ungroup())) %>% 
  dplyr::mutate(sample3 = purrr::map(.x = data, 
                                     .f = ~dplyr::group_by(.x, date) %>% 
                                       dplyr::sample_n(., size = 1, replace = FALSE) %>% 
                                       dplyr::ungroup())) %>% 
  # Create "task-normed" variables, where the previous instance of the task is substracted from the current one:
  dplyr::mutate(taskNormed = purrr::map(data, ~dplyr::group_by(., task))) %>%
  dplyr::mutate(taskNormed = purrr::map(taskNormed, ~dplyr::mutate_if(.x, is.numeric, ~(.-lag(.))))) %>%
  dplyr::mutate(taskNormed = purrr::map(taskNormed, ~na.omit(.x))) %>%
  dplyr::mutate(taskNormed = purrr::map(taskNormed, ~dplyr::ungroup(.x))) %>%
  dplyr::mutate(taskNormed = purrr::map(taskNormed, ~dplyr::mutate_if(.x, 
                                                                      is.numeric, 
                                                                      ~scales::rescale(.x, 
                                                                                       to = c(0, 49))))) %>%
  # # Take daily averages to ensure ~equally spaced observations: 
  dplyr::mutate(data_daily = purrr::map(data, 
                                        ~dplyr::group_by(., date))) %>%
  dplyr::mutate(data_daily = purrr::map(data_daily, 
                                        ~dplyr::summarise_if(.x, is.numeric, mean, na.rm = TRUE))) %>%
  # Remove day and task variables
  dplyr::mutate(data_with_tasks_and_dates = data,
                data = purrr::map(data, ~dplyr::select(.x, -Date, -date, -task))) %>% 
  dplyr::mutate(taskNormed = purrr::map(taskNormed, ~dplyr::select(.x, -Date, -date, -task))) %>%
  dplyr::mutate(data_daily_with_tasks_and_dates = data_daily,
                data_daily = purrr::map(data_daily, ~dplyr::select(.x, -date))) %>% 
  # Normalise all numeric variables
  dplyr::mutate(data_standardised = purrr::map(data, ~dplyr::mutate_if(.x, is.numeric,
                                                           ~((.x / max(.x)))))) %>% 
  dplyr::mutate(sample1_standardised = purrr::map(sample1, ~dplyr::mutate_if(.x, is.numeric,
                                                           ~((.x / max(.x)))))) %>%
  dplyr::mutate(sample2_standardised = purrr::map(sample2, ~dplyr::mutate_if(.x, is.numeric,
                                                           ~((.x / max(.x)))))) %>%
  dplyr::mutate(sample3_standardised = purrr::map(sample3, ~dplyr::mutate_if(.x, is.numeric,
                                                           ~((.x / max(.x)))))) %>%
  dplyr::mutate(data_daily_standardised = purrr::map(data_daily, ~dplyr::mutate_if(.x, is.numeric,
                                                           ~((.x / max(.x)))))) %>% 
  dplyr::mutate(taskNormed_standardised = purrr::map(taskNormed, ~dplyr::mutate_if(.x, is.numeric,
                                                           ~((.x / max(.x)))))) %>% 
  # Retain first and last observation of the day:
  dplyr::mutate(data_firstlast_divided_by_max = purrr::map(.x = data_with_tasks_and_dates, 
                                           .f = ~dplyr::group_by(.x, date) %>% 
                                             dplyr::arrange(Date) %>%
                                             dplyr::filter(row_number() == 1 | row_number() == n()) %>% 
                                             dplyr::ungroup() %>% 
                                             dplyr::mutate_if(., is.numeric,
                                                              ~(. / max(., na.rm = TRUE)))),
                data_firstlast_divided_by_max_with_tasks_and_dates = data_firstlast_divided_by_max,
                data_firstlast_divided_by_max = purrr::map(data_firstlast_divided_by_max, 
                                                           ~dplyr::select(.x, -Date, -date, -task)))

In this document, we provide supplementary information about recurrence-based analyses. We also provide code for the figures in the manuscript.

Note: We use the terms motivational attractor, profile and configuration interchangeably in this context.


Recurrence quantification and recurrence network analysis

As described in the manuscript, to explore the dynamics of a phenomenon while making no assumptions about distributional shapes of observations or their errors, about linearity, or about the time-lags involved, researchers can perform Recurrence Quantification Analysis, which provides a visual intuition about the temporal organisation of a system. There are two flavours of recurrence-based analysis: Recurrence Quantification Analysis (RQA), which quantifies the dynamics and temporal patterns of the states of a system (Marwan et al. 2007) and Recurrence Network Analysis, which quantifies the geometric structure and evolution of the system in a multidimensional state space (Zou et al. 2019).

The first step of the analysis is to plot the data points with their distances to all other data points in a matrix, as shown in the left panel of Figure below. Red cells indicate highly similar values, white cells intermediate ones, and blue cells highly dissimilar values. After this, the distance matrix can be thresholded (as in the right panel of Figure 4) by applying a rule, that binarises each cell into recurring (black) or not (white). The rule in Figure below is “preserve recurrences with absolute distance of 1 or less”.


rqa_pedagogical_distance <- magick::image_read("./figures/rqa_pedagogical_distance.png") %>% 
  magick::image_border(., color = "white", geometry = "20x10")

rqa_pedagogical_thresholded <- magick::image_read("./figures/rqa_pedagogical_thresholded.png") %>% 
  magick::image_border(., color = "white", geometry = "20x10")

magick::image_append(c(rqa_pedagogical_distance, rqa_pedagogical_thresholded))
Left: A distance matrix of a hypothetical time series of length 6: {1, 5, 4, 3, 2, 6}. Red cells indicate highly similar values, white cells intermediate ones, and blue cells highly dissimilar values. Right: A recurrence plot created by thresholding the distance matrix such, that only cells indicating distances of 1 or less are preserved.

Left: A distance matrix of a hypothetical time series of length 6: {1, 5, 4, 3, 2, 6}. Red cells indicate highly similar values, white cells intermediate ones, and blue cells highly dissimilar values. Right: A recurrence plot created by thresholding the distance matrix such, that only cells indicating distances of 1 or less are preserved.


# grid::grid.raster(png::readPNG("./figures/rqa_pedagogical_distance.png"))

While Figure above presents an auto-recurrence plot of a single time series, similar matrices can be used to represent the progression and recurrent states of entire systems over time: Recurrence plots are, in essence, visualisations of distance matrices*, and the distance in a cell can in principle be calculated for an arbitrary number of variables. Figure below presents actual data from one participant, where—instead of single values—the time points consist of configurations of six motivation-related variables (the same as in manuscript’s Figure with the time-varying vector autoregressive model). The thresholding rule in the right panel of the plot is “preserve only 5% of the closest configurations”, with closeness defined as proximity of coordinates in six-dimensional space. A visual inspection of Figure below shows that the recurrent states mostly happen in the second half of the study period. Quantifying patterns produced by recurrence plots, that is, deriving complexity measures from them, can tell important information about the system the data represents.

*We used the Euclidean distance, which can be problematic under fat tails. But given that the analysis aims to unveil oft-recurring configurations instead of rare extreme ones, this is less of a concern: Extreme states would simply show up as unique or uncategorised in the analysis.


grid::grid.raster(png::readPNG("./figures/rqa_biplot.png"))
A 6-variable motivation system of a single participant. Panel A depicts an unthresholded distance matrix, where each cell represents a measurement occasion, with red colours indicating the value is close to the corresponding time point on the other axis, while blue colours indicate the contrary, and white implies an intermediate distance. Panel B is a recurrence plot, where this unthresholded plot has been binarised with the rule of retaining only 5% of the closest recurrences. Drawn with R package casnet.

A 6-variable motivation system of a single participant. Panel A depicts an unthresholded distance matrix, where each cell represents a measurement occasion, with red colours indicating the value is close to the corresponding time point on the other axis, while blue colours indicate the contrary, and white implies an intermediate distance. Panel B is a recurrence plot, where this unthresholded plot has been binarised with the rule of retaining only 5% of the closest recurrences. Drawn with R package casnet.

Due to the recurrence plot being in essence a distance matrix, they can also be represented as networks, with time points as nodes – connected by lines if they are close enough to correspond to a configuration observed in some previous time point.

Recurrence network demonstration

The participant was beeped 5 times a day for all the questions, during an 8-hour period they determined as their workday – see here for details. In order to meet the requirement of approximately equidistant measures as well as possible, in the analysis presented in the manuscript, we chose to only use the first and last observation of the day (when more than 2 time points were available for a given day).

Below, we present other choices that could have been made as sensitivity analyses. Results in three tabs come from sampling one time point randomly from all available measurement occasions each day, with the aim of fulfilling the equidistance requirement as well as possible. We also present results for using all the data, ignoring the equidistance requirement completely. Lastly, we present results for “task-norming” the data – that is, substracting from the current six values of the variables, those from the previous time the task was conducted (and ignoring the equidistance requirement). It can be seen, that most of the profiles can be found from analysis, although their order may differ. We opted not to take the average of daily measures, as the ecological momentary assessment questions were tied to the particular tasks and moments at hand, and an average would not have necessarily represented any of the occasions it consisted of.

First and last daily observations

Here’s the 6-dimensional motivation system’s recurrence plot, weighted by similarity.

set.seed(100)

#######################

# si = similarity under the radius
emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled %>% 
  dplyr::mutate(RN = purrr::map(.x = data_firstlast_divided_by_max,
                                .f =  ~casnet::rn(.x %>% dplyr::select(#autonomy, competence, relatedness,
                                                                       pleasure, interest, importance,
                                                                       situation_requires, 
                                                                       anxiety_guilt_avoidance, 
                                                                       another_wants), 
                                                  doEmbed = FALSE, 
                                                  weighted = TRUE, 
                                                  weightedBy = "si",
                                                  targetValue = 0.05, 
                                                  emRad = NA)))
## 
## Auto-recurrence: Setting diagonal to (1 + max. distance) for analyses
## 
## Searching for a radius that will yield 0.05 for RR

emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(graph_from_adjacency = purrr::map(.x = RN,
                                                  .f = ~igraph::graph_from_adjacency_matrix(.x, 
                                                                                            weighted = TRUE, 
                                                                                            mode = "upper", 
                                                                                            diag = FALSE)))

# Edges with their distances
emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(edges_with_distances = purrr::map(.x = graph_from_adjacency,
                                                  .f = ~igraph::E(.x)$weight),
                graph_from_adjacency_orig = graph_from_adjacency)

# Larger values are closer to the state; inverse of weight makes it more intuitive
for (i in 1:nrow(emadata_nested_wrangled_both_recnets)) {
igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$weight <- (1/emadata_nested_wrangled_both_recnets$edges_with_distances[[i]])
}
# A later note to self: Now weight is a measure of distance; how far apart two time points are
# (under the radius, i.e. they're reasonably similar to begin with)

####### To check:
# igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[1]])$weight
# igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency_orig[[1]])$weight

emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(RN_plot = purrr::map(.x = RN,
                                .f =  ~casnet::rn_plot(.x,
                                                       plotDimensions = TRUE,
                                                       xlab = "6-dimensional motivation system",
                                                       ylab = "6-dimensional motivation system")))


# Make node size equal to strength. Strength is the sum of a node's edge weights.
for (i in 1:nrow(emadata_nested_wrangled_both_recnets)) {
igraph::V(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$size <- (igraph::strength(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]]))
}

# Rescaling weight as "width"; varies between 5 and 10
for (i in 1:nrow(emadata_nested_wrangled_both_recnets)) {
igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$width <- 
  casnet::elascer(igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$weight, lo = 5, hi = 10)
}

The lengthy code chunk below extracts and marks attractors in the data. The code first finds the node with largest strength centrality, then classifies all nodes connecting to it as the attractor labelled “1st”. Then it looks for the node with second largest strength centrality, which does not connect to the first, and labelles all nodes connecting to it as “2nd, and so forth. If the 6-variable configuration could be classified under several attractors, our”algorithm" categorises it under the strongest pattern it connects to.

# Get number of maximally connected node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(strongest_day = purrr::map(.x = graph_from_adjacency,
                                     .f =  ~which.max(igraph::strength(.x))
                                     ))



emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(list_of_edges = purrr::map(.x = graph_from_adjacency,
                                     .f =  ~igraph::get.data.frame(.x)
                                     ))

emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = 
                  purrr::map2(.x = data_firstlast_divided_by_max,
                              .y = graph_from_adjacency,
                              .f = ~{
                                data.frame(.x, 
                                           strength = igraph::strength(.y)) %>% 
                                  dplyr::mutate(time = dplyr::row_number()) %>% 
                                  tidyr::pivot_longer(cols = c(-strength, -time))
                              }
  ))

# Extract nodes (i.e. times) which connect to the strongest (i.e. most connected) node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = strongest_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))


# Get number of 2nd maximally connected node, which doesn't connect to the 1st
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(secondary_attractor_day = purrr::map2(.x = graph_from_adjacency,
                                                      .y = connecting_to_strongest,
                                                      .f =  ~{
                                                        data.frame(strength = igraph::strength(.x), 
                                                                   time = 1:length(igraph::strength(.x))) %>% 
                                                          dplyr::filter(!time %in% .y) %>% 
                                                          dplyr::arrange(desc(strength)) %>% 
                                                          dplyr::slice(1) %>% 
                                                          dplyr::pull(time)
                                                      }                                                    
  ))

# Extract nodes (i.e. times) which connect to the 2nd strongest node, which doesn't connect to the 1st
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_2nd_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = secondary_attractor_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

# Save as a variable in the dataset
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_2nd_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_2nd_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))

# Get number of 3rd maximally connected node, which doesn't connect to the 1st or second
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(tertiary_attractor_day = purrr::pmap(list(..1 = graph_from_adjacency,
                                                      ..2 = connecting_to_strongest,
                                                      ..3 = connecting_to_2nd_strongest),
                                                      .f =  ~{
                                                        data.frame(strength = igraph::strength(..1), 
                                                                   time = 1:length(igraph::strength(..1))) %>% 
                                                          dplyr::filter(!time %in% ..2, 
                                                                        !time %in% ..3) %>% 
                                                          dplyr::arrange(desc(strength)) %>% 
                                                          dplyr::slice(1) %>% 
                                                          dplyr::pull(time)
                                                      }                                                    
  ))

# Extract nodes (i.e. times) which connect to the 3rd strongest node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_3rd_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = tertiary_attractor_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

# Save as a variable
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_3rd_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_3rd_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))

# Get number of 4th maximally connected node, which doesn't connect to the 1st, 2nd, or 3rd
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(fourth_attractor_day = purrr::pmap(list(..1 = graph_from_adjacency,
                                                      ..2 = connecting_to_strongest,
                                                      ..3 = connecting_to_2nd_strongest,
                                                      ..4 = connecting_to_3rd_strongest),
                                                      .f =  ~{
                                                        data.frame(strength = igraph::strength(..1), 
                                                                   time = 1:length(igraph::strength(..1))) %>% 
                                                          dplyr::filter(!time %in% ..2, 
                                                                        !time %in% ..3, 
                                                                        !time %in% ..4) %>% 
                                                          dplyr::arrange(desc(strength)) %>% 
                                                          dplyr::slice(1) %>% 
                                                          dplyr::pull(time)
                                                      }                                                    
  ))

# Extract nodes (i.e. times) which connect to the 4th strongest node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_4th_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = fourth_attractor_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

# Save as a variable
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_4th_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_4th_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))

################### Make plots

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = 
                  purrr::map(.x = all_nodes_with_strengths,
                             .f = ~{
                               dplyr::mutate(.x,
                                             attractors = dplyr::case_when(
                                               strength == 0 ~ "Unique",
                                               connecting_to_strongest == TRUE ~ "1st",
                                               connecting_to_2nd_strongest == TRUE ~ "2nd",
                                               connecting_to_3rd_strongest == TRUE ~ "3rd",
                                               connecting_to_4th_strongest == TRUE ~ "4th",
                                               TRUE ~ "Uncategorised"),
                                             attractors = factor(attractors,
                                                                 levels = c("1st",
                                                                            "2nd",
                                                                            "3rd",
                                                                            "4th",
                                                                            "Uncategorised",
                                                                            "Unique")),
                                             name = factor(name,
                                                           levels = c("pleasure",
                                                                      "interest",
                                                                      "importance",
                                                                      "situation_requires",
                                                                      "anxiety_guilt_avoidance",
                                                                      "another_wants"),
                                                           labels = c("Pleasure",
                                                                      "Interest",
                                                                      "Importance",
                                                                      "Situation requires",
                                                                      "Anxiety guilt avoidance",
                                                                      "Another wants")) %>% 
                                               forcats::fct_drop()) %>% 
                                 dplyr::group_by(attractors, name) %>% 
                                 dplyr::mutate(n = n()) %>% 
                                 dplyr::ungroup() %>%
                                 dplyr::mutate(maxtime = max(time),
                                               percentage_of_total =
                                                 (n / maxtime) %>% scales::percent(accuracy = 0.1),
                                               proportion_of_total = n/maxtime,
                                               attractors_n = 
                                                 factor(paste0(attractors,
                                                               " (n = ", n, "; ", 
                                                               percentage_of_total, ")")))
                             }
                  ))

Spiral graph with colored nodes

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>%
  dplyr::mutate(node_colors = purrr::map(.x = all_nodes_with_strengths,
                                         .f = ~{tidyr::pivot_wider(.x, names_from = name) %>%
                                             dplyr::pull(attractors)}))

for (i in 1:nrow(emadata_nested_wrangled_both_recnets_nodes_plots)) {
levels(emadata_nested_wrangled_both_recnets_nodes_plots$node_colors[[i]]) <- 
  c(viridisLite::plasma(4, 
                      end = 0.8, 
                      direction = -1), "gray48", "white")
  }

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>% 
  dplyr::mutate(spiralgraph_epochs = purrr::pmap(list(..1 = graph_from_adjacency,
                                          ..2 = node_colors,
                                          ..3 = User),
                                     .f =  ~casnet::make_spiral_graph(g = ..1, 
                                                                      arcs = 4, 
                                                                      # a = .1, 
                                                                      # b = 2, 
                                                                      markTimeBy = TRUE,
                                                                      markEpochsBy = ..2,
                                                                      epochColours = ..2,
                                                                      showEpochLegend = FALSE,
                                                                      scaleEdgeSize = 1/10,
                                                                      scaleVertexSize = c(1, 5),
                                                                      showSizeLegend = FALSE,
                                                                      sizeLabel = "Node strength",
                                                                      type = "Euler",
                                                                      # alphaE = 0.1
                                                                      # title = ..3
                                     )))


# emadata_nested_wrangled_both_recnets_nodes_plots$spiralgraph_epochs[[1]] +
#   theme(plot.margin=grid::unit(c(0,0,0,0), "mm"))

# ggsave(filename = "./figures/recnetwork.png",
#        width = 7, 
#        height = 7)

# emadata_nested_wrangled_both_recnets_nodes_plots$all_nodes_with_strengths[[1]]

Attractor plot

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>%
  dplyr::mutate(observations = purrr::map_dbl(.x = data_firstlast_divided_by_max,
                                              .f = ~nrow(.)))

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>%
  dplyr::mutate(observations_daily = purrr::map_dbl(.x = data_daily,
                                              .f = ~nrow(.)))


emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>% 
  dplyr::mutate(
    attractor_plots = 
      purrr::pmap(list(..1 = all_nodes_with_strengths,
                       ..2 = observations,
                       ..3 = observations_daily,
                       ..4 = User),
                  .f = ~{
                    dplyr::mutate(..1,
                                  strength_rescaled = 
                                    scales::rescale(strength, to = c(0.3, 1.1)),
                                  alpha_strength = ifelse(strength_rescaled == 0.3, 
                                                          0.5, 
                                                          strength_rescaled)) %>% 
                      ggplot(data = .,
                             aes(x = forcats::fct_rev(name), 
                                 y = value, 
                                 size = strength_rescaled,
                                 alpha = alpha_strength,
                                 color = attractors_n)) +
                      scale_size_identity() +
                      scale_alpha_identity() +
                      geom_point(aes(alpha = alpha_strength)) +
                      geom_line(aes(group = time,
                                    alpha = alpha_strength)) +
                      scale_color_manual(values = c(viridisLite::plasma(4, 
                                                                        end = 0.8, 
                                                                        direction = -1), 
                                                    "gray40", "gray50")) +
                      scale_y_continuous(labels = scales::label_percent(accuracy = 1)) +
                      theme_bw() +
                      theme(legend.position = "none") +
                      labs(y = "Percentage of maximum reported value of variable, across full time series",
                           x = NULL,
                           title = #paste0("Participant \"", ..4, "\" - based on ", ..2, " data points (", ..3, " days)")
                                   paste0("Temporal motivation profiles - based on ", ..2, " data points (", ..3, " days)")) +
                      facet_wrap(~attractors_n) +
                      coord_flip(ylim = c(0, 1))
                  }
      ))

emadata_nested_wrangled_both_recnets_nodes_plots$attractor_plots[[1]]

# emadata_nested_wrangled_both_recnets_nodes_plots$all_nodes_with_strengths[[1]]

# Save this to be used for the task analysis later; otherwise overwritten by sensitivity analyses:
firstlast_emadata_nested_wrangled_both_recnets_nodes_plots <- 
  emadata_nested_wrangled_both_recnets_nodes_plots

cowplot::save_plot("./figures/attractors.png", 
                   emadata_nested_wrangled_both_recnets_nodes_plots$attractor_plots[[1]], 
                   dpi = 300,
                   base_height = 11.69/2)

We can observe four main attractors in the plot. The panel labeled 1st shows a relatively balanced profile, with situational requirements slightly elevated. The modal task (see next section) in this profile is internal meetings. The 2nd panel indicates a profile, which is quite high on the pleasure, interest and importance dimensions, with intermediate values on situational requirements and avoiding anxiety or guilt but extrinsic demands are low. This profile, too, is dominated by internal meetings. The 3rd attractor resembles the 1st, but has lower situational requirements and extrinsic demands; the most common task in this profile is email. The 4th attractor shows the theoretically most optimal profile for this person; high on the three autonomous motivation types and low on the controlled motivations. This profile consists of providing training, writing a book, as well as single cases of internal meetings, participating in an event, and reading a report.

A few additional attractors can be seen in the Uncategorised panel. These uncharted, weaker profiles (as measured by their frequency and homogeneity), seem to consist of mostly low profiles on the “another wants” dimension. Two profiles seem to be distinguished; one consisting of high values on pleasure, interest and importance, combined with low values on situational requirements and avoiding anxiety or guilt, whereas another uncategorised profile seems to indicate the opposite of these. Tasks vary widely, the modal ones being internal meetings and email.

The Unique panel depicts profiles that cannot be grouped under one of the other configurations, thus deemed to only occur once. These also consist mostly of internal meetings and email.

Transition networks

attractor_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>% 
  dplyr::mutate(relative_freqs = purrr::map(.x = all_nodes_with_strengths,
                                       .f = ~.x %>% 
                                         tidyr::pivot_wider(names_from = name,
                                                            values_from = value) %>% 
                                         dplyr::transmute(previousone = lag(attractors, n = 1),
                                                          nextone = attractors) %>% 
                                         dplyr::slice(-1) %>%  
                                         dplyr::group_by(previousone, nextone) %>% 
                                         dplyr::summarise(n = n()) %>% 
                                         dplyr::mutate(freq = ((n / sum(n)) * 100) %>% 
                                                         round(., digits = 0)) %>% 
                                         dplyr::select(-n) %>%
                                         data.table::dcast(previousone ~ nextone)))

# absolutes <- emadata_nested_wrangled_both_recnets_nodes_plots$all_nodes_with_strengths[[1]] %>% 
#   tidyr::pivot_wider(names_from = name,
#                      values_from = value) %>% 
#   dplyr::transmute(current = attractors,
#                    previous = lag(attractors, n = 1)) %>% 
#   data.table::dcast(current ~ previous)

attractor_plots <- attractor_plots %>% 
  dplyr::mutate(relative_freqs_plots = purrr::pmap(list(..1 = relative_freqs,
                                                        ..2 = User),
                                                  .f = ~..1 %>% 
                                                    tidyr::pivot_longer(-previousone,
                                                                        values_to = "value",
                                                                        names_to = "nextone") %>% 
                                                    ggplot(aes(x = previousone,
                                                               y = nextone)) +
                                                    geom_tile(aes(fill = value),
                                                              colour = "black",
                                                              size = 0.4) +
                                                    # geom_text(aes(label = ifelse(is.na(value),
                                                    #                              ".00",
                                                    #                              (gsub("0\\.",
                                                    #                                    "\\.",
                                                    #                                    (sprintf("%.2f", value))))))) +
                                                    geom_text(aes(label = ifelse(is.na(value),
                                                                                 "0",
                                                                                 value))) +
                                                    scale_fill_gradient(low = "white", 
                                                                        high = "red", 
                                                                        na.value = "grey",
                                                                        guide = "none") +
                                                    theme_bw() +
                                                    scale_y_discrete(expand = c(0, 0)) +
                                                    scale_x_discrete(expand = c(0, 0)) +
                                                    theme(axis.text.x = element_text(angle = 30, hjust = 1),
                                                          axis.text.y = element_text(angle = 30, hjust = 1),
                                                          legend.position = "right",
                                                          legend.title = element_blank()) +
                                                    coord_equal() +
                                                    labs(x = "Previous state", y = "Next state",
                                                         title = paste0(#..2, ": ",
                                                                        "Transitions between the 6-dimensional states"))))

attractor_plots <- attractor_plots %>% 
  dplyr::mutate(relative_freqs_networks = purrr::pmap(list(..1 = relative_freqs,
                                                        ..2 = User),
                                                  .f = ~..1 %>% 
                                                    tidyr::pivot_longer(-previousone,
                                                                        values_to = "value",
                                                                        names_to = "nextone") %>% 
                                                    qgraph::qgraph(.,
                                                                   #layout = "circle",
                                                                   edgelist = TRUE,
                                                                   directed = TRUE,
                                                                   label.scale = FALSE,
                                                                   trans = TRUE,
                                                                   layout = "spring",
                                                                   # lty = .[["line_type"]],
                                                                   # edge.color = .[["line_colour"]],
                                                                   # edge.width = .[["line_width"]],
                                                                   # node.label.position = 3, # If offset doesn't work
                                                                   # node.label.offset = c(0.5, -2), # x, y
                                                                   # title = .[["User"]],
                                                                   # labels = TRUE,
                                                                   label.cex = 1.25,
                                                                   probabilityEdges = TRUE,
                                                                   edge.labels = TRUE,
                                                                   curveAll = FALSE,
                                                                   # minimum = 1/6,
                                                                   asize = 5,
                                                                   color = c(viridisLite::plasma(4, 
                                                                                                       end = 0.8, 
                                                                                                       direction = -1),
                                                                                   "gray48", "white"),
                                                                   label.color = c("black", "black", "white", "white",
                                                                                   "black", "black"),
                                                                   filetype = "png",
                                                                   filename = paste0("./figures/transition_network_", ..2),
                                                                   mar = c(3, 3, 3, 3) # bottom, left, top, right
                                                    )))

relative_freq_network <- grid::rasterGrob(png::readPNG("./figures/transition_network_Moti_P10.png"), 
                                          interpolate = TRUE)

png(filename = "figures/transition_grid_network.png",
    height = 210*(2/3), width = 297, units = "mm", res = 300)
cowplot::plot_grid(attractor_plots$relative_freqs_plots[[1]],
                                          relative_freq_network,
                                          nrow = 1, labels = c("A)", "B)"))
dev.off()
## png 
##   2

knitr::include_graphics("./figures/transition_grid_network.png") 

Robustness to recurrence rate

Here we perform the same analysis, but change the recurrence rate from 5% upwards to 10%.

5%

Here’s the 6-dimensional motivation system’s recurrence plot, weighted by similarity.

recurrence_rate <- 0.05

set.seed(100)

#######################

# si = similarity under the radius
emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled %>% 
  dplyr::mutate(RN = purrr::map(.x = data_firstlast_divided_by_max,
                                .f =  ~casnet::rn(.x %>% dplyr::select(#autonomy, competence, relatedness,
                                                                       pleasure, interest, importance,
                                                                       situation_requires, 
                                                                       anxiety_guilt_avoidance, 
                                                                       another_wants), 
                                                  doEmbed = FALSE, 
                                                  weighted = TRUE, 
                                                  weightedBy = "si", 
                                                  emRad = NA,
                                                  targetValue = recurrence_rate)))
## 
## Auto-recurrence: Setting diagonal to (1 + max. distance) for analyses
## 
## Searching for a radius that will yield 0.05 for RR

emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(graph_from_adjacency = purrr::map(.x = RN,
                                                  .f = ~igraph::graph_from_adjacency_matrix(.x, 
                                                                                            weighted = TRUE, 
                                                                                            mode = "upper", 
                                                                                            diag = FALSE)))

# Edges with their distances
emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(edges_with_distances = purrr::map(.x = graph_from_adjacency,
                                                  .f = ~igraph::E(.x)$weight),
                graph_from_adjacency_orig = graph_from_adjacency)

# Larger values are closer to the state; inverse of weight makes it more intuitive
for (i in 1:nrow(emadata_nested_wrangled_both_recnets)) {
igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$weight <- (1/emadata_nested_wrangled_both_recnets$edges_with_distances[[i]])
}
# A later note to self: Now weight is a measure of distance; how far apart two time points are
# (under the radius, i.e. they're reasonably similar to begin with)

####### To check:
# igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[1]])$weight
# igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency_orig[[1]])$weight

emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(RN_plot = purrr::map(.x = RN,
                                .f =  ~casnet::rn_plot(.x,
                                                       plotDimensions = TRUE,
                                                       xlab = "6-dimensional motivation system",
                                                       ylab = "6-dimensional motivation system")))


# Make node size equal to strength. Strength is the sum of a node's edge weights.
for (i in 1:nrow(emadata_nested_wrangled_both_recnets)) {
igraph::V(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$size <- (igraph::strength(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]]))
}

# Rescaling weight as "width"; varies between 5 and 10
for (i in 1:nrow(emadata_nested_wrangled_both_recnets)) {
igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$width <- 
  casnet::elascer(igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$weight, lo = 5, hi = 10)
}

The lengthy code chunk below extracts and marks attractors in the data.

# Get number of maximally connected node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(strongest_day = purrr::map(.x = graph_from_adjacency,
                                     .f =  ~which.max(igraph::strength(.x))
                                     ))



emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(list_of_edges = purrr::map(.x = graph_from_adjacency,
                                     .f =  ~igraph::get.data.frame(.x)
                                     ))

emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = 
                  purrr::map2(.x = data_firstlast_divided_by_max,
                              .y = graph_from_adjacency,
                              .f = ~{
                                data.frame(.x, 
                                           strength = igraph::strength(.y)) %>% 
                                  dplyr::mutate(time = dplyr::row_number()) %>% 
                                  tidyr::pivot_longer(cols = c(-strength, -time))
                              }
  ))

# Extract nodes (i.e. times) which connect to the strongest (i.e. most connected) node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = strongest_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))


# Get number of 2nd maximally connected node, which doesn't connect to the 1st
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(secondary_attractor_day = purrr::map2(.x = graph_from_adjacency,
                                                      .y = connecting_to_strongest,
                                                      .f =  ~{
                                                        data.frame(strength = igraph::strength(.x), 
                                                                   time = 1:length(igraph::strength(.x))) %>% 
                                                          dplyr::filter(!time %in% .y) %>% 
                                                          dplyr::arrange(desc(strength)) %>% 
                                                          dplyr::slice(1) %>% 
                                                          dplyr::pull(time)
                                                      }                                                    
  ))

# Extract nodes (i.e. times) which connect to the 2nd strongest node, which doesn't connect to the 1st
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_2nd_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = secondary_attractor_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

# Save as a variable in the dataset
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_2nd_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_2nd_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))

# Get number of 3rd maximally connected node, which doesn't connect to the 1st or second
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(tertiary_attractor_day = purrr::pmap(list(..1 = graph_from_adjacency,
                                                      ..2 = connecting_to_strongest,
                                                      ..3 = connecting_to_2nd_strongest),
                                                      .f =  ~{
                                                        data.frame(strength = igraph::strength(..1), 
                                                                   time = 1:length(igraph::strength(..1))) %>% 
                                                          dplyr::filter(!time %in% ..2, 
                                                                        !time %in% ..3) %>% 
                                                          dplyr::arrange(desc(strength)) %>% 
                                                          dplyr::slice(1) %>% 
                                                          dplyr::pull(time)
                                                      }                                                    
  ))

# Extract nodes (i.e. times) which connect to the 3rd strongest node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_3rd_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = tertiary_attractor_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

# Save as a variable
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_3rd_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_3rd_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))

# Get number of 4th maximally connected node, which doesn't connect to the 1st, 2nd, or 3rd
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(fourth_attractor_day = purrr::pmap(list(..1 = graph_from_adjacency,
                                                      ..2 = connecting_to_strongest,
                                                      ..3 = connecting_to_2nd_strongest,
                                                      ..4 = connecting_to_3rd_strongest),
                                                      .f =  ~{
                                                        data.frame(strength = igraph::strength(..1), 
                                                                   time = 1:length(igraph::strength(..1))) %>% 
                                                          dplyr::filter(!time %in% ..2, 
                                                                        !time %in% ..3, 
                                                                        !time %in% ..4) %>% 
                                                          dplyr::arrange(desc(strength)) %>% 
                                                          dplyr::slice(1) %>% 
                                                          dplyr::pull(time)
                                                      }                                                    
  ))

# Extract nodes (i.e. times) which connect to the 4th strongest node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_4th_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = fourth_attractor_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

# Save as a variable
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_4th_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_4th_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))

################### Make plots

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = 
                  purrr::map(.x = all_nodes_with_strengths,
                             .f = ~{
                               dplyr::mutate(.x,
                                             attractors = dplyr::case_when(
                                               strength == 0 ~ "Unique",
                                               connecting_to_strongest == TRUE ~ "1st",
                                               connecting_to_2nd_strongest == TRUE ~ "2nd",
                                               connecting_to_3rd_strongest == TRUE ~ "3rd",
                                               connecting_to_4th_strongest == TRUE ~ "4th",
                                               TRUE ~ "Uncategorised"),
                                             attractors = factor(attractors,
                                                                 levels = c("1st",
                                                                            "2nd",
                                                                            "3rd",
                                                                            "4th",
                                                                            "Uncategorised",
                                                                            "Unique")),
                                             name = factor(name,
                                                           levels = c("pleasure",
                                                                      "interest",
                                                                      "importance",
                                                                      "situation_requires",
                                                                      "anxiety_guilt_avoidance",
                                                                      "another_wants"),
                                                           labels = c("Pleasure",
                                                                      "Interest",
                                                                      "Importance",
                                                                      "Situation requires",
                                                                      "Anxiety guilt avoidance",
                                                                      "Another wants")) %>% 
                                               forcats::fct_drop()) %>% 
                                 dplyr::group_by(attractors, name) %>% 
                                 dplyr::mutate(n = n()) %>% 
                                 dplyr::ungroup() %>%
                                 dplyr::mutate(maxtime = max(time),
                                               percentage_of_total =
                                                 (n / maxtime) %>% scales::percent(accuracy = 0.1),
                                               proportion_of_total = n/maxtime,
                                               attractors_n = 
                                                 factor(paste0(attractors,
                                                               " (n = ", n, "; ", 
                                                               percentage_of_total, ")")))
                             }
                  ))

Spiral graph with colored nodes

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>%
  dplyr::mutate(node_colors = purrr::map(.x = all_nodes_with_strengths,
                                         .f = ~{tidyr::pivot_wider(.x, names_from = name) %>%
                                             dplyr::pull(attractors)}))

for (i in 1:nrow(emadata_nested_wrangled_both_recnets_nodes_plots)) {
levels(emadata_nested_wrangled_both_recnets_nodes_plots$node_colors[[i]]) <- 
  c(viridisLite::plasma(4, 
                      end = 0.8, 
                      direction = -1), "gray48", "white")
  }

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>% 
  dplyr::mutate(spiralgraph_epochs = purrr::pmap(list(..1 = graph_from_adjacency,
                                          ..2 = node_colors,
                                          ..3 = User),
                                     .f =  ~casnet::make_spiral_graph(g = ..1, 
                                                                      arcs = 4, 
                                                                      # a = .1, 
                                                                      # b = 2, 
                                                                      markTimeBy = TRUE,
                                                                      markEpochsBy = ..2,
                                                                      epochColours = ..2,
                                                                      showEpochLegend = FALSE,
                                                                      scaleEdgeSize = 1/10,
                                                                      scaleVertexSize = c(1, 5),
                                                                      showSizeLegend = FALSE,
                                                                      sizeLabel = "Node strength",
                                                                      type = "Euler",
                                                                      # alphaE = 0.1
                                                                      # title = ..3
                                     )))


# emadata_nested_wrangled_both_recnets_nodes_plots$spiralgraph_epochs[[1]] +
#   theme(plot.margin=grid::unit(c(0,0,0,0), "mm"))

# ggsave(filename = "./figures/recnetwork.png",
#        width = 7, 
#        height = 7)

# emadata_nested_wrangled_both_recnets_nodes_plots$all_nodes_with_strengths[[1]]

Attractor plot

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>%
  dplyr::mutate(observations = purrr::map_dbl(.x = data_firstlast_divided_by_max,
                                              .f = ~nrow(.)))

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>%
  dplyr::mutate(observations_daily = purrr::map_dbl(.x = data_daily,
                                              .f = ~nrow(.)))


emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>% 
  dplyr::mutate(
    attractor_plots = 
      purrr::pmap(list(..1 = all_nodes_with_strengths,
                       ..2 = observations,
                       ..3 = observations_daily,
                       ..4 = User),
                  .f = ~{
                    dplyr::mutate(..1,
                                  strength_rescaled = 
                                    scales::rescale(strength, to = c(0.3, 1.1)),
                                  alpha_strength = ifelse(strength_rescaled == 0.3, 
                                                          0.5, 
                                                          strength_rescaled)) %>% 
                      ggplot(data = .,
                             aes(x = forcats::fct_rev(name), 
                                 y = value, 
                                 size = strength_rescaled,
                                 alpha = alpha_strength,
                                 color = attractors_n)) +
                      scale_size_identity() +
                      scale_alpha_identity() +
                      geom_point(aes(alpha = alpha_strength)) +
                      geom_line(aes(group = time,
                                    alpha = alpha_strength)) +
                      scale_color_manual(values = c(viridisLite::plasma(4, 
                                                                        end = 0.8, 
                                                                        direction = -1), 
                                                    "gray40", "gray50")) +
                      scale_y_continuous(labels = scales::label_percent(accuracy = 1)) +
                      theme_bw() +
                      theme(legend.position = "none") +
                      labs(y = "Percentage of maximum reported value of variable, across full time series",
                           x = NULL,
                           title = paste0("Participant \"", ..4, "\" - based on ", ..2, " data points (", ..3, " days)"),
                           caption = paste0("Recurrence rate used for the analysis: ", scales::percent(recurrence_rate))) +
                      facet_wrap(~attractors_n) +
                      coord_flip(ylim = c(0, 1))
                  }
      ))

emadata_nested_wrangled_both_recnets_nodes_plots$attractor_plots[[1]]

# emadata_nested_wrangled_both_recnets_nodes_plots$all_nodes_with_strengths[[1]]

# # Save this to be used for the task analysis later; otherwise overwritten by sensitivity analyses:
# firstlast_emadata_nested_wrangled_both_recnets_nodes_plots <- 
#   emadata_nested_wrangled_both_recnets_nodes_plots

# cowplot::save_plot("./figures/attractors.png", 
#                    emadata_nested_wrangled_both_recnets_nodes_plots$attractor_plots[[1]], 
#                    dpi = 300,
#                    base_height = 11.69/2)

6%

Here’s the 6-dimensional motivation system’s recurrence plot, weighted by similarity.

recurrence_rate <- 0.06

set.seed(100)

#######################

# si = similarity under the radius
emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled %>% 
  dplyr::mutate(RN = purrr::map(.x = data_firstlast_divided_by_max,
                                .f =  ~casnet::rn(.x %>% dplyr::select(#autonomy, competence, relatedness,
                                                                       pleasure, interest, importance,
                                                                       situation_requires, 
                                                                       anxiety_guilt_avoidance, 
                                                                       another_wants), 
                                                  doEmbed = FALSE, 
                                                  weighted = TRUE, 
                                                  weightedBy = "si", 
                                                  emRad = NA,
                                                  targetValue = recurrence_rate)))
## 
## Auto-recurrence: Setting diagonal to (1 + max. distance) for analyses
## 
## Searching for a radius that will yield 0.06 for RR

emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(graph_from_adjacency = purrr::map(.x = RN,
                                                  .f = ~igraph::graph_from_adjacency_matrix(.x, 
                                                                                            weighted = TRUE, 
                                                                                            mode = "upper", 
                                                                                            diag = FALSE)))

# Edges with their distances
emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(edges_with_distances = purrr::map(.x = graph_from_adjacency,
                                                  .f = ~igraph::E(.x)$weight),
                graph_from_adjacency_orig = graph_from_adjacency)

# Larger values are closer to the state; inverse of weight makes it more intuitive
for (i in 1:nrow(emadata_nested_wrangled_both_recnets)) {
igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$weight <- (1/emadata_nested_wrangled_both_recnets$edges_with_distances[[i]])
}
# A later note to self: Now weight is a measure of distance; how far apart two time points are
# (under the radius, i.e. they're reasonably similar to begin with)

####### To check:
# igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[1]])$weight
# igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency_orig[[1]])$weight

emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(RN_plot = purrr::map(.x = RN,
                                .f =  ~casnet::rn_plot(.x,
                                                       plotDimensions = TRUE,
                                                       xlab = "6-dimensional motivation system",
                                                       ylab = "6-dimensional motivation system")))


# Make node size equal to strength. Strength is the sum of a node's edge weights.
for (i in 1:nrow(emadata_nested_wrangled_both_recnets)) {
igraph::V(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$size <- (igraph::strength(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]]))
}

# Rescaling weight as "width"; varies between 5 and 10
for (i in 1:nrow(emadata_nested_wrangled_both_recnets)) {
igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$width <- 
  casnet::elascer(igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$weight, lo = 5, hi = 10)
}

The lengthy code chunk below extracts and marks attractors in the data.

# Get number of maximally connected node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(strongest_day = purrr::map(.x = graph_from_adjacency,
                                     .f =  ~which.max(igraph::strength(.x))
                                     ))



emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(list_of_edges = purrr::map(.x = graph_from_adjacency,
                                     .f =  ~igraph::get.data.frame(.x)
                                     ))

emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = 
                  purrr::map2(.x = data_firstlast_divided_by_max,
                              .y = graph_from_adjacency,
                              .f = ~{
                                data.frame(.x, 
                                           strength = igraph::strength(.y)) %>% 
                                  dplyr::mutate(time = dplyr::row_number()) %>% 
                                  tidyr::pivot_longer(cols = c(-strength, -time))
                              }
  ))

# Extract nodes (i.e. times) which connect to the strongest (i.e. most connected) node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = strongest_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))


# Get number of 2nd maximally connected node, which doesn't connect to the 1st
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(secondary_attractor_day = purrr::map2(.x = graph_from_adjacency,
                                                      .y = connecting_to_strongest,
                                                      .f =  ~{
                                                        data.frame(strength = igraph::strength(.x), 
                                                                   time = 1:length(igraph::strength(.x))) %>% 
                                                          dplyr::filter(!time %in% .y) %>% 
                                                          dplyr::arrange(desc(strength)) %>% 
                                                          dplyr::slice(1) %>% 
                                                          dplyr::pull(time)
                                                      }                                                    
  ))

# Extract nodes (i.e. times) which connect to the 2nd strongest node, which doesn't connect to the 1st
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_2nd_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = secondary_attractor_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

# Save as a variable in the dataset
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_2nd_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_2nd_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))

# Get number of 3rd maximally connected node, which doesn't connect to the 1st or second
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(tertiary_attractor_day = purrr::pmap(list(..1 = graph_from_adjacency,
                                                      ..2 = connecting_to_strongest,
                                                      ..3 = connecting_to_2nd_strongest),
                                                      .f =  ~{
                                                        data.frame(strength = igraph::strength(..1), 
                                                                   time = 1:length(igraph::strength(..1))) %>% 
                                                          dplyr::filter(!time %in% ..2, 
                                                                        !time %in% ..3) %>% 
                                                          dplyr::arrange(desc(strength)) %>% 
                                                          dplyr::slice(1) %>% 
                                                          dplyr::pull(time)
                                                      }                                                    
  ))

# Extract nodes (i.e. times) which connect to the 3rd strongest node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_3rd_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = tertiary_attractor_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

# Save as a variable
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_3rd_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_3rd_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))

# Get number of 4th maximally connected node, which doesn't connect to the 1st, 2nd, or 3rd
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(fourth_attractor_day = purrr::pmap(list(..1 = graph_from_adjacency,
                                                      ..2 = connecting_to_strongest,
                                                      ..3 = connecting_to_2nd_strongest,
                                                      ..4 = connecting_to_3rd_strongest),
                                                      .f =  ~{
                                                        data.frame(strength = igraph::strength(..1), 
                                                                   time = 1:length(igraph::strength(..1))) %>% 
                                                          dplyr::filter(!time %in% ..2, 
                                                                        !time %in% ..3, 
                                                                        !time %in% ..4) %>% 
                                                          dplyr::arrange(desc(strength)) %>% 
                                                          dplyr::slice(1) %>% 
                                                          dplyr::pull(time)
                                                      }                                                    
  ))

# Extract nodes (i.e. times) which connect to the 4th strongest node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_4th_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = fourth_attractor_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

# Save as a variable
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_4th_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_4th_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))

################### Make plots

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = 
                  purrr::map(.x = all_nodes_with_strengths,
                             .f = ~{
                               dplyr::mutate(.x,
                                             attractors = dplyr::case_when(
                                               strength == 0 ~ "Unique",
                                               connecting_to_strongest == TRUE ~ "1st",
                                               connecting_to_2nd_strongest == TRUE ~ "2nd",
                                               connecting_to_3rd_strongest == TRUE ~ "3rd",
                                               connecting_to_4th_strongest == TRUE ~ "4th",
                                               TRUE ~ "Uncategorised"),
                                             attractors = factor(attractors,
                                                                 levels = c("1st",
                                                                            "2nd",
                                                                            "3rd",
                                                                            "4th",
                                                                            "Uncategorised",
                                                                            "Unique")),
                                             name = factor(name,
                                                           levels = c("pleasure",
                                                                      "interest",
                                                                      "importance",
                                                                      "situation_requires",
                                                                      "anxiety_guilt_avoidance",
                                                                      "another_wants"),
                                                           labels = c("Pleasure",
                                                                      "Interest",
                                                                      "Importance",
                                                                      "Situation requires",
                                                                      "Anxiety guilt avoidance",
                                                                      "Another wants")) %>% 
                                               forcats::fct_drop()) %>% 
                                 dplyr::group_by(attractors, name) %>% 
                                 dplyr::mutate(n = n()) %>% 
                                 dplyr::ungroup() %>%
                                 dplyr::mutate(maxtime = max(time),
                                               percentage_of_total =
                                                 (n / maxtime) %>% scales::percent(accuracy = 0.1),
                                               proportion_of_total = n/maxtime,
                                               attractors_n = 
                                                 factor(paste0(attractors,
                                                               " (n = ", n, "; ", 
                                                               percentage_of_total, ")")))
                             }
                  ))

Spiral graph with colored nodes

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>%
  dplyr::mutate(node_colors = purrr::map(.x = all_nodes_with_strengths,
                                         .f = ~{tidyr::pivot_wider(.x, names_from = name) %>%
                                             dplyr::pull(attractors)}))

for (i in 1:nrow(emadata_nested_wrangled_both_recnets_nodes_plots)) {
levels(emadata_nested_wrangled_both_recnets_nodes_plots$node_colors[[i]]) <- 
  c(viridisLite::plasma(4, 
                      end = 0.8, 
                      direction = -1), "gray48", "white")
  }

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>% 
  dplyr::mutate(spiralgraph_epochs = purrr::pmap(list(..1 = graph_from_adjacency,
                                          ..2 = node_colors,
                                          ..3 = User),
                                     .f =  ~casnet::make_spiral_graph(g = ..1, 
                                                                      arcs = 4, 
                                                                      # a = .1, 
                                                                      # b = 2, 
                                                                      markTimeBy = TRUE,
                                                                      markEpochsBy = ..2,
                                                                      epochColours = ..2,
                                                                      showEpochLegend = FALSE,
                                                                      scaleEdgeSize = 1/10,
                                                                      scaleVertexSize = c(1, 5),
                                                                      showSizeLegend = FALSE,
                                                                      sizeLabel = "Node strength",
                                                                      type = "Euler",
                                                                      # alphaE = 0.1
                                                                      # title = ..3
                                     )))


# emadata_nested_wrangled_both_recnets_nodes_plots$spiralgraph_epochs[[1]] +
#   theme(plot.margin=grid::unit(c(0,0,0,0), "mm"))

# ggsave(filename = "./figures/recnetwork.png",
#        width = 7, 
#        height = 7)

# emadata_nested_wrangled_both_recnets_nodes_plots$all_nodes_with_strengths[[1]]

Attractor plot

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>%
  dplyr::mutate(observations = purrr::map_dbl(.x = data_firstlast_divided_by_max,
                                              .f = ~nrow(.)))

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>%
  dplyr::mutate(observations_daily = purrr::map_dbl(.x = data_daily,
                                              .f = ~nrow(.)))


emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>% 
  dplyr::mutate(
    attractor_plots = 
      purrr::pmap(list(..1 = all_nodes_with_strengths,
                       ..2 = observations,
                       ..3 = observations_daily,
                       ..4 = User),
                  .f = ~{
                    dplyr::mutate(..1,
                                  strength_rescaled = 
                                    scales::rescale(strength, to = c(0.3, 1.1)),
                                  alpha_strength = ifelse(strength_rescaled == 0.3, 
                                                          0.5, 
                                                          strength_rescaled)) %>% 
                      ggplot(data = .,
                             aes(x = forcats::fct_rev(name), 
                                 y = value, 
                                 size = strength_rescaled,
                                 alpha = alpha_strength,
                                 color = attractors_n)) +
                      scale_size_identity() +
                      scale_alpha_identity() +
                      geom_point(aes(alpha = alpha_strength)) +
                      geom_line(aes(group = time,
                                    alpha = alpha_strength)) +
                      scale_color_manual(values = c(viridisLite::plasma(4, 
                                                                        end = 0.8, 
                                                                        direction = -1), 
                                                    "gray40", "gray50")) +
                      scale_y_continuous(labels = scales::label_percent(accuracy = 1)) +
                      theme_bw() +
                      theme(legend.position = "none") +
                      labs(y = "Percentage of maximum reported value of variable, across full time series",
                           x = NULL,
                           title = paste0("Participant \"", ..4, "\" - based on ", ..2, " data points (", ..3, " days)"),
                           caption = paste0("Recurrence rate used for the analysis: ", scales::percent(recurrence_rate))) +
                      facet_wrap(~attractors_n) +
                      coord_flip(ylim = c(0, 1))
                  }
      ))

emadata_nested_wrangled_both_recnets_nodes_plots$attractor_plots[[1]]

# emadata_nested_wrangled_both_recnets_nodes_plots$all_nodes_with_strengths[[1]]

# # Save this to be used for the task analysis later; otherwise overwritten by sensitivity analyses:
# firstlast_emadata_nested_wrangled_both_recnets_nodes_plots <- 
#   emadata_nested_wrangled_both_recnets_nodes_plots

# cowplot::save_plot("./figures/attractors.png", 
#                    emadata_nested_wrangled_both_recnets_nodes_plots$attractor_plots[[1]], 
#                    dpi = 300,
#                    base_height = 11.69/2)

7%

Here’s the 6-dimensional motivation system’s recurrence plot, weighted by similarity.

recurrence_rate <- 0.07

set.seed(100)

#######################

# si = similarity under the radius
emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled %>% 
  dplyr::mutate(RN = purrr::map(.x = data_firstlast_divided_by_max,
                                .f =  ~casnet::rn(.x %>% dplyr::select(#autonomy, competence, relatedness,
                                                                       pleasure, interest, importance,
                                                                       situation_requires, 
                                                                       anxiety_guilt_avoidance, 
                                                                       another_wants), 
                                                  doEmbed = FALSE, 
                                                  weighted = TRUE, 
                                                  weightedBy = "si", 
                                                  emRad = NA,
                                                  targetValue = recurrence_rate)))
## 
## Auto-recurrence: Setting diagonal to (1 + max. distance) for analyses
## 
## Searching for a radius that will yield 0.07 for RR

emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(graph_from_adjacency = purrr::map(.x = RN,
                                                  .f = ~igraph::graph_from_adjacency_matrix(.x, 
                                                                                            weighted = TRUE, 
                                                                                            mode = "upper", 
                                                                                            diag = FALSE)))

# Edges with their distances
emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(edges_with_distances = purrr::map(.x = graph_from_adjacency,
                                                  .f = ~igraph::E(.x)$weight),
                graph_from_adjacency_orig = graph_from_adjacency)

# Larger values are closer to the state; inverse of weight makes it more intuitive
for (i in 1:nrow(emadata_nested_wrangled_both_recnets)) {
igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$weight <- (1/emadata_nested_wrangled_both_recnets$edges_with_distances[[i]])
}
# A later note to self: Now weight is a measure of distance; how far apart two time points are
# (under the radius, i.e. they're reasonably similar to begin with)

####### To check:
# igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[1]])$weight
# igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency_orig[[1]])$weight

emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(RN_plot = purrr::map(.x = RN,
                                .f =  ~casnet::rn_plot(.x,
                                                       plotDimensions = TRUE,
                                                       xlab = "6-dimensional motivation system",
                                                       ylab = "6-dimensional motivation system")))


# Make node size equal to strength. Strength is the sum of a node's edge weights.
for (i in 1:nrow(emadata_nested_wrangled_both_recnets)) {
igraph::V(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$size <- (igraph::strength(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]]))
}

# Rescaling weight as "width"; varies between 5 and 10
for (i in 1:nrow(emadata_nested_wrangled_both_recnets)) {
igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$width <- 
  casnet::elascer(igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$weight, lo = 5, hi = 10)
}

The lengthy code chunk below extracts and marks attractors in the data.

# Get number of maximally connected node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(strongest_day = purrr::map(.x = graph_from_adjacency,
                                     .f =  ~which.max(igraph::strength(.x))
                                     ))



emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(list_of_edges = purrr::map(.x = graph_from_adjacency,
                                     .f =  ~igraph::get.data.frame(.x)
                                     ))

emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = 
                  purrr::map2(.x = data_firstlast_divided_by_max,
                              .y = graph_from_adjacency,
                              .f = ~{
                                data.frame(.x, 
                                           strength = igraph::strength(.y)) %>% 
                                  dplyr::mutate(time = dplyr::row_number()) %>% 
                                  tidyr::pivot_longer(cols = c(-strength, -time))
                              }
  ))

# Extract nodes (i.e. times) which connect to the strongest (i.e. most connected) node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = strongest_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))


# Get number of 2nd maximally connected node, which doesn't connect to the 1st
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(secondary_attractor_day = purrr::map2(.x = graph_from_adjacency,
                                                      .y = connecting_to_strongest,
                                                      .f =  ~{
                                                        data.frame(strength = igraph::strength(.x), 
                                                                   time = 1:length(igraph::strength(.x))) %>% 
                                                          dplyr::filter(!time %in% .y) %>% 
                                                          dplyr::arrange(desc(strength)) %>% 
                                                          dplyr::slice(1) %>% 
                                                          dplyr::pull(time)
                                                      }                                                    
  ))

# Extract nodes (i.e. times) which connect to the 2nd strongest node, which doesn't connect to the 1st
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_2nd_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = secondary_attractor_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

# Save as a variable in the dataset
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_2nd_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_2nd_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))

# Get number of 3rd maximally connected node, which doesn't connect to the 1st or second
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(tertiary_attractor_day = purrr::pmap(list(..1 = graph_from_adjacency,
                                                      ..2 = connecting_to_strongest,
                                                      ..3 = connecting_to_2nd_strongest),
                                                      .f =  ~{
                                                        data.frame(strength = igraph::strength(..1), 
                                                                   time = 1:length(igraph::strength(..1))) %>% 
                                                          dplyr::filter(!time %in% ..2, 
                                                                        !time %in% ..3) %>% 
                                                          dplyr::arrange(desc(strength)) %>% 
                                                          dplyr::slice(1) %>% 
                                                          dplyr::pull(time)
                                                      }                                                    
  ))

# Extract nodes (i.e. times) which connect to the 3rd strongest node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_3rd_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = tertiary_attractor_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

# Save as a variable
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_3rd_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_3rd_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))

# Get number of 4th maximally connected node, which doesn't connect to the 1st, 2nd, or 3rd
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(fourth_attractor_day = purrr::pmap(list(..1 = graph_from_adjacency,
                                                      ..2 = connecting_to_strongest,
                                                      ..3 = connecting_to_2nd_strongest,
                                                      ..4 = connecting_to_3rd_strongest),
                                                      .f =  ~{
                                                        data.frame(strength = igraph::strength(..1), 
                                                                   time = 1:length(igraph::strength(..1))) %>% 
                                                          dplyr::filter(!time %in% ..2, 
                                                                        !time %in% ..3, 
                                                                        !time %in% ..4) %>% 
                                                          dplyr::arrange(desc(strength)) %>% 
                                                          dplyr::slice(1) %>% 
                                                          dplyr::pull(time)
                                                      }                                                    
  ))

# Extract nodes (i.e. times) which connect to the 4th strongest node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_4th_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = fourth_attractor_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

# Save as a variable
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_4th_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_4th_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))

################### Make plots

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = 
                  purrr::map(.x = all_nodes_with_strengths,
                             .f = ~{
                               dplyr::mutate(.x,
                                             attractors = dplyr::case_when(
                                               strength == 0 ~ "Unique",
                                               connecting_to_strongest == TRUE ~ "1st",
                                               connecting_to_2nd_strongest == TRUE ~ "2nd",
                                               connecting_to_3rd_strongest == TRUE ~ "3rd",
                                               connecting_to_4th_strongest == TRUE ~ "4th",
                                               TRUE ~ "Uncategorised"),
                                             attractors = factor(attractors,
                                                                 levels = c("1st",
                                                                            "2nd",
                                                                            "3rd",
                                                                            "4th",
                                                                            "Uncategorised",
                                                                            "Unique")),
                                             name = factor(name,
                                                           levels = c("pleasure",
                                                                      "interest",
                                                                      "importance",
                                                                      "situation_requires",
                                                                      "anxiety_guilt_avoidance",
                                                                      "another_wants"),
                                                           labels = c("Pleasure",
                                                                      "Interest",
                                                                      "Importance",
                                                                      "Situation requires",
                                                                      "Anxiety guilt avoidance",
                                                                      "Another wants")) %>% 
                                               forcats::fct_drop()) %>% 
                                 dplyr::group_by(attractors, name) %>% 
                                 dplyr::mutate(n = n()) %>% 
                                 dplyr::ungroup() %>%
                                 dplyr::mutate(maxtime = max(time),
                                               percentage_of_total =
                                                 (n / maxtime) %>% scales::percent(accuracy = 0.1),
                                               proportion_of_total = n/maxtime,
                                               attractors_n = 
                                                 factor(paste0(attractors,
                                                               " (n = ", n, "; ", 
                                                               percentage_of_total, ")")))
                             }
                  ))

Spiral graph with colored nodes

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>%
  dplyr::mutate(node_colors = purrr::map(.x = all_nodes_with_strengths,
                                         .f = ~{tidyr::pivot_wider(.x, names_from = name) %>%
                                             dplyr::pull(attractors)}))

for (i in 1:nrow(emadata_nested_wrangled_both_recnets_nodes_plots)) {
levels(emadata_nested_wrangled_both_recnets_nodes_plots$node_colors[[i]]) <- 
  c(viridisLite::plasma(4, 
                      end = 0.8, 
                      direction = -1), "gray48", "white")
  }

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>% 
  dplyr::mutate(spiralgraph_epochs = purrr::pmap(list(..1 = graph_from_adjacency,
                                          ..2 = node_colors,
                                          ..3 = User),
                                     .f =  ~casnet::make_spiral_graph(g = ..1, 
                                                                      arcs = 4, 
                                                                      # a = .1, 
                                                                      # b = 2, 
                                                                      markTimeBy = TRUE,
                                                                      markEpochsBy = ..2,
                                                                      epochColours = ..2,
                                                                      showEpochLegend = FALSE,
                                                                      scaleEdgeSize = 1/10,
                                                                      scaleVertexSize = c(1, 5),
                                                                      showSizeLegend = FALSE,
                                                                      sizeLabel = "Node strength",
                                                                      type = "Euler",
                                                                      # alphaE = 0.1
                                                                      # title = ..3
                                     )))


# emadata_nested_wrangled_both_recnets_nodes_plots$spiralgraph_epochs[[1]] +
#   theme(plot.margin=grid::unit(c(0,0,0,0), "mm"))

# ggsave(filename = "./figures/recnetwork.png",
#        width = 7, 
#        height = 7)

# emadata_nested_wrangled_both_recnets_nodes_plots$all_nodes_with_strengths[[1]]

Attractor plot

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>%
  dplyr::mutate(observations = purrr::map_dbl(.x = data_firstlast_divided_by_max,
                                              .f = ~nrow(.)))

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>%
  dplyr::mutate(observations_daily = purrr::map_dbl(.x = data_daily,
                                              .f = ~nrow(.)))


emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>% 
  dplyr::mutate(
    attractor_plots = 
      purrr::pmap(list(..1 = all_nodes_with_strengths,
                       ..2 = observations,
                       ..3 = observations_daily,
                       ..4 = User),
                  .f = ~{
                    dplyr::mutate(..1,
                                  strength_rescaled = 
                                    scales::rescale(strength, to = c(0.3, 1.1)),
                                  alpha_strength = ifelse(strength_rescaled == 0.3, 
                                                          0.5, 
                                                          strength_rescaled)) %>% 
                      ggplot(data = .,
                             aes(x = forcats::fct_rev(name), 
                                 y = value, 
                                 size = strength_rescaled,
                                 alpha = alpha_strength,
                                 color = attractors_n)) +
                      scale_size_identity() +
                      scale_alpha_identity() +
                      geom_point(aes(alpha = alpha_strength)) +
                      geom_line(aes(group = time,
                                    alpha = alpha_strength)) +
                      scale_color_manual(values = c(viridisLite::plasma(4, 
                                                                        end = 0.8, 
                                                                        direction = -1), 
                                                    "gray40", "gray50")) +
                      scale_y_continuous(labels = scales::label_percent(accuracy = 1)) +
                      theme_bw() +
                      theme(legend.position = "none") +
                      labs(y = "Percentage of maximum reported value of variable, across full time series",
                           x = NULL,
                           title = paste0("Participant \"", ..4, "\" - based on ", ..2, " data points (", ..3, " days)"),
                           caption = paste0("Recurrence rate used for the analysis: ", scales::percent(recurrence_rate))) +
                      facet_wrap(~attractors_n) +
                      coord_flip(ylim = c(0, 1))
                  }
      ))

emadata_nested_wrangled_both_recnets_nodes_plots$attractor_plots[[1]]

# emadata_nested_wrangled_both_recnets_nodes_plots$all_nodes_with_strengths[[1]]

# # Save this to be used for the task analysis later; otherwise overwritten by sensitivity analyses:
# firstlast_emadata_nested_wrangled_both_recnets_nodes_plots <- 
#   emadata_nested_wrangled_both_recnets_nodes_plots

# cowplot::save_plot("./figures/attractors.png", 
#                    emadata_nested_wrangled_both_recnets_nodes_plots$attractor_plots[[1]], 
#                    dpi = 300,
#                    base_height = 11.69/2)

8%

Here’s the 6-dimensional motivation system’s recurrence plot, weighted by similarity.

recurrence_rate <- 0.08

set.seed(100)

#######################

# si = similarity under the radius
emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled %>% 
  dplyr::mutate(RN = purrr::map(.x = data_firstlast_divided_by_max,
                                .f =  ~casnet::rn(.x %>% dplyr::select(#autonomy, competence, relatedness,
                                                                       pleasure, interest, importance,
                                                                       situation_requires, 
                                                                       anxiety_guilt_avoidance, 
                                                                       another_wants), 
                                                  doEmbed = FALSE, 
                                                  weighted = TRUE, 
                                                  weightedBy = "si", 
                                                  emRad = NA,
                                                  targetValue = recurrence_rate)))
## 
## Auto-recurrence: Setting diagonal to (1 + max. distance) for analyses
## 
## Searching for a radius that will yield 0.08 for RR

emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(graph_from_adjacency = purrr::map(.x = RN,
                                                  .f = ~igraph::graph_from_adjacency_matrix(.x, 
                                                                                            weighted = TRUE, 
                                                                                            mode = "upper", 
                                                                                            diag = FALSE)))

# Edges with their distances
emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(edges_with_distances = purrr::map(.x = graph_from_adjacency,
                                                  .f = ~igraph::E(.x)$weight),
                graph_from_adjacency_orig = graph_from_adjacency)

# Larger values are closer to the state; inverse of weight makes it more intuitive
for (i in 1:nrow(emadata_nested_wrangled_both_recnets)) {
igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$weight <- (1/emadata_nested_wrangled_both_recnets$edges_with_distances[[i]])
}
# A later note to self: Now weight is a measure of distance; how far apart two time points are
# (under the radius, i.e. they're reasonably similar to begin with)

####### To check:
# igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[1]])$weight
# igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency_orig[[1]])$weight

emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(RN_plot = purrr::map(.x = RN,
                                .f =  ~casnet::rn_plot(.x,
                                                       plotDimensions = TRUE,
                                                       xlab = "6-dimensional motivation system",
                                                       ylab = "6-dimensional motivation system")))


# Make node size equal to strength. Strength is the sum of a node's edge weights.
for (i in 1:nrow(emadata_nested_wrangled_both_recnets)) {
igraph::V(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$size <- (igraph::strength(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]]))
}

# Rescaling weight as "width"; varies between 5 and 10
for (i in 1:nrow(emadata_nested_wrangled_both_recnets)) {
igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$width <- 
  casnet::elascer(igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$weight, lo = 5, hi = 10)
}

The lengthy code chunk below extracts and marks attractors in the data.

# Get number of maximally connected node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(strongest_day = purrr::map(.x = graph_from_adjacency,
                                     .f =  ~which.max(igraph::strength(.x))
                                     ))



emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(list_of_edges = purrr::map(.x = graph_from_adjacency,
                                     .f =  ~igraph::get.data.frame(.x)
                                     ))

emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = 
                  purrr::map2(.x = data_firstlast_divided_by_max,
                              .y = graph_from_adjacency,
                              .f = ~{
                                data.frame(.x, 
                                           strength = igraph::strength(.y)) %>% 
                                  dplyr::mutate(time = dplyr::row_number()) %>% 
                                  tidyr::pivot_longer(cols = c(-strength, -time))
                              }
  ))

# Extract nodes (i.e. times) which connect to the strongest (i.e. most connected) node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = strongest_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))


# Get number of 2nd maximally connected node, which doesn't connect to the 1st
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(secondary_attractor_day = purrr::map2(.x = graph_from_adjacency,
                                                      .y = connecting_to_strongest,
                                                      .f =  ~{
                                                        data.frame(strength = igraph::strength(.x), 
                                                                   time = 1:length(igraph::strength(.x))) %>% 
                                                          dplyr::filter(!time %in% .y) %>% 
                                                          dplyr::arrange(desc(strength)) %>% 
                                                          dplyr::slice(1) %>% 
                                                          dplyr::pull(time)
                                                      }                                                    
  ))

# Extract nodes (i.e. times) which connect to the 2nd strongest node, which doesn't connect to the 1st
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_2nd_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = secondary_attractor_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

# Save as a variable in the dataset
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_2nd_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_2nd_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))

# Get number of 3rd maximally connected node, which doesn't connect to the 1st or second
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(tertiary_attractor_day = purrr::pmap(list(..1 = graph_from_adjacency,
                                                      ..2 = connecting_to_strongest,
                                                      ..3 = connecting_to_2nd_strongest),
                                                      .f =  ~{
                                                        data.frame(strength = igraph::strength(..1), 
                                                                   time = 1:length(igraph::strength(..1))) %>% 
                                                          dplyr::filter(!time %in% ..2, 
                                                                        !time %in% ..3) %>% 
                                                          dplyr::arrange(desc(strength)) %>% 
                                                          dplyr::slice(1) %>% 
                                                          dplyr::pull(time)
                                                      }                                                    
  ))

# Extract nodes (i.e. times) which connect to the 3rd strongest node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_3rd_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = tertiary_attractor_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

# Save as a variable
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_3rd_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_3rd_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))

# Get number of 4th maximally connected node, which doesn't connect to the 1st, 2nd, or 3rd
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(fourth_attractor_day = purrr::pmap(list(..1 = graph_from_adjacency,
                                                      ..2 = connecting_to_strongest,
                                                      ..3 = connecting_to_2nd_strongest,
                                                      ..4 = connecting_to_3rd_strongest),
                                                      .f =  ~{
                                                        data.frame(strength = igraph::strength(..1), 
                                                                   time = 1:length(igraph::strength(..1))) %>% 
                                                          dplyr::filter(!time %in% ..2, 
                                                                        !time %in% ..3, 
                                                                        !time %in% ..4) %>% 
                                                          dplyr::arrange(desc(strength)) %>% 
                                                          dplyr::slice(1) %>% 
                                                          dplyr::pull(time)
                                                      }                                                    
  ))

# Extract nodes (i.e. times) which connect to the 4th strongest node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_4th_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = fourth_attractor_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

# Save as a variable
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_4th_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_4th_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))

################### Make plots

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = 
                  purrr::map(.x = all_nodes_with_strengths,
                             .f = ~{
                               dplyr::mutate(.x,
                                             attractors = dplyr::case_when(
                                               strength == 0 ~ "Unique",
                                               connecting_to_strongest == TRUE ~ "1st",
                                               connecting_to_2nd_strongest == TRUE ~ "2nd",
                                               connecting_to_3rd_strongest == TRUE ~ "3rd",
                                               connecting_to_4th_strongest == TRUE ~ "4th",
                                               TRUE ~ "Uncategorised"),
                                             attractors = factor(attractors,
                                                                 levels = c("1st",
                                                                            "2nd",
                                                                            "3rd",
                                                                            "4th",
                                                                            "Uncategorised",
                                                                            "Unique")),
                                             name = factor(name,
                                                           levels = c("pleasure",
                                                                      "interest",
                                                                      "importance",
                                                                      "situation_requires",
                                                                      "anxiety_guilt_avoidance",
                                                                      "another_wants"),
                                                           labels = c("Pleasure",
                                                                      "Interest",
                                                                      "Importance",
                                                                      "Situation requires",
                                                                      "Anxiety guilt avoidance",
                                                                      "Another wants")) %>% 
                                               forcats::fct_drop()) %>% 
                                 dplyr::group_by(attractors, name) %>% 
                                 dplyr::mutate(n = n()) %>% 
                                 dplyr::ungroup() %>%
                                 dplyr::mutate(maxtime = max(time),
                                               percentage_of_total =
                                                 (n / maxtime) %>% scales::percent(accuracy = 0.1),
                                               proportion_of_total = n/maxtime,
                                               attractors_n = 
                                                 factor(paste0(attractors,
                                                               " (n = ", n, "; ", 
                                                               percentage_of_total, ")")))
                             }
                  ))

Spiral graph with colored nodes

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>%
  dplyr::mutate(node_colors = purrr::map(.x = all_nodes_with_strengths,
                                         .f = ~{tidyr::pivot_wider(.x, names_from = name) %>%
                                             dplyr::pull(attractors)}))

for (i in 1:nrow(emadata_nested_wrangled_both_recnets_nodes_plots)) {
levels(emadata_nested_wrangled_both_recnets_nodes_plots$node_colors[[i]]) <- 
  c(viridisLite::plasma(4, 
                      end = 0.8, 
                      direction = -1), "gray48", "white")
  }

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>% 
  dplyr::mutate(spiralgraph_epochs = purrr::pmap(list(..1 = graph_from_adjacency,
                                          ..2 = node_colors,
                                          ..3 = User),
                                     .f =  ~casnet::make_spiral_graph(g = ..1, 
                                                                      arcs = 4, 
                                                                      # a = .1, 
                                                                      # b = 2, 
                                                                      markTimeBy = TRUE,
                                                                      markEpochsBy = ..2,
                                                                      epochColours = ..2,
                                                                      showEpochLegend = FALSE,
                                                                      scaleEdgeSize = 1/10,
                                                                      scaleVertexSize = c(1, 5),
                                                                      showSizeLegend = FALSE,
                                                                      sizeLabel = "Node strength",
                                                                      type = "Euler",
                                                                      # alphaE = 0.1
                                                                      # title = ..3
                                     )))


# emadata_nested_wrangled_both_recnets_nodes_plots$spiralgraph_epochs[[1]] +
#   theme(plot.margin=grid::unit(c(0,0,0,0), "mm"))

# ggsave(filename = "./figures/recnetwork.png",
#        width = 7, 
#        height = 7)

# emadata_nested_wrangled_both_recnets_nodes_plots$all_nodes_with_strengths[[1]]

Attractor plot

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>%
  dplyr::mutate(observations = purrr::map_dbl(.x = data_firstlast_divided_by_max,
                                              .f = ~nrow(.)))

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>%
  dplyr::mutate(observations_daily = purrr::map_dbl(.x = data_daily,
                                              .f = ~nrow(.)))


emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>% 
  dplyr::mutate(
    attractor_plots = 
      purrr::pmap(list(..1 = all_nodes_with_strengths,
                       ..2 = observations,
                       ..3 = observations_daily,
                       ..4 = User),
                  .f = ~{
                    dplyr::mutate(..1,
                                  strength_rescaled = 
                                    scales::rescale(strength, to = c(0.3, 1.1)),
                                  alpha_strength = ifelse(strength_rescaled == 0.3, 
                                                          0.5, 
                                                          strength_rescaled)) %>% 
                      ggplot(data = .,
                             aes(x = forcats::fct_rev(name), 
                                 y = value, 
                                 size = strength_rescaled,
                                 alpha = alpha_strength,
                                 color = attractors_n)) +
                      scale_size_identity() +
                      scale_alpha_identity() +
                      geom_point(aes(alpha = alpha_strength)) +
                      geom_line(aes(group = time,
                                    alpha = alpha_strength)) +
                      scale_color_manual(values = c(viridisLite::plasma(4, 
                                                                        end = 0.8, 
                                                                        direction = -1), 
                                                    "gray40", "gray50")) +
                      scale_y_continuous(labels = scales::label_percent(accuracy = 1)) +
                      theme_bw() +
                      theme(legend.position = "none") +
                      labs(y = "Percentage of maximum reported value of variable, across full time series",
                           x = NULL,
                           title = paste0("Participant \"", ..4, "\" - based on ", ..2, " data points (", ..3, " days)"),
                           caption = paste0("Recurrence rate used for the analysis: ", scales::percent(recurrence_rate))) +
                      facet_wrap(~attractors_n) +
                      coord_flip(ylim = c(0, 1))
                  }
      ))

emadata_nested_wrangled_both_recnets_nodes_plots$attractor_plots[[1]]

# emadata_nested_wrangled_both_recnets_nodes_plots$all_nodes_with_strengths[[1]]

# # Save this to be used for the task analysis later; otherwise overwritten by sensitivity analyses:
# firstlast_emadata_nested_wrangled_both_recnets_nodes_plots <- 
#   emadata_nested_wrangled_both_recnets_nodes_plots

# cowplot::save_plot("./figures/attractors.png", 
#                    emadata_nested_wrangled_both_recnets_nodes_plots$attractor_plots[[1]], 
#                    dpi = 300,
#                    base_height = 11.69/2)

9%

Here’s the 6-dimensional motivation system’s recurrence plot, weighted by similarity.

recurrence_rate <- 0.09

set.seed(100)

#######################

# si = similarity under the radius
emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled %>% 
  dplyr::mutate(RN = purrr::map(.x = data_firstlast_divided_by_max,
                                .f =  ~casnet::rn(.x %>% dplyr::select(#autonomy, competence, relatedness,
                                                                       pleasure, interest, importance,
                                                                       situation_requires, 
                                                                       anxiety_guilt_avoidance, 
                                                                       another_wants), 
                                                  doEmbed = FALSE, 
                                                  weighted = TRUE, 
                                                  weightedBy = "si", 
                                                  emRad = NA,
                                                  targetValue = recurrence_rate)))
## 
## Auto-recurrence: Setting diagonal to (1 + max. distance) for analyses
## 
## Searching for a radius that will yield 0.09 for RR

emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(graph_from_adjacency = purrr::map(.x = RN,
                                                  .f = ~igraph::graph_from_adjacency_matrix(.x, 
                                                                                            weighted = TRUE, 
                                                                                            mode = "upper", 
                                                                                            diag = FALSE)))

# Edges with their distances
emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(edges_with_distances = purrr::map(.x = graph_from_adjacency,
                                                  .f = ~igraph::E(.x)$weight),
                graph_from_adjacency_orig = graph_from_adjacency)

# Larger values are closer to the state; inverse of weight makes it more intuitive
for (i in 1:nrow(emadata_nested_wrangled_both_recnets)) {
igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$weight <- (1/emadata_nested_wrangled_both_recnets$edges_with_distances[[i]])
}
# A later note to self: Now weight is a measure of distance; how far apart two time points are
# (under the radius, i.e. they're reasonably similar to begin with)

####### To check:
# igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[1]])$weight
# igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency_orig[[1]])$weight

emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(RN_plot = purrr::map(.x = RN,
                                .f =  ~casnet::rn_plot(.x,
                                                       plotDimensions = TRUE,
                                                       xlab = "6-dimensional motivation system",
                                                       ylab = "6-dimensional motivation system")))


# Make node size equal to strength. Strength is the sum of a node's edge weights.
for (i in 1:nrow(emadata_nested_wrangled_both_recnets)) {
igraph::V(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$size <- (igraph::strength(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]]))
}

# Rescaling weight as "width"; varies between 5 and 10
for (i in 1:nrow(emadata_nested_wrangled_both_recnets)) {
igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$width <- 
  casnet::elascer(igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$weight, lo = 5, hi = 10)
}

The lengthy code chunk below extracts and marks attractors in the data.

# Get number of maximally connected node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(strongest_day = purrr::map(.x = graph_from_adjacency,
                                     .f =  ~which.max(igraph::strength(.x))
                                     ))



emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(list_of_edges = purrr::map(.x = graph_from_adjacency,
                                     .f =  ~igraph::get.data.frame(.x)
                                     ))

emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = 
                  purrr::map2(.x = data_firstlast_divided_by_max,
                              .y = graph_from_adjacency,
                              .f = ~{
                                data.frame(.x, 
                                           strength = igraph::strength(.y)) %>% 
                                  dplyr::mutate(time = dplyr::row_number()) %>% 
                                  tidyr::pivot_longer(cols = c(-strength, -time))
                              }
  ))

# Extract nodes (i.e. times) which connect to the strongest (i.e. most connected) node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = strongest_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))


# Get number of 2nd maximally connected node, which doesn't connect to the 1st
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(secondary_attractor_day = purrr::map2(.x = graph_from_adjacency,
                                                      .y = connecting_to_strongest,
                                                      .f =  ~{
                                                        data.frame(strength = igraph::strength(.x), 
                                                                   time = 1:length(igraph::strength(.x))) %>% 
                                                          dplyr::filter(!time %in% .y) %>% 
                                                          dplyr::arrange(desc(strength)) %>% 
                                                          dplyr::slice(1) %>% 
                                                          dplyr::pull(time)
                                                      }                                                    
  ))

# Extract nodes (i.e. times) which connect to the 2nd strongest node, which doesn't connect to the 1st
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_2nd_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = secondary_attractor_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

# Save as a variable in the dataset
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_2nd_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_2nd_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))

# Get number of 3rd maximally connected node, which doesn't connect to the 1st or second
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(tertiary_attractor_day = purrr::pmap(list(..1 = graph_from_adjacency,
                                                      ..2 = connecting_to_strongest,
                                                      ..3 = connecting_to_2nd_strongest),
                                                      .f =  ~{
                                                        data.frame(strength = igraph::strength(..1), 
                                                                   time = 1:length(igraph::strength(..1))) %>% 
                                                          dplyr::filter(!time %in% ..2, 
                                                                        !time %in% ..3) %>% 
                                                          dplyr::arrange(desc(strength)) %>% 
                                                          dplyr::slice(1) %>% 
                                                          dplyr::pull(time)
                                                      }                                                    
  ))

# Extract nodes (i.e. times) which connect to the 3rd strongest node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_3rd_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = tertiary_attractor_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

# Save as a variable
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_3rd_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_3rd_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))

# Get number of 4th maximally connected node, which doesn't connect to the 1st, 2nd, or 3rd
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(fourth_attractor_day = purrr::pmap(list(..1 = graph_from_adjacency,
                                                      ..2 = connecting_to_strongest,
                                                      ..3 = connecting_to_2nd_strongest,
                                                      ..4 = connecting_to_3rd_strongest),
                                                      .f =  ~{
                                                        data.frame(strength = igraph::strength(..1), 
                                                                   time = 1:length(igraph::strength(..1))) %>% 
                                                          dplyr::filter(!time %in% ..2, 
                                                                        !time %in% ..3, 
                                                                        !time %in% ..4) %>% 
                                                          dplyr::arrange(desc(strength)) %>% 
                                                          dplyr::slice(1) %>% 
                                                          dplyr::pull(time)
                                                      }                                                    
  ))

# Extract nodes (i.e. times) which connect to the 4th strongest node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_4th_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = fourth_attractor_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

# Save as a variable
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_4th_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_4th_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))

################### Make plots

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = 
                  purrr::map(.x = all_nodes_with_strengths,
                             .f = ~{
                               dplyr::mutate(.x,
                                             attractors = dplyr::case_when(
                                               strength == 0 ~ "Unique",
                                               connecting_to_strongest == TRUE ~ "1st",
                                               connecting_to_2nd_strongest == TRUE ~ "2nd",
                                               connecting_to_3rd_strongest == TRUE ~ "3rd",
                                               connecting_to_4th_strongest == TRUE ~ "4th",
                                               TRUE ~ "Uncategorised"),
                                             attractors = factor(attractors,
                                                                 levels = c("1st",
                                                                            "2nd",
                                                                            "3rd",
                                                                            "4th",
                                                                            "Uncategorised",
                                                                            "Unique")),
                                             name = factor(name,
                                                           levels = c("pleasure",
                                                                      "interest",
                                                                      "importance",
                                                                      "situation_requires",
                                                                      "anxiety_guilt_avoidance",
                                                                      "another_wants"),
                                                           labels = c("Pleasure",
                                                                      "Interest",
                                                                      "Importance",
                                                                      "Situation requires",
                                                                      "Anxiety guilt avoidance",
                                                                      "Another wants")) %>% 
                                               forcats::fct_drop()) %>% 
                                 dplyr::group_by(attractors, name) %>% 
                                 dplyr::mutate(n = n()) %>% 
                                 dplyr::ungroup() %>%
                                 dplyr::mutate(maxtime = max(time),
                                               percentage_of_total =
                                                 (n / maxtime) %>% scales::percent(accuracy = 0.1),
                                               proportion_of_total = n/maxtime,
                                               attractors_n = 
                                                 factor(paste0(attractors,
                                                               " (n = ", n, "; ", 
                                                               percentage_of_total, ")")))
                             }
                  ))

Spiral graph with colored nodes

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>%
  dplyr::mutate(node_colors = purrr::map(.x = all_nodes_with_strengths,
                                         .f = ~{tidyr::pivot_wider(.x, names_from = name) %>%
                                             dplyr::pull(attractors)}))

for (i in 1:nrow(emadata_nested_wrangled_both_recnets_nodes_plots)) {
levels(emadata_nested_wrangled_both_recnets_nodes_plots$node_colors[[i]]) <- 
  c(viridisLite::plasma(4, 
                      end = 0.8, 
                      direction = -1), "gray48", "white")
  }

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>% 
  dplyr::mutate(spiralgraph_epochs = purrr::pmap(list(..1 = graph_from_adjacency,
                                          ..2 = node_colors,
                                          ..3 = User),
                                     .f =  ~casnet::make_spiral_graph(g = ..1, 
                                                                      arcs = 4, 
                                                                      # a = .1, 
                                                                      # b = 2, 
                                                                      markTimeBy = TRUE,
                                                                      markEpochsBy = ..2,
                                                                      epochColours = ..2,
                                                                      showEpochLegend = FALSE,
                                                                      scaleEdgeSize = 1/10,
                                                                      scaleVertexSize = c(1, 5),
                                                                      showSizeLegend = FALSE,
                                                                      sizeLabel = "Node strength",
                                                                      type = "Euler",
                                                                      # alphaE = 0.1
                                                                      # title = ..3
                                     )))


# emadata_nested_wrangled_both_recnets_nodes_plots$spiralgraph_epochs[[1]] +
#   theme(plot.margin=grid::unit(c(0,0,0,0), "mm"))

# ggsave(filename = "./figures/recnetwork.png",
#        width = 7, 
#        height = 7)

# emadata_nested_wrangled_both_recnets_nodes_plots$all_nodes_with_strengths[[1]]

Attractor plot

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>%
  dplyr::mutate(observations = purrr::map_dbl(.x = data_firstlast_divided_by_max,
                                              .f = ~nrow(.)))

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>%
  dplyr::mutate(observations_daily = purrr::map_dbl(.x = data_daily,
                                              .f = ~nrow(.)))


emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>% 
  dplyr::mutate(
    attractor_plots = 
      purrr::pmap(list(..1 = all_nodes_with_strengths,
                       ..2 = observations,
                       ..3 = observations_daily,
                       ..4 = User),
                  .f = ~{
                    dplyr::mutate(..1,
                                  strength_rescaled = 
                                    scales::rescale(strength, to = c(0.3, 1.1)),
                                  alpha_strength = ifelse(strength_rescaled == 0.3, 
                                                          0.5, 
                                                          strength_rescaled)) %>% 
                      ggplot(data = .,
                             aes(x = forcats::fct_rev(name), 
                                 y = value, 
                                 size = strength_rescaled,
                                 alpha = alpha_strength,
                                 color = attractors_n)) +
                      scale_size_identity() +
                      scale_alpha_identity() +
                      geom_point(aes(alpha = alpha_strength)) +
                      geom_line(aes(group = time,
                                    alpha = alpha_strength)) +
                      scale_color_manual(values = c(viridisLite::plasma(4, 
                                                                        end = 0.8, 
                                                                        direction = -1), 
                                                    "gray40", "gray50")) +
                      scale_y_continuous(labels = scales::label_percent(accuracy = 1)) +
                      theme_bw() +
                      theme(legend.position = "none") +
                      labs(y = "Percentage of maximum reported value of variable, across full time series",
                           x = NULL,
                           title = paste0("Participant \"", ..4, "\" - based on ", ..2, " data points (", ..3, " days)"),
                           caption = paste0("Recurrence rate used for the analysis: ", scales::percent(recurrence_rate))) +
                      facet_wrap(~attractors_n) +
                      coord_flip(ylim = c(0, 1))
                  }
      ))

emadata_nested_wrangled_both_recnets_nodes_plots$attractor_plots[[1]]

# emadata_nested_wrangled_both_recnets_nodes_plots$all_nodes_with_strengths[[1]]

# # Save this to be used for the task analysis later; otherwise overwritten by sensitivity analyses:
# firstlast_emadata_nested_wrangled_both_recnets_nodes_plots <- 
#   emadata_nested_wrangled_both_recnets_nodes_plots

# cowplot::save_plot("./figures/attractors.png", 
#                    emadata_nested_wrangled_both_recnets_nodes_plots$attractor_plots[[1]], 
#                    dpi = 300,
#                    base_height = 11.69/2)

10%

Here’s the 6-dimensional motivation system’s recurrence plot, weighted by similarity.

recurrence_rate <- 0.10

set.seed(100)

#######################

# si = similarity under the radius
emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled %>% 
  dplyr::mutate(RN = purrr::map(.x = data_firstlast_divided_by_max,
                                .f =  ~casnet::rn(.x %>% dplyr::select(#autonomy, competence, relatedness,
                                                                       pleasure, interest, importance,
                                                                       situation_requires, 
                                                                       anxiety_guilt_avoidance, 
                                                                       another_wants), 
                                                  doEmbed = FALSE, 
                                                  weighted = TRUE, 
                                                  weightedBy = "si", 
                                                  emRad = NA,
                                                  targetValue = recurrence_rate)))
## 
## Auto-recurrence: Setting diagonal to (1 + max. distance) for analyses
## 
## Searching for a radius that will yield 0.1 for RR

emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(graph_from_adjacency = purrr::map(.x = RN,
                                                  .f = ~igraph::graph_from_adjacency_matrix(.x, 
                                                                                            weighted = TRUE, 
                                                                                            mode = "upper", 
                                                                                            diag = FALSE)))

# Edges with their distances
emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(edges_with_distances = purrr::map(.x = graph_from_adjacency,
                                                  .f = ~igraph::E(.x)$weight),
                graph_from_adjacency_orig = graph_from_adjacency)

# Larger values are closer to the state; inverse of weight makes it more intuitive
for (i in 1:nrow(emadata_nested_wrangled_both_recnets)) {
igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$weight <- (1/emadata_nested_wrangled_both_recnets$edges_with_distances[[i]])
}
# A later note to self: Now weight is a measure of distance; how far apart two time points are
# (under the radius, i.e. they're reasonably similar to begin with)

####### To check:
# igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[1]])$weight
# igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency_orig[[1]])$weight

emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(RN_plot = purrr::map(.x = RN,
                                .f =  ~casnet::rn_plot(.x,
                                                       plotDimensions = TRUE,
                                                       xlab = "6-dimensional motivation system",
                                                       ylab = "6-dimensional motivation system")))


# Make node size equal to strength. Strength is the sum of a node's edge weights.
for (i in 1:nrow(emadata_nested_wrangled_both_recnets)) {
igraph::V(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$size <- (igraph::strength(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]]))
}

# Rescaling weight as "width"; varies between 5 and 10
for (i in 1:nrow(emadata_nested_wrangled_both_recnets)) {
igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$width <- 
  casnet::elascer(igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$weight, lo = 5, hi = 10)
}

The lengthy code chunk below extracts and marks attractors in the data.

# Get number of maximally connected node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(strongest_day = purrr::map(.x = graph_from_adjacency,
                                     .f =  ~which.max(igraph::strength(.x))
                                     ))



emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(list_of_edges = purrr::map(.x = graph_from_adjacency,
                                     .f =  ~igraph::get.data.frame(.x)
                                     ))

emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = 
                  purrr::map2(.x = data_firstlast_divided_by_max,
                              .y = graph_from_adjacency,
                              .f = ~{
                                data.frame(.x, 
                                           strength = igraph::strength(.y)) %>% 
                                  dplyr::mutate(time = dplyr::row_number()) %>% 
                                  tidyr::pivot_longer(cols = c(-strength, -time))
                              }
  ))

# Extract nodes (i.e. times) which connect to the strongest (i.e. most connected) node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = strongest_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))


# Get number of 2nd maximally connected node, which doesn't connect to the 1st
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(secondary_attractor_day = purrr::map2(.x = graph_from_adjacency,
                                                      .y = connecting_to_strongest,
                                                      .f =  ~{
                                                        data.frame(strength = igraph::strength(.x), 
                                                                   time = 1:length(igraph::strength(.x))) %>% 
                                                          dplyr::filter(!time %in% .y) %>% 
                                                          dplyr::arrange(desc(strength)) %>% 
                                                          dplyr::slice(1) %>% 
                                                          dplyr::pull(time)
                                                      }                                                    
  ))

# Extract nodes (i.e. times) which connect to the 2nd strongest node, which doesn't connect to the 1st
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_2nd_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = secondary_attractor_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

# Save as a variable in the dataset
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_2nd_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_2nd_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))

# Get number of 3rd maximally connected node, which doesn't connect to the 1st or second
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(tertiary_attractor_day = purrr::pmap(list(..1 = graph_from_adjacency,
                                                      ..2 = connecting_to_strongest,
                                                      ..3 = connecting_to_2nd_strongest),
                                                      .f =  ~{
                                                        data.frame(strength = igraph::strength(..1), 
                                                                   time = 1:length(igraph::strength(..1))) %>% 
                                                          dplyr::filter(!time %in% ..2, 
                                                                        !time %in% ..3) %>% 
                                                          dplyr::arrange(desc(strength)) %>% 
                                                          dplyr::slice(1) %>% 
                                                          dplyr::pull(time)
                                                      }                                                    
  ))

# Extract nodes (i.e. times) which connect to the 3rd strongest node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_3rd_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = tertiary_attractor_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

# Save as a variable
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_3rd_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_3rd_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))

# Get number of 4th maximally connected node, which doesn't connect to the 1st, 2nd, or 3rd
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(fourth_attractor_day = purrr::pmap(list(..1 = graph_from_adjacency,
                                                      ..2 = connecting_to_strongest,
                                                      ..3 = connecting_to_2nd_strongest,
                                                      ..4 = connecting_to_3rd_strongest),
                                                      .f =  ~{
                                                        data.frame(strength = igraph::strength(..1), 
                                                                   time = 1:length(igraph::strength(..1))) %>% 
                                                          dplyr::filter(!time %in% ..2, 
                                                                        !time %in% ..3, 
                                                                        !time %in% ..4) %>% 
                                                          dplyr::arrange(desc(strength)) %>% 
                                                          dplyr::slice(1) %>% 
                                                          dplyr::pull(time)
                                                      }                                                    
  ))

# Extract nodes (i.e. times) which connect to the 4th strongest node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_4th_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = fourth_attractor_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

# Save as a variable
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_4th_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_4th_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))

################### Make plots

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = 
                  purrr::map(.x = all_nodes_with_strengths,
                             .f = ~{
                               dplyr::mutate(.x,
                                             attractors = dplyr::case_when(
                                               strength == 0 ~ "Unique",
                                               connecting_to_strongest == TRUE ~ "1st",
                                               connecting_to_2nd_strongest == TRUE ~ "2nd",
                                               connecting_to_3rd_strongest == TRUE ~ "3rd",
                                               connecting_to_4th_strongest == TRUE ~ "4th",
                                               TRUE ~ "Uncategorised"),
                                             attractors = factor(attractors,
                                                                 levels = c("1st",
                                                                            "2nd",
                                                                            "3rd",
                                                                            "4th",
                                                                            "Uncategorised",
                                                                            "Unique")),
                                             name = factor(name,
                                                           levels = c("pleasure",
                                                                      "interest",
                                                                      "importance",
                                                                      "situation_requires",
                                                                      "anxiety_guilt_avoidance",
                                                                      "another_wants"),
                                                           labels = c("Pleasure",
                                                                      "Interest",
                                                                      "Importance",
                                                                      "Situation requires",
                                                                      "Anxiety guilt avoidance",
                                                                      "Another wants")) %>% 
                                               forcats::fct_drop()) %>% 
                                 dplyr::group_by(attractors, name) %>% 
                                 dplyr::mutate(n = n()) %>% 
                                 dplyr::ungroup() %>%
                                 dplyr::mutate(maxtime = max(time),
                                               percentage_of_total =
                                                 (n / maxtime) %>% scales::percent(accuracy = 0.1),
                                               proportion_of_total = n/maxtime,
                                               attractors_n = 
                                                 factor(paste0(attractors,
                                                               " (n = ", n, "; ", 
                                                               percentage_of_total, ")")))
                             }
                  ))

Spiral graph with colored nodes

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>%
  dplyr::mutate(node_colors = purrr::map(.x = all_nodes_with_strengths,
                                         .f = ~{tidyr::pivot_wider(.x, names_from = name) %>%
                                             dplyr::pull(attractors)}))

for (i in 1:nrow(emadata_nested_wrangled_both_recnets_nodes_plots)) {
levels(emadata_nested_wrangled_both_recnets_nodes_plots$node_colors[[i]]) <- 
  c(viridisLite::plasma(4, 
                      end = 0.8, 
                      direction = -1), "gray48", "white")
  }

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>% 
  dplyr::mutate(spiralgraph_epochs = purrr::pmap(list(..1 = graph_from_adjacency,
                                          ..2 = node_colors,
                                          ..3 = User),
                                     .f =  ~casnet::make_spiral_graph(g = ..1, 
                                                                      arcs = 4, 
                                                                      # a = .1, 
                                                                      # b = 2, 
                                                                      markTimeBy = TRUE,
                                                                      markEpochsBy = ..2,
                                                                      epochColours = ..2,
                                                                      showEpochLegend = FALSE,
                                                                      scaleEdgeSize = 1/10,
                                                                      scaleVertexSize = c(1, 5),
                                                                      showSizeLegend = FALSE,
                                                                      sizeLabel = "Node strength",
                                                                      type = "Euler",
                                                                      # alphaE = 0.1
                                                                      # title = ..3
                                     )))


# emadata_nested_wrangled_both_recnets_nodes_plots$spiralgraph_epochs[[1]] +
#   theme(plot.margin=grid::unit(c(0,0,0,0), "mm"))

# ggsave(filename = "./figures/recnetwork.png",
#        width = 7, 
#        height = 7)

# emadata_nested_wrangled_both_recnets_nodes_plots$all_nodes_with_strengths[[1]]

Attractor plot

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>%
  dplyr::mutate(observations = purrr::map_dbl(.x = data_firstlast_divided_by_max,
                                              .f = ~nrow(.)))

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>%
  dplyr::mutate(observations_daily = purrr::map_dbl(.x = data_daily,
                                              .f = ~nrow(.)))


emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>% 
  dplyr::mutate(
    attractor_plots = 
      purrr::pmap(list(..1 = all_nodes_with_strengths,
                       ..2 = observations,
                       ..3 = observations_daily,
                       ..4 = User),
                  .f = ~{
                    dplyr::mutate(..1,
                                  strength_rescaled = 
                                    scales::rescale(strength, to = c(0.3, 1.1)),
                                  alpha_strength = ifelse(strength_rescaled == 0.3, 
                                                          0.5, 
                                                          strength_rescaled)) %>% 
                      ggplot(data = .,
                             aes(x = forcats::fct_rev(name), 
                                 y = value, 
                                 size = strength_rescaled,
                                 alpha = alpha_strength,
                                 color = attractors_n)) +
                      scale_size_identity() +
                      scale_alpha_identity() +
                      geom_point(aes(alpha = alpha_strength)) +
                      geom_line(aes(group = time,
                                    alpha = alpha_strength)) +
                      scale_color_manual(values = c(viridisLite::plasma(4, 
                                                                        end = 0.8, 
                                                                        direction = -1), 
                                                    "gray40", "gray50")) +
                      scale_y_continuous(labels = scales::label_percent(accuracy = 1)) +
                      theme_bw() +
                      theme(legend.position = "none") +
                      labs(y = "Percentage of maximum reported value of variable, across full time series",
                           x = NULL,
                           title = paste0("Participant \"", ..4, "\" - based on ", ..2, " data points (", ..3, " days)"),
                           caption = paste0("Recurrence rate used for the analysis: ", scales::percent(recurrence_rate))) +
                      facet_wrap(~attractors_n) +
                      coord_flip(ylim = c(0, 1))
                  }
      ))

emadata_nested_wrangled_both_recnets_nodes_plots$attractor_plots[[1]]

# emadata_nested_wrangled_both_recnets_nodes_plots$all_nodes_with_strengths[[1]]

# # Save this to be used for the task analysis later; otherwise overwritten by sensitivity analyses:
# firstlast_emadata_nested_wrangled_both_recnets_nodes_plots <- 
#   emadata_nested_wrangled_both_recnets_nodes_plots

# cowplot::save_plot("./figures/attractors.png", 
#                    emadata_nested_wrangled_both_recnets_nodes_plots$attractor_plots[[1]], 
#                    dpi = 300,
#                    base_height = 11.69/2)

Robustness to distance measure

In this robustness check, we perform the analysis using Chebyshev distance instead of the Euclidean.

Here’s the 6-dimensional motivation system’s recurrence plot, weighted by similarity.

recurrence_rate <- 0.05

set.seed(100)

#######################

# si = similarity under the radius
emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled %>% 
  dplyr::mutate(RN = purrr::map(.x = data_firstlast_divided_by_max,
                                .f =  ~casnet::rn(.x %>% dplyr::select(#autonomy, competence, relatedness,
                                                                       pleasure, interest, importance,
                                                                       situation_requires, 
                                                                       anxiety_guilt_avoidance, 
                                                                       another_wants), 
                                                  doEmbed = FALSE, 
                                                  weighted = TRUE, 
                                                  weightedBy = "si", 
                                                  method = "max",
                                                  emRad = NA,
                                                  targetValue = recurrence_rate)))
## 
## Auto-recurrence: Setting diagonal to (1 + max. distance) for analyses
## 
## Searching for a radius that will yield 0.05 for RR

emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(graph_from_adjacency = purrr::map(.x = RN,
                                                  .f = ~igraph::graph_from_adjacency_matrix(.x, 
                                                                                            weighted = TRUE, 
                                                                                            mode = "upper", 
                                                                                            diag = FALSE)))

# Edges with their distances
emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(edges_with_distances = purrr::map(.x = graph_from_adjacency,
                                                  .f = ~igraph::E(.x)$weight),
                graph_from_adjacency_orig = graph_from_adjacency)

# Larger values are closer to the state; inverse of weight makes it more intuitive
for (i in 1:nrow(emadata_nested_wrangled_both_recnets)) {
igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$weight <- (1/emadata_nested_wrangled_both_recnets$edges_with_distances[[i]])
}
# A later note to self: Now weight is a measure of distance; how far apart two time points are
# (under the radius, i.e. they're reasonably similar to begin with)

####### To check:
# igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[1]])$weight
# igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency_orig[[1]])$weight

emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(RN_plot = purrr::map(.x = RN,
                                .f =  ~casnet::rn_plot(.x,
                                                       plotDimensions = TRUE,
                                                       xlab = "6-dimensional motivation system",
                                                       ylab = "6-dimensional motivation system")))


# Make node size equal to strength. Strength is the sum of a node's edge weights.
for (i in 1:nrow(emadata_nested_wrangled_both_recnets)) {
igraph::V(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$size <- (igraph::strength(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]]))
}

# Rescaling weight as "width"; varies between 5 and 10
for (i in 1:nrow(emadata_nested_wrangled_both_recnets)) {
igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$width <- 
  casnet::elascer(igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$weight, lo = 5, hi = 10)
}

The lengthy code chunk below extracts and marks attractors in the data.

# Get number of maximally connected node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(strongest_day = purrr::map(.x = graph_from_adjacency,
                                     .f =  ~which.max(igraph::strength(.x))
                                     ))



emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(list_of_edges = purrr::map(.x = graph_from_adjacency,
                                     .f =  ~igraph::get.data.frame(.x)
                                     ))

emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = 
                  purrr::map2(.x = data_firstlast_divided_by_max,
                              .y = graph_from_adjacency,
                              .f = ~{
                                data.frame(.x, 
                                           strength = igraph::strength(.y)) %>% 
                                  dplyr::mutate(time = dplyr::row_number()) %>% 
                                  tidyr::pivot_longer(cols = c(-strength, -time))
                              }
  ))

# Extract nodes (i.e. times) which connect to the strongest (i.e. most connected) node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = strongest_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))


# Get number of 2nd maximally connected node, which doesn't connect to the 1st
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(secondary_attractor_day = purrr::map2(.x = graph_from_adjacency,
                                                      .y = connecting_to_strongest,
                                                      .f =  ~{
                                                        data.frame(strength = igraph::strength(.x), 
                                                                   time = 1:length(igraph::strength(.x))) %>% 
                                                          dplyr::filter(!time %in% .y) %>% 
                                                          dplyr::arrange(desc(strength)) %>% 
                                                          dplyr::slice(1) %>% 
                                                          dplyr::pull(time)
                                                      }                                                    
  ))

# Extract nodes (i.e. times) which connect to the 2nd strongest node, which doesn't connect to the 1st
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_2nd_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = secondary_attractor_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

# Save as a variable in the dataset
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_2nd_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_2nd_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))

# Get number of 3rd maximally connected node, which doesn't connect to the 1st or second
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(tertiary_attractor_day = purrr::pmap(list(..1 = graph_from_adjacency,
                                                      ..2 = connecting_to_strongest,
                                                      ..3 = connecting_to_2nd_strongest),
                                                      .f =  ~{
                                                        data.frame(strength = igraph::strength(..1), 
                                                                   time = 1:length(igraph::strength(..1))) %>% 
                                                          dplyr::filter(!time %in% ..2, 
                                                                        !time %in% ..3) %>% 
                                                          dplyr::arrange(desc(strength)) %>% 
                                                          dplyr::slice(1) %>% 
                                                          dplyr::pull(time)
                                                      }                                                    
  ))

# Extract nodes (i.e. times) which connect to the 3rd strongest node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_3rd_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = tertiary_attractor_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

# Save as a variable
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_3rd_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_3rd_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))

# Get number of 4th maximally connected node, which doesn't connect to the 1st, 2nd, or 3rd
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(fourth_attractor_day = purrr::pmap(list(..1 = graph_from_adjacency,
                                                      ..2 = connecting_to_strongest,
                                                      ..3 = connecting_to_2nd_strongest,
                                                      ..4 = connecting_to_3rd_strongest),
                                                      .f =  ~{
                                                        data.frame(strength = igraph::strength(..1), 
                                                                   time = 1:length(igraph::strength(..1))) %>% 
                                                          dplyr::filter(!time %in% ..2, 
                                                                        !time %in% ..3, 
                                                                        !time %in% ..4) %>% 
                                                          dplyr::arrange(desc(strength)) %>% 
                                                          dplyr::slice(1) %>% 
                                                          dplyr::pull(time)
                                                      }                                                    
  ))

# Extract nodes (i.e. times) which connect to the 4th strongest node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_4th_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = fourth_attractor_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

# Save as a variable
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_4th_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_4th_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))

################### Make plots

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = 
                  purrr::map(.x = all_nodes_with_strengths,
                             .f = ~{
                               dplyr::mutate(.x,
                                             attractors = dplyr::case_when(
                                               strength == 0 ~ "Unique",
                                               connecting_to_strongest == TRUE ~ "1st",
                                               connecting_to_2nd_strongest == TRUE ~ "2nd",
                                               connecting_to_3rd_strongest == TRUE ~ "3rd",
                                               connecting_to_4th_strongest == TRUE ~ "4th",
                                               TRUE ~ "Uncategorised"),
                                             attractors = factor(attractors,
                                                                 levels = c("1st",
                                                                            "2nd",
                                                                            "3rd",
                                                                            "4th",
                                                                            "Uncategorised",
                                                                            "Unique")),
                                             name = factor(name,
                                                           levels = c("pleasure",
                                                                      "interest",
                                                                      "importance",
                                                                      "situation_requires",
                                                                      "anxiety_guilt_avoidance",
                                                                      "another_wants"),
                                                           labels = c("Pleasure",
                                                                      "Interest",
                                                                      "Importance",
                                                                      "Situation requires",
                                                                      "Anxiety guilt avoidance",
                                                                      "Another wants")) %>% 
                                               forcats::fct_drop()) %>% 
                                 dplyr::group_by(attractors, name) %>% 
                                 dplyr::mutate(n = n()) %>% 
                                 dplyr::ungroup() %>%
                                 dplyr::mutate(maxtime = max(time),
                                               percentage_of_total =
                                                 (n / maxtime) %>% scales::percent(accuracy = 0.1),
                                               proportion_of_total = n/maxtime,
                                               attractors_n = 
                                                 factor(paste0(attractors,
                                                               " (n = ", n, "; ", 
                                                               percentage_of_total, ")")))
                             }
                  ))

Spiral graph with colored nodes

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>%
  dplyr::mutate(node_colors = purrr::map(.x = all_nodes_with_strengths,
                                         .f = ~{tidyr::pivot_wider(.x, names_from = name) %>%
                                             dplyr::pull(attractors)}))

for (i in 1:nrow(emadata_nested_wrangled_both_recnets_nodes_plots)) {
levels(emadata_nested_wrangled_both_recnets_nodes_plots$node_colors[[i]]) <- 
  c(viridisLite::plasma(4, 
                      end = 0.8, 
                      direction = -1), "gray48", "white")
  }

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>% 
  dplyr::mutate(spiralgraph_epochs = purrr::pmap(list(..1 = graph_from_adjacency,
                                          ..2 = node_colors,
                                          ..3 = User),
                                     .f =  ~casnet::make_spiral_graph(g = ..1, 
                                                                      arcs = 4, 
                                                                      # a = .1, 
                                                                      # b = 2, 
                                                                      markTimeBy = TRUE,
                                                                      markEpochsBy = ..2,
                                                                      epochColours = ..2,
                                                                      showEpochLegend = FALSE,
                                                                      scaleEdgeSize = 1/10,
                                                                      scaleVertexSize = c(1, 5),
                                                                      showSizeLegend = FALSE,
                                                                      sizeLabel = "Node strength",
                                                                      type = "Euler",
                                                                      # alphaE = 0.1
                                                                      # title = ..3
                                     )))


# emadata_nested_wrangled_both_recnets_nodes_plots$spiralgraph_epochs[[1]] +
#   theme(plot.margin=grid::unit(c(0,0,0,0), "mm"))

# ggsave(filename = "./figures/recnetwork.png",
#        width = 7, 
#        height = 7)

# emadata_nested_wrangled_both_recnets_nodes_plots$all_nodes_with_strengths[[1]]

Attractor plot

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>%
  dplyr::mutate(observations = purrr::map_dbl(.x = data_firstlast_divided_by_max,
                                              .f = ~nrow(.)))

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>%
  dplyr::mutate(observations_daily = purrr::map_dbl(.x = data_daily,
                                              .f = ~nrow(.)))


emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>% 
  dplyr::mutate(
    attractor_plots = 
      purrr::pmap(list(..1 = all_nodes_with_strengths,
                       ..2 = observations,
                       ..3 = observations_daily,
                       ..4 = User),
                  .f = ~{
                    dplyr::mutate(..1,
                                  strength_rescaled = 
                                    scales::rescale(strength, to = c(0.3, 1.1)),
                                  alpha_strength = ifelse(strength_rescaled == 0.3, 
                                                          0.5, 
                                                          strength_rescaled)) %>% 
                      ggplot(data = .,
                             aes(x = forcats::fct_rev(name), 
                                 y = value, 
                                 size = strength_rescaled,
                                 alpha = alpha_strength,
                                 color = attractors_n)) +
                      scale_size_identity() +
                      scale_alpha_identity() +
                      geom_point(aes(alpha = alpha_strength)) +
                      geom_line(aes(group = time,
                                    alpha = alpha_strength)) +
                      scale_color_manual(values = c(viridisLite::plasma(4, 
                                                                        end = 0.8, 
                                                                        direction = -1), 
                                                    "gray40", "gray50")) +
                      scale_y_continuous(labels = scales::label_percent(accuracy = 1)) +
                      theme_bw() +
                      theme(legend.position = "none") +
                      labs(y = "Percentage of maximum reported value of variable, across full time series",
                           x = NULL,
                           title = paste0("Participant \"", ..4, "\" - based on ", ..2, " data points (", ..3, " days)"),
                           caption = paste0("Recurrence rate used for the analysis: ", scales::percent(recurrence_rate))) +
                      facet_wrap(~attractors_n) +
                      coord_flip(ylim = c(0, 1))
                  }
      ))

emadata_nested_wrangled_both_recnets_nodes_plots$attractor_plots[[1]]

# emadata_nested_wrangled_both_recnets_nodes_plots$all_nodes_with_strengths[[1]]

# # Save this to be used for the task analysis later; otherwise overwritten by sensitivity analyses:
# firstlast_emadata_nested_wrangled_both_recnets_nodes_plots <- 
#   emadata_nested_wrangled_both_recnets_nodes_plots

# cowplot::save_plot("./figures/attractors.png", 
#                    emadata_nested_wrangled_both_recnets_nodes_plots$attractor_plots[[1]], 
#                    dpi = 300,
#                    base_height = 11.69/2)

1. Single randomly sampled daily observation

Here’s the 6-dimensional motivation system’s recurrence plot, weighted by similarity.

set.seed(100)

#######################

# si = similarity under the radius
emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled %>% 
  dplyr::mutate(RN = purrr::map(.x = sample1_standardised,
                                .f =  ~casnet::rn(.x %>% dplyr::select(#autonomy, competence, relatedness,
                                                                       pleasure, interest, importance,
                                                                       situation_requires, 
                                                                       anxiety_guilt_avoidance, 
                                                                       another_wants), 
                                                  doEmbed = FALSE, 
                                                  weighted = TRUE, 
                                                  weightedBy = "si", 
                                                  emRad = NA)))
## 
## Auto-recurrence: Setting diagonal to (1 + max. distance) for analyses
## 
## Searching for a radius that will yield 0.05 for RR

emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(graph_from_adjacency = purrr::map(.x = RN,
                                                  .f = ~igraph::graph_from_adjacency_matrix(.x, 
                                                                                            weighted = TRUE, 
                                                                                            mode = "upper", 
                                                                                            diag = FALSE)))

# Edges with their distances
emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(edges_with_distances = purrr::map(.x = graph_from_adjacency,
                                                  .f = ~igraph::E(.x)$weight),
                graph_from_adjacency_orig = graph_from_adjacency)

# Larger values are closer to the state; inverse of weight makes it more intuitive
for (i in 1:nrow(emadata_nested_wrangled_both_recnets)) {
igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$weight <- (1/emadata_nested_wrangled_both_recnets$edges_with_distances[[i]])
}
# A later note to self: Now weight is a measure of distance; how far apart two time points are
# (under the radius, i.e. they're reasonably similar to begin with)

####### To check:
# igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[1]])$weight
# igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency_orig[[1]])$weight

emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(RN_plot = purrr::map(.x = RN,
                                .f =  ~casnet::rn_plot(.x,
                                                       plotDimensions = TRUE,
                                                       xlab = "6-dimensional motivation system",
                                                       ylab = "6-dimensional motivation system")))


# Make node size equal to strength. Strength is the sum of a node's edge weights.
for (i in 1:nrow(emadata_nested_wrangled_both_recnets)) {
igraph::V(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$size <- (igraph::strength(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]]))
}

# Rescaling weight as "width"; varies between 5 and 10
for (i in 1:nrow(emadata_nested_wrangled_both_recnets)) {
igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$width <- 
  casnet::elascer(igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$weight, lo = 5, hi = 10)
}

The lengthy code chunk below extracts and marks attractors in the data.

# Get number of maximally connected node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(strongest_day = purrr::map(.x = graph_from_adjacency,
                                     .f =  ~which.max(igraph::strength(.x))
                                     ))



emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(list_of_edges = purrr::map(.x = graph_from_adjacency,
                                     .f =  ~igraph::get.data.frame(.x)
                                     ))

emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = 
                  purrr::map2(.x = sample1_standardised,
                              .y = graph_from_adjacency,
                              .f = ~{
                                data.frame(.x %>% 
                                             dplyr::select(#autonomy, competence, relatedness,
                                               pleasure, interest, importance,
                                               situation_requires, anxiety_guilt_avoidance, another_wants), 
                                           strength = igraph::strength(.y)) %>% 
                                  dplyr::mutate(time = dplyr::row_number()) %>% 
                                  tidyr::pivot_longer(cols = c(-strength, -time))
                              }
  ))

# Extract nodes (i.e. times) which connect to the strongest (i.e. most connected) node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = strongest_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))


# Get number of 2nd maximally connected node, which doesn't connect to the 1st
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(secondary_attractor_day = purrr::map2(.x = graph_from_adjacency,
                                                      .y = connecting_to_strongest,
                                                      .f =  ~{
                                                        data.frame(strength = igraph::strength(.x), 
                                                                   time = 1:length(igraph::strength(.x))) %>% 
                                                          dplyr::filter(!time %in% .y) %>% 
                                                          dplyr::arrange(desc(strength)) %>% 
                                                          dplyr::slice(1) %>% 
                                                          dplyr::pull(time)
                                                      }                                                    
  ))

# Extract nodes (i.e. times) which connect to the 2nd strongest node, which doesn't connect to the 1st
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_2nd_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = secondary_attractor_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

# Save as a variable in the dataset
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_2nd_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_2nd_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))

# Get number of 3rd maximally connected node, which doesn't connect to the 1st or second
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(tertiary_attractor_day = purrr::pmap(list(..1 = graph_from_adjacency,
                                                      ..2 = connecting_to_strongest,
                                                      ..3 = connecting_to_2nd_strongest),
                                                      .f =  ~{
                                                        data.frame(strength = igraph::strength(..1), 
                                                                   time = 1:length(igraph::strength(..1))) %>% 
                                                          dplyr::filter(!time %in% ..2, 
                                                                        !time %in% ..3) %>% 
                                                          dplyr::arrange(desc(strength)) %>% 
                                                          dplyr::slice(1) %>% 
                                                          dplyr::pull(time)
                                                      }                                                    
  ))

# Extract nodes (i.e. times) which connect to the 3rd strongest node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_3rd_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = tertiary_attractor_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

# Save as a variable
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_3rd_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_3rd_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))

# Get number of 4th maximally connected node, which doesn't connect to the 1st, 2nd, or 3rd
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(fourth_attractor_day = purrr::pmap(list(..1 = graph_from_adjacency,
                                                      ..2 = connecting_to_strongest,
                                                      ..3 = connecting_to_2nd_strongest,
                                                      ..4 = connecting_to_3rd_strongest),
                                                      .f =  ~{
                                                        data.frame(strength = igraph::strength(..1), 
                                                                   time = 1:length(igraph::strength(..1))) %>% 
                                                          dplyr::filter(!time %in% ..2, 
                                                                        !time %in% ..3, 
                                                                        !time %in% ..4) %>% 
                                                          dplyr::arrange(desc(strength)) %>% 
                                                          dplyr::slice(1) %>% 
                                                          dplyr::pull(time)
                                                      }                                                    
  ))

# Extract nodes (i.e. times) which connect to the 4th strongest node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_4th_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = fourth_attractor_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

# Save as a variable
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_4th_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_4th_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))

################### Make plots

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = 
                  purrr::map(.x = all_nodes_with_strengths,
                             .f = ~{
                               dplyr::mutate(.x,
                                             attractors = dplyr::case_when(
                                               strength == 0 ~ "Unique",
                                               connecting_to_strongest == TRUE ~ "1st",
                                               connecting_to_2nd_strongest == TRUE ~ "2nd",
                                               connecting_to_3rd_strongest == TRUE ~ "3rd",
                                               connecting_to_4th_strongest == TRUE ~ "4th",
                                               TRUE ~ "Uncategorised"),
                                             attractors = factor(attractors,
                                                                 levels = c("1st",
                                                                            "2nd",
                                                                            "3rd",
                                                                            "4th",
                                                                            "Uncategorised",
                                                                            "Unique")),
                                             name = factor(name,
                                                           levels = c("pleasure",
                                                                      "interest",
                                                                      "importance",
                                                                      "situation_requires",
                                                                      "anxiety_guilt_avoidance",
                                                                      "another_wants"),
                                                           labels = c("Pleasure",
                                                                      "Interest",
                                                                      "Importance",
                                                                      "Situation requires",
                                                                      "Anxiety guilt avoidance",
                                                                      "Another wants")) %>% 
                                               forcats::fct_drop()) %>% 
                                 dplyr::group_by(attractors, name) %>% 
                                 dplyr::mutate(n = n()) %>% 
                                 dplyr::ungroup() %>%
                                 dplyr::mutate(maxtime = max(time),
                                               percentage_of_total =
                                                 (n / maxtime) %>% scales::percent(accuracy = 0.1),
                                               proportion_of_total = n/maxtime,
                                               attractors_n = 
                                                 factor(paste0(attractors,
                                                               " (n = ", n, "; ", 
                                                               percentage_of_total, ")")))
                             }
                  ))

Spiral graph with colored nodes

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>%
  dplyr::mutate(node_colors = purrr::map(.x = all_nodes_with_strengths,
                                         .f = ~{tidyr::pivot_wider(.x, names_from = name) %>%
                                             dplyr::pull(attractors)}))

for (i in 1:nrow(emadata_nested_wrangled_both_recnets_nodes_plots)) {
levels(emadata_nested_wrangled_both_recnets_nodes_plots$node_colors[[i]]) <- 
  c(viridisLite::plasma(4, 
                      end = 0.8, 
                      direction = -1), "gray48", "white")
  }

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>% 
  dplyr::mutate(spiralgraph_epochs = purrr::pmap(list(..1 = graph_from_adjacency,
                                          ..2 = node_colors,
                                          ..3 = User),
                                     .f =  ~casnet::make_spiral_graph(g = ..1, 
                                                                      arcs = 4, 
                                                                      # a = .1, 
                                                                      # b = 2, 
                                                                      markTimeBy = TRUE,
                                                                      markEpochsBy = ..2,
                                                                      epochColours = ..2,
                                                                      showEpochLegend = FALSE,
                                                                      scaleEdgeSize = 1/10,
                                                                      scaleVertexSize = c(1, 5),
                                                                      showSizeLegend = FALSE,
                                                                      sizeLabel = "Node strength",
                                                                      type = "Euler",
                                                                      # alphaE = 0.1
                                                                      # title = ..3
                                     )))


# emadata_nested_wrangled_both_recnets_nodes_plots$spiralgraph_epochs[[1]] +
#   theme(plot.margin=grid::unit(c(0,0,0,0), "mm"))

# ggsave(filename = "./figures/recnetwork.png",
#        width = 7, 
#        height = 7)

# emadata_nested_wrangled_both_recnets_nodes_plots$all_nodes_with_strengths[[1]]

Attractor plot

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>%
  dplyr::mutate(observations = purrr::map_dbl(.x = sample1_standardised,
                                              .f = ~nrow(.)))

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>%
  dplyr::mutate(observations_daily = purrr::map_dbl(.x = data_daily,
                                              .f = ~nrow(.)))


emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>% 
  dplyr::mutate(
    attractor_plots = 
      purrr::pmap(list(..1 = all_nodes_with_strengths,
                       ..2 = observations,
                       ..3 = observations_daily,
                       ..4 = User),
                  .f = ~{
                    dplyr::mutate(..1,
                                  strength_rescaled = 
                                    scales::rescale(strength, to = c(0.3, 1.1)),
                                  alpha_strength = ifelse(strength_rescaled == 0.3, 
                                                          0.5, 
                                                          strength_rescaled)) %>% 
                      ggplot(data = .,
                             aes(x = forcats::fct_rev(name), 
                                 y = value, 
                                 size = strength_rescaled,
                                 alpha = alpha_strength,
                                 color = attractors_n)) +
                      scale_size_identity() +
                      scale_alpha_identity() +
                      geom_point(aes(alpha = alpha_strength)) +
                      geom_line(aes(group = time,
                                    alpha = alpha_strength)) +
                      scale_color_manual(values = c(viridisLite::plasma(4, 
                                                                        end = 0.8, 
                                                                        direction = -1), 
                                                    "gray40", "gray50")) +
                      scale_y_continuous(labels = scales::label_percent(accuracy = 1)) +
                      theme_bw() +
                      theme(legend.position = "none") +
                      labs(y = "Percentage of maximum reported value of variable, across full time series",
                           x = NULL,
                           title = paste0("Participant \"", ..4, "\" - based on ", ..2, " data points (", ..3, " days)")) +
                      facet_wrap(~attractors_n) +
                      coord_flip(ylim = c(0, 1))
                  }
      ))

emadata_nested_wrangled_both_recnets_nodes_plots$attractor_plots[[1]]

# emadata_nested_wrangled_both_recnets_nodes_plots$all_nodes_with_strengths[[1]]

2. Single randomly sampled daily observation

Here’s the 6-dimensional motivation system’s recurrence plot, weighted by similarity.

set.seed(100)

#######################

# si = similarity under the radius
emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled %>% 
  dplyr::mutate(RN = purrr::map(.x = sample2_standardised,
                                .f =  ~casnet::rn(.x %>% dplyr::select(#autonomy, competence, relatedness,
                                                                       pleasure, interest, importance,
                                                                       situation_requires, 
                                                                       anxiety_guilt_avoidance, 
                                                                       another_wants), 
                                                  doEmbed = FALSE, 
                                                  weighted = TRUE, 
                                                  weightedBy = "si", 
                                                  emRad = NA)))
## 
## Auto-recurrence: Setting diagonal to (1 + max. distance) for analyses
## 
## Searching for a radius that will yield 0.05 for RR

emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(graph_from_adjacency = purrr::map(.x = RN,
                                                  .f = ~igraph::graph_from_adjacency_matrix(.x, 
                                                                                            weighted = TRUE, 
                                                                                            mode = "upper", 
                                                                                            diag = FALSE)))

# Edges with their distances
emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(edges_with_distances = purrr::map(.x = graph_from_adjacency,
                                                  .f = ~igraph::E(.x)$weight),
                graph_from_adjacency_orig = graph_from_adjacency)

# Larger values are closer to the state; inverse of weight makes it more intuitive
for (i in 1:nrow(emadata_nested_wrangled_both_recnets)) {
igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$weight <- (1/emadata_nested_wrangled_both_recnets$edges_with_distances[[i]])
}
# A later note to self: Now weight is a measure of distance; how far apart two time points are
# (under the radius, i.e. they're reasonably similar to begin with)

####### To check:
# igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[1]])$weight
# igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency_orig[[1]])$weight

emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(RN_plot = purrr::map(.x = RN,
                                .f =  ~casnet::rn_plot(.x,
                                                       plotDimensions = TRUE,
                                                       xlab = "6-dimensional motivation system",
                                                       ylab = "6-dimensional motivation system")))


# Make node size equal to strength. Strength is the sum of a node's edge weights.
for (i in 1:nrow(emadata_nested_wrangled_both_recnets)) {
igraph::V(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$size <- (igraph::strength(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]]))
}

# Rescaling weight as "width"; varies between 5 and 10
for (i in 1:nrow(emadata_nested_wrangled_both_recnets)) {
igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$width <- 
  casnet::elascer(igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$weight, lo = 5, hi = 10)
}

The lengthy code chunk below extracts and marks attractors in the data.

# Get number of maximally connected node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(strongest_day = purrr::map(.x = graph_from_adjacency,
                                     .f =  ~which.max(igraph::strength(.x))
                                     ))



emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(list_of_edges = purrr::map(.x = graph_from_adjacency,
                                     .f =  ~igraph::get.data.frame(.x)
                                     ))

emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = 
                  purrr::map2(.x = sample2_standardised,
                              .y = graph_from_adjacency,
                              .f = ~{
                                data.frame(.x %>% 
                                             dplyr::select(#autonomy, competence, relatedness,
                                               pleasure, interest, importance,
                                               situation_requires, anxiety_guilt_avoidance, another_wants), 
                                           strength = igraph::strength(.y)) %>% 
                                  dplyr::mutate(time = dplyr::row_number()) %>% 
                                  tidyr::pivot_longer(cols = c(-strength, -time))
                              }
  ))

# Extract nodes (i.e. times) which connect to the strongest (i.e. most connected) node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = strongest_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))


# Get number of 2nd maximally connected node, which doesn't connect to the 1st
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(secondary_attractor_day = purrr::map2(.x = graph_from_adjacency,
                                                      .y = connecting_to_strongest,
                                                      .f =  ~{
                                                        data.frame(strength = igraph::strength(.x), 
                                                                   time = 1:length(igraph::strength(.x))) %>% 
                                                          dplyr::filter(!time %in% .y) %>% 
                                                          dplyr::arrange(desc(strength)) %>% 
                                                          dplyr::slice(1) %>% 
                                                          dplyr::pull(time)
                                                      }                                                    
  ))

# Extract nodes (i.e. times) which connect to the 2nd strongest node, which doesn't connect to the 1st
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_2nd_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = secondary_attractor_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

# Save as a variable in the dataset
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_2nd_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_2nd_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))

# Get number of 3rd maximally connected node, which doesn't connect to the 1st or second
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(tertiary_attractor_day = purrr::pmap(list(..1 = graph_from_adjacency,
                                                      ..2 = connecting_to_strongest,
                                                      ..3 = connecting_to_2nd_strongest),
                                                      .f =  ~{
                                                        data.frame(strength = igraph::strength(..1), 
                                                                   time = 1:length(igraph::strength(..1))) %>% 
                                                          dplyr::filter(!time %in% ..2, 
                                                                        !time %in% ..3) %>% 
                                                          dplyr::arrange(desc(strength)) %>% 
                                                          dplyr::slice(1) %>% 
                                                          dplyr::pull(time)
                                                      }                                                    
  ))

# Extract nodes (i.e. times) which connect to the 3rd strongest node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_3rd_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = tertiary_attractor_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

# Save as a variable
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_3rd_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_3rd_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))

# Get number of 4th maximally connected node, which doesn't connect to the 1st, 2nd, or 3rd
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(fourth_attractor_day = purrr::pmap(list(..1 = graph_from_adjacency,
                                                      ..2 = connecting_to_strongest,
                                                      ..3 = connecting_to_2nd_strongest,
                                                      ..4 = connecting_to_3rd_strongest),
                                                      .f =  ~{
                                                        data.frame(strength = igraph::strength(..1), 
                                                                   time = 1:length(igraph::strength(..1))) %>% 
                                                          dplyr::filter(!time %in% ..2, 
                                                                        !time %in% ..3, 
                                                                        !time %in% ..4) %>% 
                                                          dplyr::arrange(desc(strength)) %>% 
                                                          dplyr::slice(1) %>% 
                                                          dplyr::pull(time)
                                                      }                                                    
  ))

# Extract nodes (i.e. times) which connect to the 4th strongest node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_4th_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = fourth_attractor_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

# Save as a variable
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_4th_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_4th_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))

################### Make plots

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = 
                  purrr::map(.x = all_nodes_with_strengths,
                             .f = ~{
                               dplyr::mutate(.x,
                                             attractors = dplyr::case_when(
                                               strength == 0 ~ "Unique",
                                               connecting_to_strongest == TRUE ~ "1st",
                                               connecting_to_2nd_strongest == TRUE ~ "2nd",
                                               connecting_to_3rd_strongest == TRUE ~ "3rd",
                                               connecting_to_4th_strongest == TRUE ~ "4th",
                                               TRUE ~ "Uncategorised"),
                                             attractors = factor(attractors,
                                                                 levels = c("1st",
                                                                            "2nd",
                                                                            "3rd",
                                                                            "4th",
                                                                            "Uncategorised",
                                                                            "Unique")),
                                             name = factor(name,
                                                           levels = c("pleasure",
                                                                      "interest",
                                                                      "importance",
                                                                      "situation_requires",
                                                                      "anxiety_guilt_avoidance",
                                                                      "another_wants"),
                                                           labels = c("Pleasure",
                                                                      "Interest",
                                                                      "Importance",
                                                                      "Situation requires",
                                                                      "Anxiety guilt avoidance",
                                                                      "Another wants")) %>% 
                                               forcats::fct_drop()) %>% 
                                 dplyr::group_by(attractors, name) %>% 
                                 dplyr::mutate(n = n()) %>% 
                                 dplyr::ungroup() %>%
                                 dplyr::mutate(maxtime = max(time),
                                               percentage_of_total =
                                                 (n / maxtime) %>% scales::percent(accuracy = 0.1),
                                               proportion_of_total = n/maxtime,
                                               attractors_n = 
                                                 factor(paste0(attractors,
                                                               " (n = ", n, "; ", 
                                                               percentage_of_total, ")")))
                             }
                  ))

Spiral graph with colored nodes

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>%
  dplyr::mutate(node_colors = purrr::map(.x = all_nodes_with_strengths,
                                         .f = ~{tidyr::pivot_wider(.x, names_from = name) %>%
                                             dplyr::pull(attractors)}))

for (i in 1:nrow(emadata_nested_wrangled_both_recnets_nodes_plots)) {
levels(emadata_nested_wrangled_both_recnets_nodes_plots$node_colors[[i]]) <- 
  c(viridisLite::plasma(4, 
                      end = 0.8, 
                      direction = -1), "gray48", "white")
  }

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>% 
  dplyr::mutate(spiralgraph_epochs = purrr::pmap(list(..1 = graph_from_adjacency,
                                          ..2 = node_colors,
                                          ..3 = User),
                                     .f =  ~casnet::make_spiral_graph(g = ..1, 
                                                                      arcs = 4, 
                                                                      # a = .1, 
                                                                      # b = 2, 
                                                                      markTimeBy = TRUE,
                                                                      markEpochsBy = ..2,
                                                                      epochColours = ..2,
                                                                      showEpochLegend = FALSE,
                                                                      scaleEdgeSize = 1/10,
                                                                      scaleVertexSize = c(1, 5),
                                                                      showSizeLegend = FALSE,
                                                                      sizeLabel = "Node strength",
                                                                      type = "Euler",
                                                                      # alphaE = 0.1
                                                                      # title = ..3
                                     )))


# emadata_nested_wrangled_both_recnets_nodes_plots$spiralgraph_epochs[[1]] +
#   theme(plot.margin=grid::unit(c(0,0,0,0), "mm"))

# ggsave(filename = "./figures/recnetwork.png",
#        width = 7, 
#        height = 7)

# emadata_nested_wrangled_both_recnets_nodes_plots$all_nodes_with_strengths[[1]]

Attractor plot

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>%
  dplyr::mutate(observations = purrr::map_dbl(.x = sample2_standardised,
                                              .f = ~nrow(.)))

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>%
  dplyr::mutate(observations_daily = purrr::map_dbl(.x = data_daily,
                                              .f = ~nrow(.)))


emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>% 
  dplyr::mutate(
    attractor_plots = 
      purrr::pmap(list(..1 = all_nodes_with_strengths,
                       ..2 = observations,
                       ..3 = observations_daily,
                       ..4 = User),
                  .f = ~{
                    dplyr::mutate(..1,
                                  strength_rescaled = 
                                    scales::rescale(strength, to = c(0.3, 1.1)),
                                  alpha_strength = ifelse(strength_rescaled == 0.3, 
                                                          0.5, 
                                                          strength_rescaled)) %>% 
                      ggplot(data = .,
                             aes(x = forcats::fct_rev(name), 
                                 y = value, 
                                 size = strength_rescaled,
                                 alpha = alpha_strength,
                                 color = attractors_n)) +
                      scale_size_identity() +
                      scale_alpha_identity() +
                      geom_point(aes(alpha = alpha_strength)) +
                      geom_line(aes(group = time,
                                    alpha = alpha_strength)) +
                      scale_color_manual(values = c(viridisLite::plasma(4, 
                                                                        end = 0.8, 
                                                                        direction = -1), 
                                                    "gray40", "gray50")) +
                      scale_y_continuous(labels = scales::label_percent(accuracy = 1)) +
                      theme_bw() +
                      theme(legend.position = "none") +
                      labs(y = "Percentage of maximum reported value of variable, across full time series",
                           x = NULL,
                           title = paste0("Participant \"", ..4, "\" - based on ", ..2, " data points (", ..3, " days)")) +
                      facet_wrap(~attractors_n) +
                      coord_flip(ylim = c(0, 1))
                  }
      ))

emadata_nested_wrangled_both_recnets_nodes_plots$attractor_plots[[1]]

# emadata_nested_wrangled_both_recnets_nodes_plots$all_nodes_with_strengths[[1]]

3. Single randomly sampled daily observation

Here’s the 6-dimensional motivation system’s recurrence plot, weighted by similarity.

set.seed(100)

#######################

# si = similarity under the radius
emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled %>% 
  dplyr::mutate(RN = purrr::map(.x = sample3_standardised,
                                .f =  ~casnet::rn(.x %>% dplyr::select(#autonomy, competence, relatedness,
                                                                       pleasure, interest, importance,
                                                                       situation_requires, 
                                                                       anxiety_guilt_avoidance, 
                                                                       another_wants), 
                                                  doEmbed = FALSE, 
                                                  weighted = TRUE, 
                                                  weightedBy = "si", 
                                                  emRad = NA)))
## 
## Auto-recurrence: Setting diagonal to (1 + max. distance) for analyses
## 
## Searching for a radius that will yield 0.05 for RR

emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(graph_from_adjacency = purrr::map(.x = RN,
                                                  .f = ~igraph::graph_from_adjacency_matrix(.x, 
                                                                                            weighted = TRUE, 
                                                                                            mode = "upper", 
                                                                                            diag = FALSE)))

# Edges with their distances
emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(edges_with_distances = purrr::map(.x = graph_from_adjacency,
                                                  .f = ~igraph::E(.x)$weight),
                graph_from_adjacency_orig = graph_from_adjacency)

# Larger values are closer to the state; inverse of weight makes it more intuitive
for (i in 1:nrow(emadata_nested_wrangled_both_recnets)) {
igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$weight <- (1/emadata_nested_wrangled_both_recnets$edges_with_distances[[i]])
}
# A later note to self: Now weight is a measure of distance; how far apart two time points are
# (under the radius, i.e. they're reasonably similar to begin with)

####### To check:
# igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[1]])$weight
# igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency_orig[[1]])$weight

emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(RN_plot = purrr::map(.x = RN,
                                .f =  ~casnet::rn_plot(.x,
                                                       plotDimensions = TRUE,
                                                       xlab = "6-dimensional motivation system",
                                                       ylab = "6-dimensional motivation system")))


# Make node size equal to strength. Strength is the sum of a node's edge weights.
for (i in 1:nrow(emadata_nested_wrangled_both_recnets)) {
igraph::V(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$size <- (igraph::strength(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]]))
}

# Rescaling weight as "width"; varies between 5 and 10
for (i in 1:nrow(emadata_nested_wrangled_both_recnets)) {
igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$width <- 
  casnet::elascer(igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$weight, lo = 5, hi = 10)
}

The lengthy code chunk below extracts and marks attractors in the data.

# Get number of maximally connected node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(strongest_day = purrr::map(.x = graph_from_adjacency,
                                     .f =  ~which.max(igraph::strength(.x))
                                     ))



emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(list_of_edges = purrr::map(.x = graph_from_adjacency,
                                     .f =  ~igraph::get.data.frame(.x)
                                     ))

emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = 
                  purrr::map2(.x = sample3_standardised,
                              .y = graph_from_adjacency,
                              .f = ~{
                                data.frame(.x %>% 
                                             dplyr::select(#autonomy, competence, relatedness,
                                               pleasure, interest, importance,
                                               situation_requires, anxiety_guilt_avoidance, another_wants), 
                                           strength = igraph::strength(.y)) %>% 
                                  dplyr::mutate(time = dplyr::row_number()) %>% 
                                  tidyr::pivot_longer(cols = c(-strength, -time))
                              }
  ))

# Extract nodes (i.e. times) which connect to the strongest (i.e. most connected) node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = strongest_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))


# Get number of 2nd maximally connected node, which doesn't connect to the 1st
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(secondary_attractor_day = purrr::map2(.x = graph_from_adjacency,
                                                      .y = connecting_to_strongest,
                                                      .f =  ~{
                                                        data.frame(strength = igraph::strength(.x), 
                                                                   time = 1:length(igraph::strength(.x))) %>% 
                                                          dplyr::filter(!time %in% .y) %>% 
                                                          dplyr::arrange(desc(strength)) %>% 
                                                          dplyr::slice(1) %>% 
                                                          dplyr::pull(time)
                                                      }                                                    
  ))

# Extract nodes (i.e. times) which connect to the 2nd strongest node, which doesn't connect to the 1st
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_2nd_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = secondary_attractor_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

# Save as a variable in the dataset
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_2nd_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_2nd_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))

# Get number of 3rd maximally connected node, which doesn't connect to the 1st or second
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(tertiary_attractor_day = purrr::pmap(list(..1 = graph_from_adjacency,
                                                      ..2 = connecting_to_strongest,
                                                      ..3 = connecting_to_2nd_strongest),
                                                      .f =  ~{
                                                        data.frame(strength = igraph::strength(..1), 
                                                                   time = 1:length(igraph::strength(..1))) %>% 
                                                          dplyr::filter(!time %in% ..2, 
                                                                        !time %in% ..3) %>% 
                                                          dplyr::arrange(desc(strength)) %>% 
                                                          dplyr::slice(1) %>% 
                                                          dplyr::pull(time)
                                                      }                                                    
  ))

# Extract nodes (i.e. times) which connect to the 3rd strongest node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_3rd_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = tertiary_attractor_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

# Save as a variable
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_3rd_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_3rd_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))

# Get number of 4th maximally connected node, which doesn't connect to the 1st, 2nd, or 3rd
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(fourth_attractor_day = purrr::pmap(list(..1 = graph_from_adjacency,
                                                      ..2 = connecting_to_strongest,
                                                      ..3 = connecting_to_2nd_strongest,
                                                      ..4 = connecting_to_3rd_strongest),
                                                      .f =  ~{
                                                        data.frame(strength = igraph::strength(..1), 
                                                                   time = 1:length(igraph::strength(..1))) %>% 
                                                          dplyr::filter(!time %in% ..2, 
                                                                        !time %in% ..3, 
                                                                        !time %in% ..4) %>% 
                                                          dplyr::arrange(desc(strength)) %>% 
                                                          dplyr::slice(1) %>% 
                                                          dplyr::pull(time)
                                                      }                                                    
  ))

# Extract nodes (i.e. times) which connect to the 4th strongest node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_4th_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = fourth_attractor_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

# Save as a variable
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_4th_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_4th_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))

################### Make plots

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = 
                  purrr::map(.x = all_nodes_with_strengths,
                             .f = ~{
                               dplyr::mutate(.x,
                                             attractors = dplyr::case_when(
                                               strength == 0 ~ "Unique",
                                               connecting_to_strongest == TRUE ~ "1st",
                                               connecting_to_2nd_strongest == TRUE ~ "2nd",
                                               connecting_to_3rd_strongest == TRUE ~ "3rd",
                                               connecting_to_4th_strongest == TRUE ~ "4th",
                                               TRUE ~ "Uncategorised"),
                                             attractors = factor(attractors,
                                                                 levels = c("1st",
                                                                            "2nd",
                                                                            "3rd",
                                                                            "4th",
                                                                            "Uncategorised",
                                                                            "Unique")),
                                             name = factor(name,
                                                           levels = c("pleasure",
                                                                      "interest",
                                                                      "importance",
                                                                      "situation_requires",
                                                                      "anxiety_guilt_avoidance",
                                                                      "another_wants"),
                                                           labels = c("Pleasure",
                                                                      "Interest",
                                                                      "Importance",
                                                                      "Situation requires",
                                                                      "Anxiety guilt avoidance",
                                                                      "Another wants")) %>% 
                                               forcats::fct_drop()) %>% 
                                 dplyr::group_by(attractors, name) %>% 
                                 dplyr::mutate(n = n()) %>% 
                                 dplyr::ungroup() %>%
                                 dplyr::mutate(maxtime = max(time),
                                               percentage_of_total =
                                                 (n / maxtime) %>% scales::percent(accuracy = 0.1),
                                               proportion_of_total = n/maxtime,
                                               attractors_n = 
                                                 factor(paste0(attractors,
                                                               " (n = ", n, "; ", 
                                                               percentage_of_total, ")")))
                             }
                  ))

Spiral graph with colored nodes

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>%
  dplyr::mutate(node_colors = purrr::map(.x = all_nodes_with_strengths,
                                         .f = ~{tidyr::pivot_wider(.x, names_from = name) %>%
                                             dplyr::pull(attractors)}))

for (i in 1:nrow(emadata_nested_wrangled_both_recnets_nodes_plots)) {
levels(emadata_nested_wrangled_both_recnets_nodes_plots$node_colors[[i]]) <- 
  c(viridisLite::plasma(4, 
                      end = 0.8, 
                      direction = -1), "gray48", "white")
  }

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>% 
  dplyr::mutate(spiralgraph_epochs = purrr::pmap(list(..1 = graph_from_adjacency,
                                          ..2 = node_colors,
                                          ..3 = User),
                                     .f =  ~casnet::make_spiral_graph(g = ..1, 
                                                                      arcs = 4, 
                                                                      # a = .1, 
                                                                      # b = 2, 
                                                                      markTimeBy = TRUE,
                                                                      markEpochsBy = ..2,
                                                                      epochColours = ..2,
                                                                      showEpochLegend = FALSE,
                                                                      scaleEdgeSize = 1/10,
                                                                      scaleVertexSize = c(1, 5),
                                                                      showSizeLegend = FALSE,
                                                                      sizeLabel = "Node strength",
                                                                      type = "Euler",
                                                                      # alphaE = 0.1
                                                                      # title = ..3
                                     )))


# emadata_nested_wrangled_both_recnets_nodes_plots$spiralgraph_epochs[[1]] +
#   theme(plot.margin=grid::unit(c(0,0,0,0), "mm"))

# ggsave(filename = "./figures/recnetwork.png",
#        width = 7, 
#        height = 7)

# emadata_nested_wrangled_both_recnets_nodes_plots$all_nodes_with_strengths[[1]]

Attractor plot

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>%
  dplyr::mutate(observations = purrr::map_dbl(.x = sample3_standardised,
                                              .f = ~nrow(.)))

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>%
  dplyr::mutate(observations_daily = purrr::map_dbl(.x = data_daily,
                                              .f = ~nrow(.)))


emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>% 
  dplyr::mutate(
    attractor_plots = 
      purrr::pmap(list(..1 = all_nodes_with_strengths,
                       ..2 = observations,
                       ..3 = observations_daily,
                       ..4 = User),
                  .f = ~{
                    dplyr::mutate(..1,
                                  strength_rescaled = 
                                    scales::rescale(strength, to = c(0.3, 1.1)),
                                  alpha_strength = ifelse(strength_rescaled == 0.3, 
                                                          0.5, 
                                                          strength_rescaled)) %>% 
                      ggplot(data = .,
                             aes(x = forcats::fct_rev(name), 
                                 y = value, 
                                 size = strength_rescaled,
                                 alpha = alpha_strength,
                                 color = attractors_n)) +
                      scale_size_identity() +
                      scale_alpha_identity() +
                      geom_point(aes(alpha = alpha_strength)) +
                      geom_line(aes(group = time,
                                    alpha = alpha_strength)) +
                      scale_color_manual(values = c(viridisLite::plasma(4, 
                                                                        end = 0.8, 
                                                                        direction = -1), 
                                                    "gray40", "gray50")) +
                      scale_y_continuous(labels = scales::label_percent(accuracy = 1)) +
                      theme_bw() +
                      theme(legend.position = "none") +
                      labs(y = "Percentage of maximum reported value of variable, across full time series",
                           x = NULL,
                           title = paste0("Participant \"", ..4, "\" - based on ", ..2, " data points (", ..3, " days)")) +
                      facet_wrap(~attractors_n) +
                      coord_flip(ylim = c(0, 1))
                  }
      ))

emadata_nested_wrangled_both_recnets_nodes_plots$attractor_plots[[1]]

# emadata_nested_wrangled_both_recnets_nodes_plots$all_nodes_with_strengths[[1]]

All data

Here’s the 6-dimensional motivation system’s recurrence plot, weighted by similarity.

set.seed(1)

#######################

# si = similarity under the radius
emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled %>% 
  dplyr::mutate(RN = purrr::map(.x = data_standardised,
                                .f =  ~casnet::rn(.x %>% dplyr::select(#autonomy, competence, relatedness,
                                                                       pleasure, interest, importance,
                                                                       situation_requires, 
                                                                       anxiety_guilt_avoidance, 
                                                                       another_wants), 
                                                  doEmbed = FALSE, 
                                                  weighted = TRUE, 
                                                  weightedBy = "si", 
                                                  emRad = NA)))
## 
## Auto-recurrence: Setting diagonal to (1 + max. distance) for analyses
## 
## Searching for a radius that will yield 0.05 for RR

emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(graph_from_adjacency = purrr::map(.x = RN,
                                                  .f = ~igraph::graph_from_adjacency_matrix(.x, 
                                                                                            weighted = TRUE, 
                                                                                            mode = "upper", 
                                                                                            diag = FALSE)))

# Edges with their distances
emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(edges_with_distances = purrr::map(.x = graph_from_adjacency,
                                                  .f = ~igraph::E(.x)$weight),
                graph_from_adjacency_orig = graph_from_adjacency)

# Larger values are closer to the state; inverse of weight makes it more intuitive
for (i in 1:nrow(emadata_nested_wrangled_both_recnets)) {
igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$weight <- (1/emadata_nested_wrangled_both_recnets$edges_with_distances[[i]])
}
# A later note to self: Now weight is a measure of distance; how far apart two time points are
# (under the radius, i.e. they're reasonably similar to begin with)

####### To check:
# igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[1]])$weight
# igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency_orig[[1]])$weight

emadata_nested_wrangled_both_recnets <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(RN_plot = purrr::map(.x = RN,
                                .f =  ~casnet::rn_plot(.x,
                                                       plotDimensions = TRUE,
                                                       xlab = "6-dimensional motivation system",
                                                       ylab = "6-dimensional motivation system")))


# Make node size equal to strength. Strength is the sum of a node's edge weights.
for (i in 1:nrow(emadata_nested_wrangled_both_recnets)) {
igraph::V(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$size <- (igraph::strength(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]]))
}

# Rescaling weight as "width"; varies between 5 and 10
for (i in 1:nrow(emadata_nested_wrangled_both_recnets)) {
igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$width <- 
  casnet::elascer(igraph::E(emadata_nested_wrangled_both_recnets$graph_from_adjacency[[i]])$weight, lo = 5, hi = 10)
}

The lengthy code chunk below extracts and marks attractors in the data.

# Get number of maximally connected node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets %>% 
  dplyr::mutate(strongest_day = purrr::map(.x = graph_from_adjacency,
                                     .f =  ~which.max(igraph::strength(.x))
                                     ))



emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(list_of_edges = purrr::map(.x = graph_from_adjacency,
                                     .f =  ~igraph::get.data.frame(.x)
                                     ))

emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = 
                  purrr::map2(.x = data_standardised,
                              .y = graph_from_adjacency,
                              .f = ~{
                                data.frame(.x %>% 
                                             dplyr::select(#autonomy, competence, relatedness,
                                               pleasure, interest, importance,
                                               situation_requires, anxiety_guilt_avoidance, another_wants), 
                                           strength = igraph::strength(.y)) %>% 
                                  dplyr::mutate(time = dplyr::row_number()) %>% 
                                  tidyr::pivot_longer(cols = c(-strength, -time))
                              }
  ))

# Extract nodes (i.e. times) which connect to the strongest (i.e. most connected) node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = strongest_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))


# Get number of 2nd maximally connected node, which doesn't connect to the 1st
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(secondary_attractor_day = purrr::map2(.x = graph_from_adjacency,
                                                      .y = connecting_to_strongest,
                                                      .f =  ~{
                                                        data.frame(strength = igraph::strength(.x), 
                                                                   time = 1:length(igraph::strength(.x))) %>% 
                                                          dplyr::filter(!time %in% .y) %>% 
                                                          dplyr::arrange(desc(strength)) %>% 
                                                          dplyr::slice(1) %>% 
                                                          dplyr::pull(time)
                                                      }                                                    
  ))

# Extract nodes (i.e. times) which connect to the 2nd strongest node, which doesn't connect to the 1st
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_2nd_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = secondary_attractor_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

# Save as a variable in the dataset
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_2nd_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_2nd_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))

# Get number of 3rd maximally connected node, which doesn't connect to the 1st or second
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(tertiary_attractor_day = purrr::pmap(list(..1 = graph_from_adjacency,
                                                      ..2 = connecting_to_strongest,
                                                      ..3 = connecting_to_2nd_strongest),
                                                      .f =  ~{
                                                        data.frame(strength = igraph::strength(..1), 
                                                                   time = 1:length(igraph::strength(..1))) %>% 
                                                          dplyr::filter(!time %in% ..2, 
                                                                        !time %in% ..3) %>% 
                                                          dplyr::arrange(desc(strength)) %>% 
                                                          dplyr::slice(1) %>% 
                                                          dplyr::pull(time)
                                                      }                                                    
  ))

# Extract nodes (i.e. times) which connect to the 3rd strongest node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_3rd_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = tertiary_attractor_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

# Save as a variable
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_3rd_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_3rd_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))

# Get number of 4th maximally connected node, which doesn't connect to the 1st, 2nd, or 3rd
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(fourth_attractor_day = purrr::pmap(list(..1 = graph_from_adjacency,
                                                      ..2 = connecting_to_strongest,
                                                      ..3 = connecting_to_2nd_strongest,
                                                      ..4 = connecting_to_3rd_strongest),
                                                      .f =  ~{
                                                        data.frame(strength = igraph::strength(..1), 
                                                                   time = 1:length(igraph::strength(..1))) %>% 
                                                          dplyr::filter(!time %in% ..2, 
                                                                        !time %in% ..3, 
                                                                        !time %in% ..4) %>% 
                                                          dplyr::arrange(desc(strength)) %>% 
                                                          dplyr::slice(1) %>% 
                                                          dplyr::pull(time)
                                                      }                                                    
  ))

# Extract nodes (i.e. times) which connect to the 4th strongest node
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(connecting_to_4th_strongest = purrr::map2(.x = list_of_edges,
                                                      .y = fourth_attractor_day,
                                                      .f = ~{
                                                        .x %>% dplyr::filter(from == .y | to == .y) %>% 
                                                          dplyr::arrange(weight) %>% 
                                                          tidyr::pivot_longer(cols = c(from, to),
                                                                              values_to = "node") %>% 
                                                          dplyr::distinct(node, 
                                                                          #.keep_all = TRUE
                                                          ) %>% 
                                                          dplyr::pull(node)
                                                      }
                                                      )
  )

# Save as a variable
emadata_nested_wrangled_both_recnets_nodes <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = purrr::map2(.x = all_nodes_with_strengths,
                                                       .y = connecting_to_4th_strongest,
                                                       .f = ~{
                                                         dplyr::mutate(.x,
                                                                       connecting_to_4th_strongest = 
                                                                         dplyr::case_when(time %in% .y ~ TRUE,
                                                                                          TRUE ~ FALSE))
                                                       }
  ))

################### Make plots

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes %>% 
  dplyr::mutate(all_nodes_with_strengths = 
                  purrr::map(.x = all_nodes_with_strengths,
                             .f = ~{
                               dplyr::mutate(.x,
                                             attractors = dplyr::case_when(
                                               strength == 0 ~ "Unique",
                                               connecting_to_strongest == TRUE ~ "1st",
                                               connecting_to_2nd_strongest == TRUE ~ "2nd",
                                               connecting_to_3rd_strongest == TRUE ~ "3rd",
                                               connecting_to_4th_strongest == TRUE ~ "4th",
                                               TRUE ~ "Uncategorised"),
                                             attractors = factor(attractors,
                                                                 levels = c("1st",
                                                                            "2nd",
                                                                            "3rd",
                                                                            "4th",
                                                                            "Uncategorised",
                                                                            "Unique")),
                                             name = factor(name,
                                                           levels = c("pleasure",
                                                                      "interest",
                                                                      "importance",
                                                                      "situation_requires",
                                                                      "anxiety_guilt_avoidance",
                                                                      "another_wants"),
                                                           labels = c("Pleasure",
                                                                      "Interest",
                                                                      "Importance",
                                                                      "Situation requires",
                                                                      "Anxiety guilt avoidance",
                                                                      "Another wants")) %>% 
                                               forcats::fct_drop()) %>% 
                                 dplyr::group_by(attractors, name) %>% 
                                 dplyr::mutate(n = n()) %>% 
                                 dplyr::ungroup() %>%
                                 dplyr::mutate(maxtime = max(time),
                                               percentage_of_total =
                                                 (n / maxtime) %>% scales::percent(accuracy = 0.1),
                                               proportion_of_total = n/maxtime,
                                               attractors_n = 
                                                 factor(paste0(attractors,
                                                               " (n = ", n, "; ", 
                                                               percentage_of_total, ")")))
                             }
                  ))

Spiral graph with colored nodes

emadata_nested_wrangled_both_recnets_nodes_plots <- emadata_nested_wrangled_both_recnets_nodes_plots %>%
  dplyr::mutate(node_colors = purrr::map(.x = all_nodes_with_strengths,
                                         .f = ~{tidyr::pivot_wider(.x, names_from = name) %>%
                                             dplyr::pull(attractors)}))