# CodeSiteEff_I2_par_usage

## Introduction

The `CodeSiteEff_I2_par` function estimates site-specific effects using input embeddings and penalization methods. This vignette demonstrates how to utilize the function with appropriate input data and parameters.

## Load the Required Library

Ensure the `MUGS` package is loaded before running the example:

    library(MUGS)

## Load the Data

Load the required datasets for the example:

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

## Prepare Variables

Set up the variables required for the `CodeSiteEff_I2_par` function:

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

    # Ensure row and column names are consistent for matching
    rownames(U.1) <- as.character(seq_len(nrow(U.1)))  # "1" to "100"
    rownames(U.2) <- as.character(seq(from = 51, length.out = nrow(U.2)))  # "51" to "150"

    # Align S.1 and S.2 with embeddings
    rownames(S.1) <- rownames(U.1)
    colnames(S.1) <- rownames(U.1)

    rownames(S.2) <- rownames(U.2)
    colnames(S.2) <- rownames(U.2)

    # Get common codes
    names.list.1 <- rownames(S.1)
    names.list.2 <- rownames(S.2)
    common_codes <- intersect(names.list.1, names.list.2)
    n.common <- length(common_codes)

    if (n.common == 0) stop("Error: No common codes found between source and target sites.")

    full.name.list <- c(names.list.1, names.list.2)

    # Initialize delta matrix
    delta.int <- matrix(0, length(full.name.list), p)
    rownames(delta.int) <- full.name.list

## Run the Function

Run the `CodeSiteEff_I2_par` function:

    # Estimate site-specific effects
    CodeSiteEff_l2_par.out <-  CodeSiteEff_l2_par(
      S.1 = S.1,
      S.2 = S.2,
      n1 = 100,
      n2 = 100,
      U.1 = U.1,
      U.2 = U.2,
      V.1= U.1,
      V.2 = U.2,
      delta.int = delta.int,
      lambda.delta = 3000,
      p=5,
      common_codes = common_codes,
      n.common = 50,
      n.core=2)
    #> Warning: package 'doSNOW' was built under R version 4.4.1
    #> Loading required package: foreach
    #> Warning: package 'foreach' was built under R version 4.4.1
    #> Loading required package: iterators
    #> Loading required package: snow
    #>
    #> Attaching package: 'snow'
    #> The following objects are masked from 'package:parallel':
    #>
    #>     closeNode, clusterApply, clusterApplyLB, clusterCall, clusterEvalQ,
    #>     clusterExport, clusterMap, clusterSplit, makeCluster, parApply,
    #>     parCapply, parLapply, parRapply, parSapply, recvData, recvOneData,
    #>     sendData, splitIndices, stopCluster

## Examine the Output

Explore the structure and key components of the output:

    # View structure of the output
    str(CodeSiteEff_l2_par.out)
    #> List of 3
    #>  $ delta  : num [1:200, 1:5] 0 0 0 0 0 0 0 0 0 0 ...
    #>   ..- attr(*, "dimnames")=List of 2
    #>   .. ..$ : chr [1:200] "1" "2" "3" "4" ...
    #>   .. ..$ : NULL
    #>  $ V.1.new: num [1:100, 1:5] 0.206 1.437 0.28 0.71 -0.543 ...
    #>   ..- attr(*, "dimnames")=List of 2
    #>   .. ..$ : chr [1:100] "1" "2" "3" "4" ...
    #>   .. ..$ : NULL
    #>  $ V.2.new: num [1:100, 1:5] 0.468 1.595 -0.152 -1.13 -0.165 ...
    #>   ..- attr(*, "dimnames")=List of 2
    #>   .. ..$ : chr [1:100] "51" "52" "53" "54" ...
    #>   .. ..$ : NULL

    # Print specific components of the result
    cat("\nEstimated Effects (Delta):\n")
    #>
    #> Estimated Effects (Delta):
    print(CodeSiteEff_l2_par.out$delta[1:5, 1:5])  # First 5 rows and columns of delta matrix
    #>   [,1] [,2] [,3] [,4] [,5]
    #> 1    0    0    0    0    0
    #> 2    0    0    0    0    0
    #> 3    0    0    0    0    0
    #> 4    0    0    0    0    0
    #> 5    0    0    0    0    0

    cat("\nRegularization Path:\n")
    #>
    #> Regularization Path:
    print(CodeSiteEff_l2_par.out$path)
    #> NULL

## Notes

1. Custom Parameters: Modify parameters like `n1`, `n2`, `p`, and `lambda.delta` 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 delta matrix and the regularization path.

## Summary

This vignette demonstrated how to use the `CodeSiteEff_l2_par` function for estimating site-specific effects. Adjust input parameters and datasets to test different scenarios and interpret the output components for your analysis.