SOCR ≫ TCIU Website ≫ TCIU GitHub ≫

1 Spacetime IID vs. Spacekime Sampling

For all natural spacetime processes, various population characteristics like the mean, variance, range, and quantiles can be estimated by collecting independent and identically distributed (IID) samples. These samples represent observed data that is traditionally used to obtain sample-driven estimates of the specific population characteristics via standard formulas like the sample arithmetic average, variance, range, quantiles, etc. The latter approximate their population counterparts and form the basis for classical parametric and non-parametric statistical inference.

Typically, reliable spacetime statistical inference is conditional on the distribution of the native process as well as a sample-size reflecting the characteristics of the phenomenon. We will demonstrate that spacekime analytics can be equally effective with measuring a single spacetime observation and having a reasonable estimates of the unobserved process kime-phases.

Without loss of generality, suppose we have a pair of cohorts \(A\) and \(B\) and we obtain a series of measurements \(\{X_{A,i}\}_{i=1}^{n_A}\) and \(\{X_{B,i}\}_{i=1}^{n_B}\), respectively. Obviously the relations between the cohorts could widely vary, from being samples of the same process, to being related or completely independent.

To allow us to examine the extreme cases of pairing (1) IID cohorts (\(A\) and \(B\)), and (2) independent but differently distributed cohorts (\(A\) and \(C\)). The latter case may be thought of as a split of the first cohort (\(A\)) into training (\(C\)) and testing (\(D\)) groups. This design allows us to compare the classical spacetime-derived population characteristics of cohort \(A\) to their spacekime-reconstructed counterparts obtained using a single random kime-magnitude observation from \(A\) and kime-phases estimates derived from cohorts \(B\), \(C\) or \(D\).

1.1 fMRI timeseries

The demonstration below is based on a functional magnetic resonance imaging (fMRI) data, which is a 4D hypervolume with intensities representing the blood oxygenation level dependence at a specific spacetime location \((x,y,z,t)\). For simplicity, we will only focus on two fixed spatial locations with varying intensity distributions.

## NIfTI-1 format
##   Type            : nifti
##   Data Type       : 4 (INT16)
##   Bits per Pixel  : 16
##   Slice Code      : 0 (Unknown)
##   Intent Code     : 0 (None)
##   Qform Code      : 1 (Scanner_Anat)
##   Sform Code      : 0 (Unknown)
##   Dimension       : 64 x 64 x 21 x 180
##   Pixel Dimension : 4 x 4 x 6 x 3
##   Voxel Units     : mm
##   Time Units      : sec
## [1]  64  64  21 180
## [1] 180

## [1] 180

Clearly the intensities of cohorts \(A\) and \(B\) are independent and follow different distribution. We’ll split the first cohort (\(A\)) into training (\(C\)) and testing (\(D\)) subgroups. Then we will:

  • transform all four cohorts into Fourier k-space,
  • iteratively randomly sample single observations from cohort \(C\),
  • reconstruct the data into spacetime using alternative kime-phase estimates derived from cohorts \(B\) and \(D\), and
  • compute and compare the classical spacetime-derived population characteristics of cohort \(A\) to their counterparts obtained using a single \(C\) kime-radial measurements paired with \(B\) and \(D\) kime-phases.
# Generic function to Transform Data to/from k-space (Space/Fourier domain)
kSpaceTransform <- function(data, inverse = FALSE, reconPhases = NULL) {
  # ForwardFT (rawData, FALSE, NULL)
  # InverseFT(magnitudes, TRUE, reconPhasesToUse) or InverseFT(FT_data, TRUE, NULL)
  FT_data <- array(complex(), length(data))
  mag_FT_data <- array(complex(), length(data))
  phase_FT_data <- array(complex(), length(data))
  IFT_reconPhases_data <- array(complex(), length(data))

  if (inverse == FALSE | is.null(reconPhases)) {
      FT_data <- fft(data, inverse)
      X2 <- FT_data
      mag_FT_data <- sqrt(Re(X2)^2+Im(X2)^2) 
      phase_FT_data <- atan2(Im(X2), Re(X2)) 
  }
  else {  # for IFT synthesis using user-provided Phases, typically from kime-phase aggregators
      Real <- data * cos(reconPhases)  
      Imaginary <- data * sin(reconPhases) 
      IFT_reconPhases_data <- 
          Re(fft(Real+1i*Imaginary, inverse = TRUE)/length(data))
  }

    ######### Test the FT-IFT analysis-synthesis back-and-forth transform process 
    #         to confirm calculations
    # X2 <- FT_data[ , 1]; mag_FT_data[ , 1] <- sqrt(Re(X2)^2+Im(X2)^2); 
    # phase_FT_data[ , 1] <- atan2(Im(X2), Re(X2)); 
    # Real2 = mag_FT_data[ , 1] * cos(phase_FT_data[ , 1])
    # Imaginary2 = mag_FT_data[ , 1] * sin(phase_FT_data[ , 1])
    # man_hat_X2 = Re(fft(Real2 + 1i*Imaginary2, inverse = T)/length(X2))
    # ifelse(abs(man_hat_X2[5] - data[5, 1]) < 0.001, "Perfect Syntesis", "Problems!!!")
    #########
  
    if (inverse == FALSE | is.null(reconPhases)) {
      return(list("magnitudes"=mag_FT_data, "phases"=phase_FT_data))
      # Use kSpaceTransform$magnitudes & kSpaceTransform$phases to retrieve teh Mags and Phases
    }
    else {
      return(IFT_reconPhases_data)
      # Use Re(kSpaceTransform) to extract spacetime Real-valued reconstructed data
    }
}

# 1. Split the first cohort ($A$) into *training* ($C$) and *testing* ($D$) subgroups.
subset_int <- sample(length(xA_df$value),floor(length(xA_df$value)*0.8))  
# 80% training + 20% testing
xC_fMRI_train <- xA_df$value [subset_int]; length(xC_fMRI_train) # 144
## [1] 144
## [1] 36
## [1] 180
## [1] 180
## [1] 10620 10483 10519 10661 10598 10780
## [1] 10620 10483 10519 10661 10598 10780
## [1] 144  30
## [1] TRUE TRUE TRUE TRUE TRUE TRUE
## [1] TRUE
##        V1          
##  Min.   :-2.27528  
##  1st Qu.:-0.72802  
##  Median :-0.01324  
##  Mean   : 0.00000  
##  3rd Qu.: 0.69867  
##  Max.   : 3.53484
##        V1         
##  Min.   :-2.3609  
##  1st Qu.:-0.6187  
##  Median : 0.0327  
##  Mean   : 0.0000  
##  3rd Qu.: 0.7080  
##  Max.   : 3.4877
##        V1          
##  Min.   :-3.32577  
##  1st Qu.:-0.56299  
##  Median :-0.03926  
##  Mean   : 0.00000  
##  3rd Qu.: 0.60848  
##  Max.   : 2.56786
##        V1          
##  Min.   :-2.72484  
##  1st Qu.:-0.62653  
##  Median : 0.04799  
##  Mean   : 0.00000  
##  3rd Qu.: 0.68595  
##  Max.   : 2.43560

1.1.1 Data Visualization

## Warning: Removed 180 rows containing non-finite values (stat_density).

## [1] 144
## [1] 144  30
## [1] 144  30
## [1] 144  30
# Compute the averages accross all N=30 experiments for the B, C & D reconstructions
ift_ft_xC_fMRI_1sampleN_PhaseB_avg <- apply(ift_ft_xC_fMRI_1sampleN_PhaseB, 1, mean)
ift_ft_xC_fMRI_1sampleN_PhaseC_avg <- apply(ift_ft_xC_fMRI_1sampleN_PhaseC, 1, mean)
ift_ft_xC_fMRI_1sampleN_PhaseD_avg <- apply(ift_ft_xC_fMRI_1sampleN_PhaseD, 1, mean)

# Plot 4 density curves (orig=xC_fMRI and 3 reconstructions from B, C and D)
xC_fMRI_train_scale_df <- as.data.frame(scale(xC_fMRI_train))
colnames(xC_fMRI_train_scale_df) <- "value"; xC_fMRI_train_scale_df$series <- "xC_fMRI_original"

ift_ft_xC_fMRI_1sampleN_PhaseB_avg_scale_df <- as.data.frame(scale(ift_ft_xC_fMRI_1sampleN_PhaseB_avg))
colnames(ift_ft_xC_fMRI_1sampleN_PhaseB_avg_scale_df) <- "value"
ift_ft_xC_fMRI_1sampleN_PhaseB_avg_scale_df$series <- "SK_PhaseB"

ift_ft_xC_fMRI_1sampleN_PhaseC_avg_scale_df <- as.data.frame(scale(ift_ft_xC_fMRI_1sampleN_PhaseC_avg))
colnames(ift_ft_xC_fMRI_1sampleN_PhaseC_avg_scale_df) <- "value"
ift_ft_xC_fMRI_1sampleN_PhaseC_avg_scale_df$series <- "SK_PhaseC"

ift_ft_xC_fMRI_1sampleN_PhaseD_avg_scale_df <- as.data.frame(scale(ift_ft_xC_fMRI_1sampleN_PhaseD_avg))
colnames(ift_ft_xC_fMRI_1sampleN_PhaseD_avg_scale_df) <- "value"
ift_ft_xC_fMRI_1sampleN_PhaseD_avg_scale_df$series <- "SK_PhaseD"

# and combine into your new data frame vegLengths
xC_fMRI_SK_Phases_B_C_D_df <- rbind(xC_fMRI_train_scale_df, ift_ft_xC_fMRI_1sampleN_PhaseB_avg_scale_df, 
             ift_ft_xC_fMRI_1sampleN_PhaseC_avg_scale_df, ift_ft_xC_fMRI_1sampleN_PhaseD_avg_scale_df)

# library(ggplot2)
ggplot(xC_fMRI_SK_Phases_B_C_D_df, aes(value, fill = series)) + 
  geom_density(aes(color=series, linetype = series), alpha=0.4, size=1.2) +  # position = "stack"
  theme(text = element_text(size=20)) +
  scale_fill_manual( values = c("yellow", "red", "blue", "green")) +
  geom_line(data=xC_fMRI_train_scale_df, stat = "density", color="purple", lty=4, lwd=2) +
  ## guides(color = guide_legend(order=1)) +
  theme(axis.title.x=element_blank(),axis.text.x=element_blank(), axis.ticks.x=element_blank())

1.2 IID Simulation

The following simulation example generates two mixture distribution random samples each of \(n=10,000\) observations, \(\{X_{A,i}\}_{i=1}^{n_A}\), where \(X_{A,i} = 0.3U_i + 0.7V_i\), \(U_i \sim N(0,1)\) and \(V_i \sim N(5,3)\), and \(\{X_{B,i}\}_{i=1}^{n_B}\), where \(X_{B,i} = 0.4P_i + 0.6Q_i\), \(P_i \sim N(20,20)\) and \(Q_i \sim N(100,30)\).

## [1] 10000

## [1] 10000

Figure 4.1

## quartz_off_screen 
##                 2

Clearly the intensities of cohorts \(A\) and \(B\) are independent and follow different distributions. We’ll split the first cohort (\(A\)) into training (\(C\)) and testing (\(D\)) subgroups, and then:

  • transform all four cohorts into Fourier k-space,
  • iteratively randomly sample single observations from cohort \(C\),
  • reconstruct the data into spacetime using a single kime-magnitude value and alternative kime-phase estimates derived from cohorts \(B\), \(C\), and \(D\), and
  • compute and compare the classical spacetime-derived population characteristics of cohort \(A\) to their spacekime counterparts obtained using a single \(C\) kime-magnitude paired with \(B\), \(C\), or \(D\) kime-phases.
# Generic function to Transform 1D Data to/from k-space (Space/Fourier domain)
kSpaceTransform <- function(data, inverse = FALSE, reconPhases = NULL) {
  # ForwardFT (rawData, FALSE, NULL)
  # InverseFT(magnitudes, TRUE, reconPhasesToUse) or InverseFT(FT_data, TRUE, NULL)
  FT_data <- array(complex(), length(data))
  mag_FT_data <- array(complex(), length(data))
  phase_FT_data <- array(complex(), length(data))
  IFT_reconPhases_data <- array(complex(), length(data))

  if (inverse == FALSE | is.null(reconPhases)) {
      FT_data <- fft(data, inverse)
      X2 <- FT_data
      mag_FT_data <- sqrt(Re(X2)^2+Im(X2)^2) 
      phase_FT_data <- atan2(Im(X2), Re(X2)) 
  }
  else {  # for IFT synthesis using user-provided Phases, typically from kime-phase aggregators
      Real <- data * cos(reconPhases)  
      Imaginary <- data * sin(reconPhases) 
      IFT_reconPhases_data <- 
          Re(fft(Real+1i*Imaginary, inverse = TRUE)/length(data))
  }

    ######### Test the FT-IFT analysis-synthesis back-and-forth transform process 
    #         to confirm calculations
    # X2 <- FT_data[ , 1]; mag_FT_data[ , 1] <- sqrt(Re(X2)^2+Im(X2)^2); 
    # phase_FT_data[ , 1] <- atan2(Im(X2), Re(X2)); 
    # Real2 = mag_FT_data[ , 1] * cos(phase_FT_data[ , 1])
    # Imaginary2 = mag_FT_data[ , 1] * sin(phase_FT_data[ , 1])
    # man_hat_X2 = Re(fft(Real2 + 1i*Imaginary2, inverse = T)/length(X2))
    # ifelse(abs(man_hat_X2[5] - data[5, 1]) < 0.001, "Perfect Syntesis", "Problems!!!")
    #########
  
    if (inverse == FALSE | is.null(reconPhases)) {
      return(list("magnitudes"=mag_FT_data, "phases"=phase_FT_data))
      # Use kSpaceTransform$magnitudes & kSpaceTransform$phases to retrieve teh Mags and Phases
    }
    else {
      return(IFT_reconPhases_data)
      # Use Re(kSpaceTransform) to extract spacetime Real-valued reconstructed data
    }
}

# 1. Split the first cohort ($A$) into *training* ($C$) and *testing* ($D$) subgroups.
subset_int <- sample(length(xA_df$value),floor(length(xA_df$value)*0.8))  
# 80% training + 20% testing
xC <- xA_df$value [subset_int]; length(xC) # 8000
## [1] 8000
## [1] 2000
## [1]  1.3788028  5.8322877  8.2533235 -2.0370931  0.4291247  6.5181677
## [1]  1.3788028  5.8322877  8.2533235 -2.0370931  0.4291247  6.5181677
## [1] 8000   30
## [1] -2.664535e-15 -1.554312e-15 -4.440892e-16 -5.773160e-15  0.000000e+00
## [6] -8.881784e-16
## [1] TRUE
##        V1          
##  Min.   :-2.38784  
##  1st Qu.:-0.88609  
##  Median :-0.03893  
##  Mean   : 0.00000  
##  3rd Qu.: 0.75821  
##  Max.   : 3.59925
##        V1          
##  Min.   :-2.52901  
##  1st Qu.:-0.76221  
##  Median :-0.05584  
##  Mean   : 0.00000  
##  3rd Qu.: 0.72999  
##  Max.   : 3.73114
##        V1           
##  Min.   :-3.798440  
##  1st Qu.:-0.636799  
##  Median : 0.009279  
##  Mean   : 0.000000  
##  3rd Qu.: 0.645119  
##  Max.   : 3.986702
##        V1          
##  Min.   :-2.66007  
##  1st Qu.:-0.79651  
##  Median :-0.08165  
##  Mean   : 0.00000  
##  3rd Qu.: 0.73477  
##  Max.   : 3.39448

1.2.1 Data Visualization

## [1] 8000
## [1] 8000   30
## [1] 8000   30
## [1] 8000   30
# Compute the averages accross all N=30 experiments for the B, C & D spacekime (IFT) reconstructions
ift_ft_xC_1sampleN_PhaseB_avg <- apply(ift_ft_xC_1sampleN_PhaseB, 1, mean)
ift_ft_xC_1sampleN_PhaseC_avg <- apply(ift_ft_xC_1sampleN_PhaseC, 1, mean)
ift_ft_xC_1sampleN_PhaseD_avg <- apply(ift_ft_xC_1sampleN_PhaseD, 1, mean)

# Plot 4 density curves (orig=xC and 3 reconstructions from B, C and D)
xC_scale_df <- as.data.frame(xC)
colnames(xC_scale_df) <- "value"; xC_scale_df$series <- "xC_original"

ift_ft_xC_1sampleN_PhaseB_avg_scale_df <- 
    as.data.frame(ift_ft_xC_1sampleN_PhaseB_avg)
colnames(ift_ft_xC_1sampleN_PhaseB_avg_scale_df) <- "value"
ift_ft_xC_1sampleN_PhaseB_avg_scale_df$series <- "SK_PhaseB"

ift_ft_xC_1sampleN_PhaseC_avg_scale_df <- 
    as.data.frame(ift_ft_xC_1sampleN_PhaseC_avg)
colnames(ift_ft_xC_1sampleN_PhaseC_avg_scale_df) <- "value"
ift_ft_xC_1sampleN_PhaseC_avg_scale_df$series <- "SK_PhaseC"

ift_ft_xC_1sampleN_PhaseD_avg_scale_df <- 
      as.data.frame(ift_ft_xC_1sampleN_PhaseD_avg)
colnames(ift_ft_xC_1sampleN_PhaseD_avg_scale_df) <- "value"
ift_ft_xC_1sampleN_PhaseD_avg_scale_df$series <- "SK_PhaseD"

# Combine into a new data frame xC_SK_Phases_B_C_D_df
xC_SK_Phases_B_C_D_df <- rbind(xC_scale_df, ift_ft_xC_1sampleN_PhaseB_avg_scale_df, 
             ift_ft_xC_1sampleN_PhaseC_avg_scale_df, ift_ft_xC_1sampleN_PhaseD_avg_scale_df)

# library(ggplot2)
ggplot(xC_SK_Phases_B_C_D_df, aes(value, fill = series)) + 
  geom_density(aes(color=series, linetype = series), alpha=0.4, size=1.2) +  # position = "stack"
  theme(text = element_text(size=20)) +
  scale_fill_manual( values = c("yellow", "red", "blue", "green")) +
  geom_line(data=xC_scale_df, stat = "density", color="purple", lty=4, lwd=2) +
  ## guides(color = guide_legend(order=1)) +
  theme(axis.title.x=element_blank(),axis.text.x=element_blank(), axis.ticks.x=element_blank()) # +

## [1] 0.8844888
## [1] 0.01011428
## [1] 0.001782234

Figure 4.2

## quartz_off_screen 
##                 2
## 
##  Two-sample Kolmogorov-Smirnov test
## 
## data:  scale(xC) and scale(ift_ft_xC_1sampleN_PhaseC_avg)
## D = 0.05775, p-value = 5.174e-12
## alternative hypothesis: two-sided
## Warning in ks.test(scale(ift_ft_xC_1sampleN_PhaseC_avg),
## scale(ift_ft_xC_1sampleN_PhaseD_avg)): p-value will be approximate in the
## presence of ties
## 
##  Two-sample Kolmogorov-Smirnov test
## 
## data:  scale(ift_ft_xC_1sampleN_PhaseC_avg) and scale(ift_ft_xC_1sampleN_PhaseD_avg)
## D = 0.014625, p-value = 0.3592
## alternative hypothesis: two-sided
## 
## 
##  Anderson-Darling k-sample test.
## 
## Number of samples:  2
## Sample sizes:  8000, 8000
## Number of ties: 0
## 
## Mean of  Anderson-Darling  Criterion: 1
## Standard deviation of  Anderson-Darling  Criterion: 0.76131
## 
## T.AD = ( Anderson-Darling  Criterion - mean)/sigma
## 
## Null Hypothesis: All samples come from a common population.
## 
##               AD  T.AD  asympt. P-value
## version 1: 16.18 19.93        4.168e-09
## version 2: 16.20 19.94        4.291e-09

Figure 4.3

## Warning in ks.test(scale(ift_ft_xC_1sampleN_PhaseD_avg),
## scale(ift_ft_xC_1sampleN_PhaseC_avg)): p-value will be approximate in the
## presence of ties

## Warning in ks.test(scale(ift_ft_xC_1sampleN_PhaseD_avg),
## scale(ift_ft_xC_1sampleN_PhaseC_avg)): p-value will be approximate in the
## presence of ties
## quartz_off_screen 
##                 2
## [1] 0.359816
## [1] 0.2563408
## [1] 0.001021943
## [1] 0.2446415
## [1] -0.6270681
## [1] -0.3619818
## [1] 0.2149918
## [1] -0.5228285

1.3 Bayesian Inference Simulation

Here we relate to Bayesian inference to Spacekime analytics based on a single (cohort (\(A\)) spacetime observation \(x_{i_o}\) and some prior kime-phases (obtained from cohorts \(A\), \(B\), or \(C\)). This is accomplished by computing the prior or posterior predictive distribution.

1.3.1 Canned academic example (using the cars dataset)

## 
## SAMPLING FOR MODEL 'continuous' NOW (CHAIN 1).
## Chain 1: 
## Chain 1: Gradient evaluation took 8.7e-05 seconds
## Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.87 seconds.
## Chain 1: Adjust your expectations accordingly!
## Chain 1: 
## Chain 1: 
## Chain 1: Iteration:    1 / 2000 [  0%]  (Warmup)
## Chain 1: Iteration:  200 / 2000 [ 10%]  (Warmup)
## Chain 1: Iteration:  400 / 2000 [ 20%]  (Warmup)
## Chain 1: Iteration:  600 / 2000 [ 30%]  (Warmup)
## Chain 1: Iteration:  800 / 2000 [ 40%]  (Warmup)
## Chain 1: Iteration: 1000 / 2000 [ 50%]  (Warmup)
## Chain 1: Iteration: 1001 / 2000 [ 50%]  (Sampling)
## Chain 1: Iteration: 1200 / 2000 [ 60%]  (Sampling)
## Chain 1: Iteration: 1400 / 2000 [ 70%]  (Sampling)
## Chain 1: Iteration: 1600 / 2000 [ 80%]  (Sampling)
## Chain 1: Iteration: 1800 / 2000 [ 90%]  (Sampling)
## Chain 1: Iteration: 2000 / 2000 [100%]  (Sampling)
## Chain 1: 
## Chain 1:  Elapsed Time: 0.057879 seconds (Warm-up)
## Chain 1:                0.049047 seconds (Sampling)
## Chain 1:                0.106926 seconds (Total)
## Chain 1: 
## 
## SAMPLING FOR MODEL 'continuous' NOW (CHAIN 2).
## Chain 2: 
## Chain 2: Gradient evaluation took 1.2e-05 seconds
## Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 0.12 seconds.
## Chain 2: Adjust your expectations accordingly!
## Chain 2: 
## Chain 2: 
## Chain 2: Iteration:    1 / 2000 [  0%]  (Warmup)
## Chain 2: Iteration:  200 / 2000 [ 10%]  (Warmup)
## Chain 2: Iteration:  400 / 2000 [ 20%]  (Warmup)
## Chain 2: Iteration:  600 / 2000 [ 30%]  (Warmup)
## Chain 2: Iteration:  800 / 2000 [ 40%]  (Warmup)
## Chain 2: Iteration: 1000 / 2000 [ 50%]  (Warmup)
## Chain 2: Iteration: 1001 / 2000 [ 50%]  (Sampling)
## Chain 2: Iteration: 1200 / 2000 [ 60%]  (Sampling)
## Chain 2: Iteration: 1400 / 2000 [ 70%]  (Sampling)
## Chain 2: Iteration: 1600 / 2000 [ 80%]  (Sampling)
## Chain 2: Iteration: 1800 / 2000 [ 90%]  (Sampling)
## Chain 2: Iteration: 2000 / 2000 [100%]  (Sampling)
## Chain 2: 
## Chain 2:  Elapsed Time: 0.06328 seconds (Warm-up)
## Chain 2:                0.043701 seconds (Sampling)
## Chain 2:                0.106981 seconds (Total)
## Chain 2: 
## 
## SAMPLING FOR MODEL 'continuous' NOW (CHAIN 3).
## Chain 3: 
## Chain 3: Gradient evaluation took 1.3e-05 seconds
## Chain 3: 1000 transitions using 10 leapfrog steps per transition would take 0.13 seconds.
## Chain 3: Adjust your expectations accordingly!
## Chain 3: 
## Chain 3: 
## Chain 3: Iteration:    1 / 2000 [  0%]  (Warmup)
## Chain 3: Iteration:  200 / 2000 [ 10%]  (Warmup)
## Chain 3: Iteration:  400 / 2000 [ 20%]  (Warmup)
## Chain 3: Iteration:  600 / 2000 [ 30%]  (Warmup)
## Chain 3: Iteration:  800 / 2000 [ 40%]  (Warmup)
## Chain 3: Iteration: 1000 / 2000 [ 50%]  (Warmup)
## Chain 3: Iteration: 1001 / 2000 [ 50%]  (Sampling)
## Chain 3: Iteration: 1200 / 2000 [ 60%]  (Sampling)
## Chain 3: Iteration: 1400 / 2000 [ 70%]  (Sampling)
## Chain 3: Iteration: 1600 / 2000 [ 80%]  (Sampling)
## Chain 3: Iteration: 1800 / 2000 [ 90%]  (Sampling)
## Chain 3: Iteration: 2000 / 2000 [100%]  (Sampling)
## Chain 3: 
## Chain 3:  Elapsed Time: 0.052791 seconds (Warm-up)
## Chain 3:                0.051304 seconds (Sampling)
## Chain 3:                0.104095 seconds (Total)
## Chain 3: 
## 
## SAMPLING FOR MODEL 'continuous' NOW (CHAIN 4).
## Chain 4: 
## Chain 4: Gradient evaluation took 1e-05 seconds
## Chain 4: 1000 transitions using 10 leapfrog steps per transition would take 0.1 seconds.
## Chain 4: Adjust your expectations accordingly!
## Chain 4: 
## Chain 4: 
## Chain 4: Iteration:    1 / 2000 [  0%]  (Warmup)
## Chain 4: Iteration:  200 / 2000 [ 10%]  (Warmup)
## Chain 4: Iteration:  400 / 2000 [ 20%]  (Warmup)
## Chain 4: Iteration:  600 / 2000 [ 30%]  (Warmup)
## Chain 4: Iteration:  800 / 2000 [ 40%]  (Warmup)
## Chain 4: Iteration: 1000 / 2000 [ 50%]  (Warmup)
## Chain 4: Iteration: 1001 / 2000 [ 50%]  (Sampling)
## Chain 4: Iteration: 1200 / 2000 [ 60%]  (Sampling)
## Chain 4: Iteration: 1400 / 2000 [ 70%]  (Sampling)
## Chain 4: Iteration: 1600 / 2000 [ 80%]  (Sampling)
## Chain 4: Iteration: 1800 / 2000 [ 90%]  (Sampling)
## Chain 4: Iteration: 2000 / 2000 [100%]  (Sampling)
## Chain 4: 
## Chain 4:  Elapsed Time: 0.053378 seconds (Warm-up)
## Chain 4:                0.050564 seconds (Sampling)
## Chain 4:                0.103942 seconds (Total)
## Chain 4:
## [1] 500  32
##      Mazda RX4 Mazda RX4 Wag Datsun 710 Hornet 4 Drive Hornet Sportabout
## [1,]  27.83336      18.09879   20.46724       14.94926          21.22127
## [2,]  24.77289      16.98112   23.68493       20.31273          22.80704
## [3,]  23.77452      19.55476   21.77243       20.91546          26.30971
## [4,]  26.54254      26.26840   24.77963       18.60093          15.30545
## [5,]  19.92686      21.62055   17.91423       18.02250          21.09841
## [6,]  21.90781      26.91936   29.17032       22.04833          20.19422
##       Valiant Duster 360 Merc 240D Merc 230 Merc 280 Merc 280C Merc 450SE
## [1,] 16.04502   11.63372  10.66559 21.98524 18.77021  16.82558   20.66930
## [2,] 17.02287   19.83796  17.14120 16.72355 18.56964  18.19241   13.83327
## [3,] 11.94724   17.55357  20.60386 20.24220 16.07105  13.96353   21.31576
## [4,] 18.45152   11.59260  17.42055 19.23979 19.46640  16.28337   11.92779
## [5,] 19.79754   20.18799  10.31959 22.63071 14.70449  21.23318   12.28052
## [6,] 19.52867   17.17794  26.50403 20.79099 16.85627  16.74970   13.36202
##      Merc 450SL Merc 450SLC Cadillac Fleetwood Lincoln Continental
## [1,]   15.08942    11.83466           6.372787            9.092416
## [2,]   17.31504    16.66325           9.372890           12.408165
## [3,]   15.25952    12.59248          17.628026           10.148073
## [4,]   13.40738    19.09064           9.711535            4.601367
## [5,]   12.99332    11.82580          13.126779           10.442684
## [6,]   19.48801    17.77542          10.097532           13.456131
##      Chrysler Imperial Fiat 128 Honda Civic Toyota Corolla Toyota Corona
## [1,]          3.101490 26.17248    33.26072       26.44477      23.51168
## [2,]          9.955109 23.92047    32.12174       31.83815      18.68618
## [3,]         15.272304 26.73828    31.45684       29.42007      18.51923
## [4,]          4.139900 24.74204    29.99198       27.54381      23.87595
## [5,]         10.925105 31.37050    25.43979       29.22767      19.55515
## [6,]         11.670132 26.93697    23.03120       27.34927      22.59423
##      Dodge Challenger AMC Javelin Camaro Z28 Pontiac Firebird Fiat X1-9
## [1,]         16.76778    20.74306   13.82242         20.16253  21.41692
## [2,]         20.19332    19.76735   15.33447         14.03374  29.31019
## [3,]         19.45280    20.17639   18.14594         18.82701  21.83076
## [4,]         19.98458    16.43369   17.16427         17.83606  31.44703
## [5,]         19.06299    12.15604   13.62427         14.28430  27.65276
## [6,]         18.59290    17.41055   17.88571         16.34603  24.81989
##      Porsche 914-2 Lotus Europa Ford Pantera L Ferrari Dino Maserati Bora
## [1,]      26.13833     30.02604       21.19611     19.17363      14.86521
## [2,]      25.81284     31.69259       17.01650     22.37688      14.34606
## [3,]      22.29241     29.33770       23.94098     21.58584      15.72230
## [4,]      30.00675     27.46188       19.61855     26.95284      22.31696
## [5,]      22.70551     27.20964       19.32096     22.94286      16.71434
## [6,]      25.03603     30.59111       12.12025     20.62699      15.26275
##      Volvo 142E
## [1,]   19.80113
## [2,]   21.60304
## [3,]   25.29954
## [4,]   20.34696
## [5,]   17.49975
## [6,]   18.85936

## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

1.3.2 Example of spacekime-analytics as a Bayesian Inference problem (using the fMRI dataset)

Recall that we have:

  • Original data ift_ft_xC_fMRI
  • IFT of the data using C-Phases ift_ft_xC_fMRI_1sampleN_PhaseC for all samples of \(N=30\)
  • IFT of the data using B-Phases ift_ft_xC_fMRI_1sampleN_PhaseB for all samples of \(N=30\), and
  • IFT of the data using D-Phases ift_ft_xC_fMRI_1sampleN_PhaseD for all samples of \(N=30\)
## [1] 144
## [1] 144  30
## [1] 144  30
## [1] 144  30
## [1]  90 144

## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

1.3.3 Example of spacekime-analytics as a Bayesian Inference problem (using the bimodal simulated dataset)

Recall that we have:

  • Original data xC
  • IFT of the data using C-Phases ift_ft_xC_1sampleN_PhaseC for all samples of \(N=30\)
  • IFT of the data using B-Phases ift_ft_xC_1sampleN_PhaseB for all samples of \(N=30\), and
  • IFT of the data using D-Phases ift_ft_xC_1sampleN_PhaseD for all samples of \(N=30\)
## [1] 8000
## [1] 8000   30
## [1] 8000   30
## [1] 8000   30
## [1]   90 8000

Figure 4.4A

## Warning: Removed 3308 rows containing non-finite values (stat_density).

## Warning: Removed 3308 rows containing non-finite values (stat_density).
## quartz_off_screen 
##                 2
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Figure 4.4D

## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## quartz_off_screen 
##                 2
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Figure 4.4C

## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## quartz_off_screen 
##                 2

Figure 4.4B

## quartz_off_screen 
##                 2

2 Appendix: Functions Used

2.2 kSpaceTransform()

#' A generic function to Transform Data ={all predictors (X) and outcome (Y)} to k-space (Fourier domain)
#' For ForwardFT, set parameters as (rawData, FALSE, NULL)
#' For InverseFT, there are two parameters setting: (magnitudes, TRUE, reconPhasesToUse) or (FT_data, TRUE, NULL)
#'
#' @param data dataset that needs K-space transformation
kSpaceTransform <- function(data, inverse = FALSE, reconPhases = NULL) {
  # ForwardFT (rawData, FALSE, NULL)
  # InverseFT(magnitudes, TRUE, reconPhasesToUse) or InverseFT(FT_data, TRUE, NULL)
  FT_data <- array(complex(), length(data))
  mag_FT_data <- array(complex(), length(data))
  phase_FT_data <- array(complex(), length(data))
  IFT_reconPhases_data <- array(complex(), length(data))

  if (inverse == FALSE | is.null(reconPhases)) {
      FT_data <- fft(data, inverse)
      X2 <- FT_data
      mag_FT_data <- sqrt(Re(X2)^2+Im(X2)^2) 
      phase_FT_data <- atan2(Im(X2), Re(X2)) 
  }
  else {  # for IFT synthesis using user-provided Phases, typically from kime-phase aggregators
      Real <- data * cos(reconPhases)  
      Imaginary <- data * sin(reconPhases) 
      IFT_reconPhases_data <- 
          Re(fft(Real+1i*Imaginary, inverse = TRUE)/length(data))
  }

    ######### Test the FT-IFT analysis-synthesis back-and-forth transform process 
    #         to confirm calculations
    # X2 <- FT_data[ , 1]; mag_FT_data[ , 1] <- sqrt(Re(X2)^2+Im(X2)^2); 
    # phase_FT_data[ , 1] <- atan2(Im(X2), Re(X2)); 
    # Real2 = mag_FT_data[ , 1] * cos(phase_FT_data[ , 1])
    # Imaginary2 = mag_FT_data[ , 1] * sin(phase_FT_data[ , 1])
    # man_hat_X2 = Re(fft(Real2 + 1i*Imaginary2, inverse = T)/length(X2))
    # ifelse(abs(man_hat_X2[5] - data[5, 1]) < 0.001, "Perfect Syntesis", "Problems!!!")
    #########
  
    if (inverse == FALSE | is.null(reconPhases)) {
      return(list("magnitudes"=mag_FT_data, "phases"=phase_FT_data))
      # Use kSpaceTransform$magnitudes & kSpaceTransform$phases to retrieve teh Mags and Phases
    }
    else {
      return(IFT_reconPhases_data)
      # Use Re(kSpaceTransform) to extract spacetime Real-valued reconstructed data
    }
}