Skip to contents

Introduction

The CodeEff_Matrix function estimates code effects using left and right embeddings from source and target sites. This vignette demonstrates how to use this function with example data provided in the package.


Load the Required Library

Ensure the MUGS package is loaded before running the example:


Load the Data

Load the required datasets for the example:

# Load required data
data(S.1)
data(S.2)
data(U.1)
data(U.2)
data(X.group.source)
data(X.group.target)

Prepare Variables

Prepare the variables required for the CodeEff_Matrix function:

# Set parameters
n1 <- 100
n2 <- 100
p <- 5

# Initial right embeddings
V.1 <- U.1
V.2 <- U.2

# Fix rownames to ensure alignment
n1.no <- n1 - 50  # if you know n.common = 50
rownames(U.1) <- as.character(seq_len(nrow(U.1)))  # "1" to "n1"
rownames(U.2) <- as.character(seq(from = n1.no + 1, length.out = nrow(U.2)))
rownames(S.1) <- rownames(U.1)
rownames(S.2) <- rownames(U.2)
rownames(V.1) <- rownames(U.1)
rownames(V.2) <- rownames(U.2)

# Extract names and find common codes
names.list.1 <- rownames(S.1)
names.list.2 <- rownames(S.2)
common_codes <- intersect(names.list.1, names.list.2)

# Check for overlap
if (length(common_codes) == 0) stop("Error: No common codes found between S.1 and S.2.")

# Create zeta.int
full.name.list <- c(names.list.1, names.list.2)
zeta.int <- matrix(0, length(full.name.list), p)
rownames(zeta.int) <- full.name.list

Run the Function

Run the CodeEff_Matrix function:

# Estimate code effects
CodeEff_Matrix.out <- CodeEff_Matrix(
  S.1=S.1,
  S.2=S.2,
  n1=n1,
  n2=n2,
  U.1=U.1,
  U.2=U.2,
  V.1=U.1,
  V.2=U.2,
  common_codes = common_codes,
  zeta.int = zeta.int,
  lambda=10,
  p=5
  )
#>   [1] "1"   "2"   "3"   "4"   "5"   "6"   "7"   "8"   "9"   "10"  "11"  "12" 
#>  [13] "13"  "14"  "15"  "16"  "17"  "18"  "19"  "20"  "21"  "22"  "23"  "24" 
#>  [25] "25"  "26"  "27"  "28"  "29"  "30"  "31"  "32"  "33"  "34"  "35"  "36" 
#>  [37] "37"  "38"  "39"  "40"  "41"  "42"  "43"  "44"  "45"  "46"  "47"  "48" 
#>  [49] "49"  "50"  "51"  "52"  "53"  "54"  "55"  "56"  "57"  "58"  "59"  "60" 
#>  [61] "61"  "62"  "63"  "64"  "65"  "66"  "67"  "68"  "69"  "70"  "71"  "72" 
#>  [73] "73"  "74"  "75"  "76"  "77"  "78"  "79"  "80"  "81"  "82"  "83"  "84" 
#>  [85] "85"  "86"  "87"  "88"  "89"  "90"  "91"  "92"  "93"  "94"  "95"  "96" 
#>  [97] "97"  "98"  "99"  "100"
#>   [1] "1"   "2"   "3"   "4"   "5"   "6"   "7"   "8"   "9"   "10"  "11"  "12" 
#>  [13] "13"  "14"  "15"  "16"  "17"  "18"  "19"  "20"  "21"  "22"  "23"  "24" 
#>  [25] "25"  "26"  "27"  "28"  "29"  "30"  "31"  "32"  "33"  "34"  "35"  "36" 
#>  [37] "37"  "38"  "39"  "40"  "41"  "42"  "43"  "44"  "45"  "46"  "47"  "48" 
#>  [49] "49"  "50"  "51"  "52"  "53"  "54"  "55"  "56"  "57"  "58"  "59"  "60" 
#>  [61] "61"  "62"  "63"  "64"  "65"  "66"  "67"  "68"  "69"  "70"  "71"  "72" 
#>  [73] "73"  "74"  "75"  "76"  "77"  "78"  "79"  "80"  "81"  "82"  "83"  "84" 
#>  [85] "85"  "86"  "87"  "88"  "89"  "90"  "91"  "92"  "93"  "94"  "95"  "96" 
#>  [97] "97"  "98"  "99"  "100"
#>   [1] "51"  "52"  "53"  "54"  "55"  "56"  "57"  "58"  "59"  "60"  "61"  "62" 
#>  [13] "63"  "64"  "65"  "66"  "67"  "68"  "69"  "70"  "71"  "72"  "73"  "74" 
#>  [25] "75"  "76"  "77"  "78"  "79"  "80"  "81"  "82"  "83"  "84"  "85"  "86" 
#>  [37] "87"  "88"  "89"  "90"  "91"  "92"  "93"  "94"  "95"  "96"  "97"  "98" 
#>  [49] "99"  "100" "101" "102" "103" "104" "105" "106" "107" "108" "109" "110"
#>  [61] "111" "112" "113" "114" "115" "116" "117" "118" "119" "120" "121" "122"
#>  [73] "123" "124" "125" "126" "127" "128" "129" "130" "131" "132" "133" "134"
#>  [85] "135" "136" "137" "138" "139" "140" "141" "142" "143" "144" "145" "146"
#>  [97] "147" "148" "149" "150"
#>   [1] "51"  "52"  "53"  "54"  "55"  "56"  "57"  "58"  "59"  "60"  "61"  "62" 
#>  [13] "63"  "64"  "65"  "66"  "67"  "68"  "69"  "70"  "71"  "72"  "73"  "74" 
#>  [25] "75"  "76"  "77"  "78"  "79"  "80"  "81"  "82"  "83"  "84"  "85"  "86" 
#>  [37] "87"  "88"  "89"  "90"  "91"  "92"  "93"  "94"  "95"  "96"  "97"  "98" 
#>  [49] "99"  "100" "101" "102" "103" "104" "105" "106" "107" "108" "109" "110"
#>  [61] "111" "112" "113" "114" "115" "116" "117" "118" "119" "120" "121" "122"
#>  [73] "123" "124" "125" "126" "127" "128" "129" "130" "131" "132" "133" "134"
#>  [85] "135" "136" "137" "138" "139" "140" "141" "142" "143" "144" "145" "146"
#>  [97] "147" "148" "149" "150"
#> [1] 200   5
#> [1] 200   5

Examine the Output

Explore the structure and key components of the output:

# View structure of the output
str(CodeEff_Matrix.out)
#> List of 4
#>  $ zeta   : num [1:200, 1:5] -0.0398 -0.0586 0.0439 0.2047 -0.0651 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : chr [1:200] "1" "2" "3" "4" ...
#>   .. ..$ : NULL
#>  $ dif_F  : num 0.066
#>  $ V.1.new: num [1:100, 1:5] 0.167 1.378 0.324 0.915 -0.608 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : chr [1:100] "1" "2" "3" "4" ...
#>   .. ..$ : NULL
#>  $ V.2.new: num [1:100, 1:5] 1.3503 1.5287 0.0172 -1.1758 -0.2535 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : chr [1:100] "51" "52" "53" "54" ...
#>   .. ..$ : NULL

# Print specific components of the result
cat("\nEstimated Code Effects (zeta):\n")
#> 
#> Estimated Code Effects (zeta):
print(CodeEff_Matrix.out$zeta[1:5, 1:3])  # Show the first 5 rows and 3 columns of zeta
#>          [,1]         [,2]         [,3]
#> 1 -0.03977591 -0.016382264  0.072139614
#> 2 -0.05861561  0.102410067 -0.063516152
#> 3  0.04393681 -0.115463039 -0.199989211
#> 4  0.20468193 -0.002688834 -0.004118189
#> 5 -0.06513334 -0.153351503 -0.130099178

cat("\nFrobenius Norm Difference (dif_F):\n")
#> 
#> Frobenius Norm Difference (dif_F):
print(CodeEff_Matrix.out$dif_F)
#> [1] 0.06595868

cat("\nUpdated Right Embeddings for Source Site (V.1.new):\n")
#> 
#> Updated Right Embeddings for Source Site (V.1.new):
print(CodeEff_Matrix.out$V.1.new[1:5, 1:3])  # Show first 5 rows and 3 columns of V.1.new
#>         [,1]         [,2]       [,3]
#> 1  0.1665095  0.814170162  0.3663227
#> 2  1.3783222 -1.960960373  0.2720757
#> 3  0.3239034 -0.006156115  0.7451411
#> 4  0.9147213 -0.935517116 -1.1990329
#> 5 -0.6076534  1.054516303  1.0303034

cat("\nUpdated Right Embeddings for Target Site (V.2.new):\n")
#> 
#> Updated Right Embeddings for Target Site (V.2.new):
print(CodeEff_Matrix.out$V.2.new[1:5, 1:3])  # Show first 5 rows and 3 columns of V.2.new
#>           [,1]       [,2]       [,3]
#> 51  1.35027749 -0.9642268  0.4754664
#> 52  1.52874085  0.9799293 -0.5918077
#> 53  0.01721724  0.4860534 -1.8969629
#> 54 -1.17575003  0.6388669  0.2976318
#> 55 -0.25350442 -0.7676733  0.5316585

Notes

  1. Custom Parameters: Modify parameters like n1, n2, p, and lambda to test different scenarios.
  2. Data Preparation: Ensure datasets (S.1, S.2, U.1, U.2, etc.) are correctly loaded and aligned.
  3. Output: Key components include the estimated zeta matrix, Frobenius norm difference, and updated embeddings.

Summary

This vignette demonstrated how to use the CodeEff_Matrix function for estimating code effects. Adjust input parameters and datasets to test different scenarios and interpret the output components for your analysis.