Primo metodo

Supponiamo di avere calcolato i sottomovimenti dell’indice e del pollice ((as esempio, di tipo 1, le variazioni di velocita)), e che li abbiamo categorizzati con 0 (assenza di sottomovimenti) e con 1 (presenza dei sottomovimenti).

La domanda della ricerca potrebbe essere: Dato un sottomovimento del pollice (ad esempio) si verifica anche un sottomovimento dell’indice? I sottomovimenti dell’indice di verificano approssimativamente nella stessa finestra temporale dei sottomovimenti del pollice (o viceversa)?

Questo è il dataframe.

library(collapse)
library(dplyr)
library(tidyverse)
library(gridExtra)
library(zoo)
dat<- data.frame (
  'sub'   = rep (c(1,2), each = 1000),
  'trial' = rep(c(1,2), each = 500),
  'Thumb'=  rbinom(2000, 1, 0.1) ,
  'Index'=  rbinom(2000, 1, 0.1))

Creo una funzione che mi individua “Thumb = 1” (presenza del sottomovimento) e mi calcola per la variaible Index 5 finestre temporali gap1 = n-1, n, n+1 gap2 = n-2, n+1, n, n-1, n-2 e cosi via.

f<- function(df, n){
  df %>%
    collapse::flag(-n:n) %>%
    rowSums(na.rm = T) - 1
  }

library(dplyr)
a<-dat %>% 
  group_by(trial, sub) %>% 
  group_modify(~ .x %>% 
  mutate(gap1 = f(.x, 1), gap2 = f(.x, 2), 
  gap3 = f(.x, 3), gap4 = f(.x, 4), gap5 = f(.x, 5)))  %>% filter(Thumb == 1) %>% ungroup
a
## # A tibble: 200 × 9
##    trial   sub Thumb Index  gap1  gap2  gap3  gap4  gap5
##    <dbl> <dbl> <int> <int> <dbl> <dbl> <dbl> <dbl> <dbl>
##  1     1     1     1     0     0     0     0     0     0
##  2     1     1     1     1     1     1     2     2     3
##  3     1     1     1     0     0     1     1     1     1
##  4     1     1     1     0     1     2     2     2     2
##  5     1     1     1     0     0     0     0     0     1
##  6     1     1     1     0     0     0     0     0     1
##  7     1     1     1     0     0     1     1     1     1
##  8     1     1     1     0     1     1     1     1     2
##  9     1     1     1     0     1     1     1     2     2
## 10     1     1     1     0     0     1     2     2     2
## # … with 190 more rows

Calcolo la proporzione fra il numero delle volte che indice e pollice presentano lo stesso numero di sottomovimenti e la somma delle le volte che presentano lo stesso numero di sottomovimenti e le volte che hanno un numero diverso di sottomovimenti. (Questa formula è migliorabile) :)

f <- function(a,b,c,d,e,f,g) {
  Sync = b == a
  TotSync = length(Sync[Sync == TRUE])
  Sub = b == 0 | b != 0
  TotSub = length(Sub[Sub == TRUE])
  Sync = TotSync/TotSub
  
  Sync1 = c == a
  TotSync1 = length(Sync1[Sync1 == TRUE])
  Sub1 = c == 0 | c != 0
  TotSub1 = length(Sub1[Sub1 == TRUE])
  Sync1 = TotSync1/TotSub1
  
  Sync2 = d == a
  TotSync2 = length(Sync2[Sync2 == TRUE])
  Sub2 = d == 0 | d != 0
  TotSub2 = length(Sub2[Sub2 == TRUE])
  Sync2 = TotSync2/TotSub2
  
  Sync3 = e == a
  TotSync3 = length(Sync3[Sync3 == TRUE])
  Sub3 = e == 0 | e != 0
  TotSub3 = length(Sub3[Sub3 == TRUE])
  Sync3 = TotSync3/TotSub3
  
  Sync4 = f == a
  TotSync4 = length(Sync4[Sync4 == TRUE])
  Sub4 = f == 0 | f != 0
  TotSub4 = length(Sub4[Sub4 == TRUE])
  Sync4 = TotSync4/TotSub4
  
  Sync5 = g == a
  TotSync5 = length(Sync5[Sync5 == TRUE])
  Sub5 = g == 0 | g != 0
  TotSub5 = length(Sub5[Sub5 == TRUE])
  Sync5 = TotSync5/TotSub5
  
  
  return(c(Sync,Sync1,Sync2,Sync3,Sync4,Sync5))
}

Calcolo f per ogni condizione:

a %>%
  group_by(trial,sub) %>%
  summarize(as.data.frame(matrix(f(Index, Thumb,gap1,  gap2,  gap3,  gap4,  gap5), nr = 1)))
## `summarise()` has grouped output by 'trial'. You can override using the `.groups` argument.
## # A tibble: 4 × 8
## # Groups:   trial [2]
##   trial   sub     V1    V2    V3    V4    V5    V6
##   <dbl> <dbl>  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1     1     1 0.0980 0.627 0.431 0.294 0.255 0.137
## 2     1     2 0.125  0.667 0.438 0.188 0.125 0.104
## 3     2     1 0.0435 0.609 0.522 0.348 0.261 0.196
## 4     2     2 0.0545 0.673 0.491 0.327 0.182 0.109

Ho provato anche altri metodi. Ad esempio in questo calcolo delle finestre sia per thumb che per index.

dat<- data.frame (
  'Sub'   = rep (1,2, each = 1000),
  'Trial' = rep(1,2, each = 500),
  'Thumb'=  rbinom(2000, 1, 0.5) ,
  'Index'=  rbinom(2000, 1, 0.5))

roll<-function(x,lags){
  if (length(x)<lags) {
    tmp=c(rep(NA,length(x)))  
  }
  else {
    tmp=rollsum(x, lags, align = "right", fill = NA)
  }
  tmp=as.numeric(tmp)
  return(tmp)
}

tmp1 <- as.data.frame(dat %>% 
  group_by(Trial, Sub) %>%
  mutate(
    IndexLag1 = ave(Index, Trial, FUN = function(x) roll(x, 2)),
    ThumbLag1 = ave(Thumb, Trial, FUN = function(x) roll(x, 2)),
    
    
    IndexLag2 = ave(Index, Trial, FUN = function(x) roll(x, 3)),
    ThumbLag2 = ave(Thumb, Trial, FUN = function(x) roll(x, 3)),
    
    
    IndexLag3 = ave(Index, Trial, FUN = function(x) roll(x, 4)),
    ThumbLag3 = ave(Thumb, Trial, FUN = function(x) roll(x, 4)),
    
    IndexLag4 = ave(Index, Trial, FUN = function(x) roll(x, 4)),
    ThumbLag4 = ave(Thumb, Trial, FUN = function(x) roll(x, 4))))

Poi si applica f.