=========================================================
研究題目:數位世代青少年現實心理需求滿足對網路成癮的影響:
核心負向情緒與網路代償的串聯中介效應分析
資料來源:TIGPS 2024 Wave 2 國八學生問卷
========================================================

0. data & package

##############################
### 0. data & package
##############################

if (!require("tidyverse")) install.packages("tidyverse")
if (!require("haven")) install.packages("haven")
if (!require("lavaan")) install.packages("lavaan")
if (!require("semTools")) install.packages("semTools")
if (!require("psych")) install.packages("psych")

library(tidyverse)
library(haven)
library(lavaan)
library(semTools)
library(psych)

raw_data <- read_sav("TIGPSw2_s.sav")

1. 資料整理

##############################
### 1. 資料整理
##############################

# SDT
Relatedness_items <- c("bs5e", "bs5f", "bs12b", "bs9a")
Autonomy_items <- c("bs55q", "bs55r", "bs55s")
Competence_items <- c("bs55g", "bs55h", "bs55i")
SDT_items <- c(Relatedness_items, Autonomy_items, Competence_items)

# 負向情緒
# bs56g、bs56i 本身是負向情緒,不反向
# bs53a 是正向自我價值,需反向
NegEmo_items <- c("bs56c", "bs56d", "bs56e", "bs56f", 
                  "bs56g", "bs56i", "bs56j",
                  "bs56k", "bs56l", "bs56m", "bs56n","bs53a")

# 網路代償調節
CybComp_items <- c("bs25m", "bs44b", "bs25i", "bs25l", 
                   "bs25c", "bs22e", "bs22d", "bs25n")

# 網路成癮
IntAdd_items <- c( "bs28a", "bs28b", "bs28c", "bs28d", "bs28e",
                   "bs28f", "bs28g", "bs28h", "bs28i", "bs28j")

all_raw_items <- c( SDT_items, NegEmo_items, CybComp_items, IntAdd_items )


selected_df <- raw_data %>%
  select( nstudent_id, bs1, bs3, bs4a, bs4b, bs4c, all_of(all_raw_items))

clean_df <- selected_df %>%
  mutate(across(everything(), ~ ifelse(. < 0, NA, .))) %>%
  mutate(bs53a = 5 - bs53a) # 反向題處理

apply(apply(clean_df, 1, is.na), 1, sum)
## nstudent_id         bs1         bs3        bs4a        bs4b        bs4c 
##           0           0          17          28          28          28 
##        bs5e        bs5f       bs12b        bs9a       bs55q       bs55r 
##          49          49          57           9          86          86 
##       bs55s       bs55g       bs55h       bs55i       bs56c       bs56d 
##          86          86          86          86          89          90 
##       bs56e       bs56f       bs56g       bs56i       bs56j       bs56k 
##          89          89          89          89          89          89 
##       bs56l       bs56m       bs56n       bs53a       bs25m       bs44b 
##          89          89          89          84          50          88 
##       bs25i       bs25l       bs25c       bs22e       bs22d       bs25n 
##          50          50          50          51          51          50 
##       bs28a       bs28b       bs28c       bs28d       bs28e       bs28f 
##          55          54          54          54          54          54 
##       bs28g       bs28h       bs28i       bs28j 
##          54          54          54          54
clean_df <- clean_df %>%
  drop_na(all_of(all_raw_items), bs1, bs3) # n=8537

2. CFA

##############################
### 2. CFA 
##############################

cfa_data <- clean_df
cat("CFA有效樣本數 =", nrow(cfa_data), "\n")  # n= 8537
## CFA有效樣本數 = 8537
# model
cfa_model <- '

Relatedness =~
bs5e + bs5f + bs12b + bs9a

Autonomy =~
bs55q + bs55r + bs55s

Competence =~
bs55g + bs55h + bs55i

X_SDT =~
Relatedness + Autonomy + Competence

M1_NegEmo =~
bs56c + bs56d + bs56e + bs56f +
bs56g + bs56i + bs56j +
bs56k + bs56l + bs56m +
bs56n + bs53a

M2_CybComp =~
bs25m + bs44b + bs25i + bs25l +
bs25c + bs22e + bs22d + bs25n

Y_IntAdd =~
bs28a + bs28b + bs28c + bs28d + bs28e +
bs28f + bs28g + bs28h + bs28i + bs28j

'

cfa_fit <- cfa( model = cfa_model, data = cfa_data, estimator = "ML", std.lv = TRUE )

summary( cfa_fit, fit.measures = TRUE, standardized = TRUE)
## lavaan 0.6-21 ended normally after 59 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        89
## 
##   Number of observations                          8537
## 
## Model Test User Model:
##                                                        
##   Test statistic                              30409.605
##   Degrees of freedom                                731
##   P-value (Chi-square)                            0.000
## 
## Model Test Baseline Model:
## 
##   Test statistic                            211847.141
##   Degrees of freedom                               780
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.859
##   Tucker-Lewis Index (TLI)                       0.850
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)            -351565.414
##   Loglikelihood unrestricted model (H1)    -336360.611
##                                                       
##   Akaike (AIC)                              703308.827
##   Bayesian (BIC)                            703936.470
##   Sample-size adjusted Bayesian (SABIC)     703653.644
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.069
##   90 Percent confidence interval - lower         0.068
##   90 Percent confidence interval - upper         0.070
##   P-value H_0: RMSEA <= 0.050                    0.000
##   P-value H_0: RMSEA >= 0.080                    0.000
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.067
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   Relatedness =~                                                        
##     bs5e              0.734    0.010   76.715    0.000    0.788    0.888
##     bs5f              0.655    0.009   75.634    0.000    0.703    0.860
##     bs12b             0.170    0.007   24.682    0.000    0.182    0.281
##     bs9a              0.221    0.009   24.115    0.000    0.237    0.275
##   Autonomy =~                                                           
##     bs55q             0.382    0.008   45.639    0.000    0.561    0.801
##     bs55r             0.350    0.008   45.694    0.000    0.515    0.804
##     bs55s             0.399    0.009   46.838    0.000    0.586    0.873
##   Competence =~                                                         
##     bs55g             0.305    0.021   14.692    0.000    0.707    0.875
##     bs55h             0.304    0.021   14.691    0.000    0.705    0.874
##     bs55i             0.293    0.020   14.693    0.000    0.680    0.879
##   X_SDT =~                                                              
##     Relatedness       0.390    0.015   26.257    0.000    0.364    0.364
##     Autonomy          1.076    0.040   26.782    0.000    0.733    0.733
##     Competence        2.089    0.175   11.970    0.000    0.902    0.902
##   M1_NegEmo =~                                                          
##     bs56c             0.872    0.010   88.933    0.000    0.872    0.805
##     bs56d             0.662    0.009   76.137    0.000    0.662    0.722
##     bs56e             0.618    0.008   80.823    0.000    0.618    0.754
##     bs56f             0.646    0.008   79.643    0.000    0.646    0.746
##     bs56g             0.720    0.010   70.203    0.000    0.720    0.680
##     bs56i             0.783    0.009   84.904    0.000    0.783    0.780
##     bs56j             0.815    0.010   83.313    0.000    0.815    0.770
##     bs56k             0.766    0.008   93.868    0.000    0.766    0.833
##     bs56l             0.793    0.009   84.931    0.000    0.793    0.780
##     bs56m             0.914    0.009   96.504    0.000    0.914    0.848
##     bs56n             0.912    0.010   94.868    0.000    0.912    0.839
##     bs53a             0.368    0.009   42.336    0.000    0.368    0.445
##   M2_CybComp =~                                                         
##     bs25m             0.803    0.009   87.791    0.000    0.803    0.818
##     bs44b             0.345    0.009   37.444    0.000    0.345    0.411
##     bs25i             0.613    0.009   65.277    0.000    0.613    0.659
##     bs25l             0.678    0.009   75.234    0.000    0.678    0.734
##     bs25c             0.552    0.009   58.982    0.000    0.552    0.609
##     bs22e             0.317    0.012   26.223    0.000    0.317    0.295
##     bs22d             0.227    0.011   19.798    0.000    0.227    0.225
##     bs25n             0.812    0.009   91.361    0.000    0.812    0.840
##   Y_IntAdd =~                                                           
##     bs28a             0.682    0.008   86.352    0.000    0.682    0.792
##     bs28b             0.683    0.008   89.153    0.000    0.683    0.809
##     bs28c             0.690    0.008   87.674    0.000    0.690    0.800
##     bs28d             0.726    0.008   85.392    0.000    0.726    0.786
##     bs28e             0.717    0.009   84.267    0.000    0.717    0.779
##     bs28f             0.712    0.009   82.544    0.000    0.712    0.768
##     bs28g             0.609    0.008   74.911    0.000    0.609    0.717
##     bs28h             0.559    0.008   72.966    0.000    0.559    0.703
##     bs28i             0.567    0.008   69.751    0.000    0.567    0.680
##     bs28j             0.568    0.008   68.058    0.000    0.568    0.667
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   X_SDT ~~                                                              
##     M1_NegEmo        -0.209    0.012  -17.219    0.000   -0.209   -0.209
##     M2_CybComp       -0.095    0.013   -7.405    0.000   -0.095   -0.095
##     Y_IntAdd         -0.190    0.012  -15.468    0.000   -0.190   -0.190
##   M1_NegEmo ~~                                                          
##     M2_CybComp        0.306    0.011   28.240    0.000    0.306    0.306
##     Y_IntAdd          0.306    0.011   29.137    0.000    0.306    0.306
##   M2_CybComp ~~                                                         
##     Y_IntAdd          0.500    0.009   53.843    0.000    0.500    0.500
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .bs5e              0.167    0.010   15.954    0.000    0.167    0.212
##    .bs5f              0.174    0.009   20.471    0.000    0.174    0.261
##    .bs12b             0.389    0.006   64.507    0.000    0.389    0.921
##    .bs9a              0.689    0.011   64.547    0.000    0.689    0.925
##    .bs55q             0.176    0.004   48.252    0.000    0.176    0.358
##    .bs55r             0.145    0.003   47.921    0.000    0.145    0.354
##    .bs55s             0.107    0.003   35.413    0.000    0.107    0.238
##    .bs55g             0.152    0.003   43.651    0.000    0.152    0.234
##    .bs55h             0.153    0.003   43.950    0.000    0.153    0.236
##    .bs55i             0.135    0.003   42.775    0.000    0.135    0.227
##    .bs56c             0.414    0.007   58.827    0.000    0.414    0.353
##    .bs56d             0.402    0.007   61.482    0.000    0.402    0.479
##    .bs56e             0.290    0.005   60.682    0.000    0.290    0.432
##    .bs56f             0.333    0.005   60.898    0.000    0.333    0.444
##    .bs56g             0.604    0.010   62.299    0.000    0.604    0.538
##    .bs56i             0.395    0.007   59.837    0.000    0.395    0.392
##    .bs56j             0.457    0.008   60.185    0.000    0.457    0.407
##    .bs56k             0.258    0.005   57.264    0.000    0.258    0.305
##    .bs56l             0.404    0.007   59.831    0.000    0.404    0.392
##    .bs56m             0.326    0.006   56.232    0.000    0.326    0.281
##    .bs56n             0.350    0.006   56.891    0.000    0.350    0.296
##    .bs53a             0.548    0.008   64.461    0.000    0.548    0.802
##    .bs25m             0.320    0.007   47.333    0.000    0.320    0.332
##    .bs44b             0.586    0.009   63.585    0.000    0.586    0.831
##    .bs25i             0.489    0.008   58.633    0.000    0.489    0.565
##    .bs25l             0.395    0.007   55.094    0.000    0.395    0.462
##    .bs25c             0.517    0.009   60.231    0.000    0.517    0.629
##    .bs22e             1.057    0.016   64.518    0.000    1.057    0.913
##    .bs22d             0.971    0.015   64.878    0.000    0.971    0.949
##    .bs25n             0.275    0.006   44.065    0.000    0.275    0.295
##    .bs28a             0.276    0.005   57.425    0.000    0.276    0.372
##    .bs28b             0.246    0.004   56.423    0.000    0.246    0.345
##    .bs28c             0.267    0.005   56.970    0.000    0.267    0.359
##    .bs28d             0.325    0.006   57.737    0.000    0.325    0.382
##    .bs28e             0.333    0.006   58.085    0.000    0.333    0.393
##    .bs28f             0.352    0.006   58.583    0.000    0.352    0.410
##    .bs28g             0.351    0.006   60.382    0.000    0.351    0.486
##    .bs28h             0.319    0.005   60.756    0.000    0.319    0.505
##    .bs28i             0.374    0.006   61.314    0.000    0.374    0.538
##    .bs28j             0.402    0.007   61.581    0.000    0.402    0.555
##    .Relatedness       1.000                               0.868    0.868
##    .Autonomy          1.000                               0.463    0.463
##    .Competence        1.000                               0.186    0.186
##     X_SDT             1.000                               1.000    1.000
##     M1_NegEmo         1.000                               1.000    1.000
##     M2_CybComp        1.000                               1.000    1.000
##     Y_IntAdd          1.000                               1.000    1.000
fitMeasures( cfa_fit, c("chisq", "df", "cfi", "tli", "rmsea", "srmr"))
##     chisq        df       cfi       tli     rmsea      srmr 
## 30409.605   731.000     0.859     0.850     0.069     0.067

3. reliability

##############################
### 3. reliability
##############################

rel <- semTools::reliability(cfa_fit)

table_reliability <- data.frame(
  Construct = c(
    "Relatedness","Autonomy","Competence",
    "Negative Emotion","Cyber Compensation","Internet Addiction"),
  
  Alpha = c(
    psych::alpha(cfa_data[, Relatedness_items], check.keys = TRUE)$total$raw_alpha,
    psych::alpha(cfa_data[, Autonomy_items], check.keys = TRUE)$total$raw_alpha,
    psych::alpha(cfa_data[, Competence_items], check.keys = TRUE)$total$raw_alpha,
    psych::alpha(cfa_data[, NegEmo_items], check.keys = TRUE)$total$raw_alpha,
    psych::alpha(cfa_data[, CybComp_items], check.keys = TRUE)$total$raw_alpha,
    psych::alpha(cfa_data[, IntAdd_items], check.keys = TRUE)$total$raw_alpha ),
  
  CR = c(
    rel["omega", "Relatedness"],
    rel["omega", "Autonomy"],
    rel["omega", "Competence"],
    rel["omega", "M1_NegEmo"],
    rel["omega", "M2_CybComp"],
    rel["omega", "Y_IntAdd"]),
  
  AVE = c(
    rel["avevar", "Relatedness"],
    rel["avevar", "Autonomy"],
    rel["avevar", "Competence"],
    rel["avevar", "M1_NegEmo"],
    rel["avevar", "M2_CybComp"],
    rel["avevar", "Y_IntAdd"])) %>%
  mutate(across(where(is.numeric), ~ round(.x, 3)))

table_reliability
##            Construct Alpha    CR   AVE
## 1        Relatedness 0.664 0.720 0.459
## 2           Autonomy 0.865 0.866 0.683
## 3         Competence 0.908 0.908 0.768
## 4   Negative Emotion 0.939 0.943 0.588
## 5 Cyber Compensation 0.801 0.804 0.371
## 6 Internet Addiction 0.928 0.929 0.569
alpha_SDT<- psych::alpha(cfa_data[, SDT_items], check.keys = TRUE)$total$raw_alpha

# 是否要放二階因子的因素負荷量??
# 另外手算
standardizedSolution(cfa_fit) %>% filter(lhs=="X_SDT", op=="=~")
##     lhs op         rhs est.std    se      z pvalue ci.lower ci.upper
## 1 X_SDT =~ Relatedness   0.364 0.012 30.256      0    0.340    0.387
## 2 X_SDT =~    Autonomy   0.733 0.013 57.806      0    0.708    0.757
## 3 X_SDT =~  Competence   0.902 0.014 64.210      0    0.874    0.930
lambda <- standardizedSolution(cfa_fit) %>% filter(lhs == "X_SDT", op == "=~") %>%
  pull(est.std)

CR_SDT <- (sum(lambda)^2) / ((sum(lambda)^2) + sum(1 - lambda^2))
AVE_SDT <- mean(lambda^2)

data.frame(Alpha = round(alpha_SDT, 3),
           CR    = round(CR_SDT, 3),
           AVE   = round(AVE_SDT, 3))
##   Alpha    CR   AVE
## 1 0.839 0.725 0.494

4. 描述統計

##############################
### 4. 描述統計
##############################


# 算總分
analysis_df <- clean_df %>%
  mutate(
    GRIT = bs4a + bs4b + bs4c,
    Relatedness = bs5e + bs5f + bs12b + bs9a,
    Autonomy = bs55q + bs55r + bs55s,
    Competence = bs55g + bs55h + bs55i,
    
    X_SDT = Relatedness + Autonomy + Competence,
    
    M1_NegEmo =
      bs56c + bs56d + bs56e + bs56f +
      bs56g + bs56i + bs56j +
      bs56k + bs56l + bs56m + bs56n + bs53a,
    
    M2_CybComp =
      bs25m + bs44b + bs25i + bs25l +
      bs25c + bs22e + bs22d + bs25n,
    
    Y_IntAdd =
      bs28a + bs28b + bs28c + bs28d + bs28e +
      bs28f + bs28g + bs28h + bs28i + bs28j )

names(analysis_df)
##  [1] "nstudent_id" "bs1"         "bs3"         "bs4a"        "bs4b"       
##  [6] "bs4c"        "bs5e"        "bs5f"        "bs12b"       "bs9a"       
## [11] "bs55q"       "bs55r"       "bs55s"       "bs55g"       "bs55h"      
## [16] "bs55i"       "bs56c"       "bs56d"       "bs56e"       "bs56f"      
## [21] "bs56g"       "bs56i"       "bs56j"       "bs56k"       "bs56l"      
## [26] "bs56m"       "bs56n"       "bs53a"       "bs25m"       "bs44b"      
## [31] "bs25i"       "bs25l"       "bs25c"       "bs22e"       "bs22d"      
## [36] "bs25n"       "bs28a"       "bs28b"       "bs28c"       "bs28d"      
## [41] "bs28e"       "bs28f"       "bs28g"       "bs28h"       "bs28i"      
## [46] "bs28j"       "GRIT"        "Relatedness" "Autonomy"    "Competence" 
## [51] "X_SDT"       "M1_NegEmo"   "M2_CybComp"  "Y_IntAdd"

背景變項

# 背景變項

table_gender <- analysis_df %>%
  count(bs1) %>% #編碼簿中 1女 2男
  mutate( Gender = c("女生", "男生"), Percent = round(100 * n / sum(n), 2)) %>%
  select(Gender, N = n, Percent)

table_gender
## # A tibble: 2 × 3
##   Gender     N Percent
##   <chr>  <int>   <dbl>
## 1 女生    4211    49.3
## 2 男生    4326    50.7
table_ses <- analysis_df %>%
  count(bs3) %>%
  mutate( Percent = round(100 * n / sum(n), 2))

table_ses
## # A tibble: 10 × 3
##      bs3     n Percent
##    <dbl> <int>   <dbl>
##  1     1   154    1.8 
##  2     2   170    1.99
##  3     3   314    3.68
##  4     4   673    7.88
##  5     5  2268   26.6 
##  6     6  2093   24.5 
##  7     7  1587   18.6 
##  8     8   695    8.14
##  9     9   189    2.21
## 10    10   394    4.62

連續變項

# 連續變項
table_descriptive <- psych::describe(analysis_df %>%
                                       select(X_SDT, Relatedness, Autonomy, Competence,
                                              M1_NegEmo, M2_CybComp, Y_IntAdd )) %>%
  as.data.frame() %>%
  rownames_to_column("Variable") %>%
  select( Variable, n, M = mean, SD = sd, Skewness = skew, Kurtosis = kurtosis ) %>%
  
  mutate(across(where(is.numeric), ~ round(.x, 3)))

table_descriptive
##      Variable    n      M    SD Skewness Kurtosis
## 1       X_SDT 8537 30.847 4.897   -0.457    0.824
## 2 Relatedness 8537 12.138 2.286   -0.519    0.285
## 3    Autonomy 8537  9.754 1.788   -0.727    1.517
## 4  Competence 8537  8.956 2.194   -0.634    0.469
## 5   M1_NegEmo 8537  7.523 9.138    2.157    4.704
## 6  M2_CybComp 8537 19.561 4.947    0.028   -0.151
## 7    Y_IntAdd 8537 20.211 6.767    0.297   -0.081

5. correlation

##############################
### 5. correlation
##############################

corr_data <- analysis_df %>% select(X_SDT, M1_NegEmo, M2_CybComp, Y_IntAdd)

corr_result <- psych::corr.test(corr_data, method = "pearson", adjust = "none")

r_matrix <- corr_result$r
p_matrix <- corr_result$p

vars <- c("X_SDT", "M1_NegEmo", "M2_CybComp", "Y_IntAdd")

formatted_matrix <- matrix(  "",  nrow = length(vars), ncol = length(vars) )

rownames(formatted_matrix) <- c(
  "1. SDT",
  "2. Negative Emotion",
  "3. Cyber Compensation",
  "4. Internet Addiction" )

colnames(formatted_matrix) <- c("1", "2", "3", "4")

for (i in 1:length(vars)) {
  for (j in 1:length(vars)) {
        if (i > j) {
      r_val <- r_matrix[i, j]
      p_val <- p_matrix[i, j]
      
      stars <- case_when( p_val < .001 ~ "***", p_val < .01  ~ "**", p_val < .05  ~ "*", TRUE ~ "")
      
      formatted_matrix[i, j] <- paste0(sprintf("%.3f", r_val), stars)}}}

table_correlation <- formatted_matrix %>%  as.data.frame() %>% rownames_to_column("Variable")

table_correlation
##                Variable         1        2        3 4
## 1                1. SDT                              
## 2   2. Negative Emotion -0.267***                    
## 3 3. Cyber Compensation  -0.029** 0.244***           
## 4 4. Internet Addiction -0.183*** 0.295*** 0.451***

6. 串聯中介模型

##############################
### 6. 串聯中介模型
##############################


cat("正式中介分析有效樣本數 =", nrow(analysis_df), "\n")
## 正式中介分析有效樣本數 = 8537
mediation_model <- '

  #  M1(Negative Emotion)
  M1_NegEmo ~ a1*X_SDT + bs1 + bs3

  # M2(Cyber Compensation)
  M2_CybComp ~ a2*X_SDT + b1*M1_NegEmo + bs1 + bs3
  
  # Y(Internet Addiction)
  Y_IntAdd ~ cp*X_SDT + c1*M1_NegEmo + b2*M2_CybComp + bs1 + bs3

  
  # 定義效果量 (Defined Parameters) #
  
  # 1. (X -> M1 -> M2 -> Y)
  serial_indirect := a1 * b1 * b2
  
  # 2. (X -> M1 -> Y)
  m1_indirect     := a1 * c1
  
  # 3. (X -> M2 -> Y)
  m2_indirect     := a2 * b2
  
  # 4. TOTAL
  total_indirect  := (a1 * b1 * b2) + (a1 * c1) + (a2 * b2)
  
  # 5. c prime
  direct_effect   := cp
  
  # 6. (Total Effect = 直接效果 + 總間接效果)
  total_effect    := cp + total_indirect
'

set.seed(2026)

fit_model <- sem( model = mediation_model, data = analysis_df, se = "bootstrap", bootstrap = 1000)

summary( fit_model, fit.measures = TRUE, standardized = TRUE, rsquare = TRUE)
## lavaan 0.6-21 ended normally after 1 iteration
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        15
## 
##   Number of observations                          8537
## 
## Model Test User Model:
##                                                       
##   Test statistic                                 0.000
##   Degrees of freedom                                 0
## 
## Model Test Baseline Model:
## 
##   Test statistic                              3911.877
##   Degrees of freedom                                12
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    1.000
##   Tucker-Lewis Index (TLI)                       1.000
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)             -83242.487
##   Loglikelihood unrestricted model (H1)     -83242.487
##                                                       
##   Akaike (AIC)                              166514.973
##   Bayesian (BIC)                            166620.756
##   Sample-size adjusted Bayesian (SABIC)     166573.089
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.000
##   90 Percent confidence interval - lower         0.000
##   90 Percent confidence interval - upper         0.000
##   P-value H_0: RMSEA <= 0.050                       NA
##   P-value H_0: RMSEA >= 0.080                       NA
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.000
## 
## Parameter Estimates:
## 
##   Standard errors                            Bootstrap
##   Number of requested bootstrap draws             1000
##   Number of successful bootstrap draws            1000
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   M1_NegEmo ~                                                           
##     X_SDT     (a1)   -0.475    0.024  -19.659    0.000   -0.475   -0.255
##     bs1              -2.387    0.195  -12.258    0.000   -2.387   -0.131
##     bs3              -0.356    0.066   -5.356    0.000   -0.356   -0.069
##   M2_CybComp ~                                                          
##     X_SDT     (a2)    0.043    0.014    2.983    0.003    0.043    0.042
##     M1_NegEmo (b1)    0.141    0.007   20.962    0.000    0.141    0.261
##     bs1               0.492    0.105    4.684    0.000    0.492    0.050
##     bs3              -0.035    0.034   -1.048    0.295   -0.035   -0.013
##   Y_IntAdd ~                                                            
##     X_SDT     (cp)   -0.178    0.015  -11.648    0.000   -0.178   -0.129
##     M1_NegEmo (c1)    0.120    0.009   13.478    0.000    0.120    0.162
##     M2_CybCmp (b2)    0.557    0.016   35.920    0.000    0.557    0.408
##     bs1               0.102    0.128    0.793    0.428    0.102    0.008
##     bs3               0.014    0.042    0.330    0.741    0.014    0.004
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .M1_NegEmo        75.696    2.197   34.462    0.000   75.696    0.907
##    .M2_CybComp       22.907    0.343   66.864    0.000   22.907    0.936
##    .Y_IntAdd         34.116    0.589   57.912    0.000   34.116    0.745
## 
## R-Square:
##                    Estimate
##     M1_NegEmo         0.093
##     M2_CybComp        0.064
##     Y_IntAdd          0.255
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##     serial_indirct   -0.037    0.003  -14.044    0.000   -0.037   -0.027
##     m1_indirect      -0.057    0.005  -12.076    0.000   -0.057   -0.041
##     m2_indirect       0.024    0.008    2.948    0.003    0.024    0.017
##     total_indirect   -0.071    0.009   -7.556    0.000   -0.071   -0.051
##     direct_effect    -0.178    0.015  -11.648    0.000   -0.178   -0.129
##     total_effect     -0.249    0.018  -13.699    0.000   -0.249   -0.180
# Confidence Intervals
parameterEstimates( fit_model, boot.ci.type = "perc", standardized = TRUE) %>%
  filter(op == ":=")
##               lhs op                        rhs           label    est    se
## 1 serial_indirect :=                   a1*b1*b2 serial_indirect -0.037 0.003
## 2     m1_indirect :=                      a1*c1     m1_indirect -0.057 0.005
## 3     m2_indirect :=                      a2*b2     m2_indirect  0.024 0.008
## 4  total_indirect := (a1*b1*b2)+(a1*c1)+(a2*b2)  total_indirect -0.071 0.009
## 5   direct_effect :=                         cp   direct_effect -0.178 0.015
## 6    total_effect :=          cp+total_indirect    total_effect -0.249 0.018
##         z pvalue ci.lower ci.upper std.lv std.all std.nox
## 1 -14.044  0.000   -0.043   -0.032 -0.037  -0.027  -0.006
## 2 -12.076  0.000   -0.067   -0.049 -0.057  -0.041  -0.008
## 3   2.948  0.003    0.008    0.040  0.024   0.017   0.004
## 4  -7.556  0.000   -0.089   -0.050 -0.071  -0.051  -0.010
## 5 -11.648  0.000   -0.207   -0.147 -0.178  -0.129  -0.026
## 6 -13.699  0.000   -0.284   -0.212 -0.249  -0.180  -0.037

7. Post survey

##############################
### 7. Post survey
##############################

post_corr_data <- analysis_df %>% select(Relatedness, Autonomy, Competence,
                                         M1_NegEmo, M2_CybComp, Y_IntAdd)

post_corr_result <- psych::corr.test(post_corr_data, method = "pearson", adjust = "none")

r_matrix <- post_corr_result$r
p_matrix <- post_corr_result$p

vars <- c("Relatedness", "Autonomy", "Competence", "M1_NegEmo", "M2_CybComp", "Y_IntAdd")

postformatted_matrix <- matrix(  "",  nrow = length(vars), ncol = length(vars) )

rownames(postformatted_matrix) <- c(
  "1. Relatedness",
  "2. Autonomy",
  "3. Competence",
  "4. Negative Emotion",
  "5. Cyber Compensation",
  "6. Internet Addiction" )

colnames(postformatted_matrix) <- c("1", "2", "3", "4", "5", "6")

for (i in 1:length(vars)) {
  for (j in 1:length(vars)) {
    if (i > j) {
      r_val <- r_matrix[i, j]
      p_val <- p_matrix[i, j]
      
      stars <- case_when( p_val < .001 ~ "***", p_val < .01  ~ "**", p_val < .05  ~ "*", TRUE ~ "")
      
      postformatted_matrix[i, j] <- paste0(sprintf("%.3f", r_val), stars)}}}

posttable_correlation <- postformatted_matrix %>%  as.data.frame() %>% rownames_to_column("Variable")

posttable_correlation
##                Variable         1         2         3        4        5 6
## 1        1. Relatedness                                                  
## 2           2. Autonomy  0.319***                                        
## 3         3. Competence  0.352***  0.586***                              
## 4   4. Negative Emotion -0.313*** -0.079*** -0.205***                    
## 5 5. Cyber Compensation -0.080***  0.039***    -0.014 0.244***           
## 6 6. Internet Addiction -0.153*** -0.121*** -0.150*** 0.295*** 0.451***