SkillAgentSearch skills...

FactorEx

R package factorEx: Design and Analysis for Factorial Experiments

Install / Use

/learn @naoki-egami/FactorEx
About this skill

Quality Score

0/100

Category

Design

Supported Platforms

Universal

README

factorEx: Design and Analysis for Factorial Experiments

Description:

R package factorEx provides design-based and model-based estimators for the population average marginal component effects (the pAMCE) in factorial experiments, including conjoint analysis. The package also implements a series of recommendations offered in de la Cuesta, Egami, and Imai (2022, PA) and Egami and Imai (2019, JASA).

Authors:

References:

Installation Instructions

factorEx is available on CRAN and can be installed using:

install.packages("factorEx")

You can also install the most recent development version using the devtools package. First you have to install devtools using the following code. Note that you only have to do this once:

if(!require(devtools)) install.packages("devtools")

Then, load devtools and use the function install_github() to install factorEx:

library(devtools)
install_github("naoki-egami/factorEx", dependencies=TRUE)

Examples

  • Design-based Confirmatory Analysis
    • Case 1: Use Marginal Distributions for Target Profile Distribution
    • Case 2: Use Combination of Marginal and Partial Joint Distributions for Target Profile Distribution
  • Model-based Exploratory Analysis

(1) Design-based Confirmatory Analysis

Here, we use the conjoint experiment that randomized profiles according to the marginal population randomization design.

Case 1: Use Marginal Distributions for Target Profile Distributions

When using marginal distributions, target_dist should be a list and each element should have a factor name. Within each list, a numeric vector should have the same level names as those in data.

## Load the package and data
library(factorEx)
data("OnoBurden")

OnoBurden_data_pr <- OnoBurden$OnoBurden_data_pr # randomization based on marginal population design

# we focus on target profile distributions based on Democratic legislators. 
# See de la Cuesta, Egami, and Imai (2019+) for details.
target_dist_marginal <- OnoBurden$target_dist_marginal

target_dist_marginal
## $gender
##      Male    Female 
## 0.6778243 0.3221757 
## 
## $age
## 36 years old 44 years old 52 years old 60 years old 68 years old 76 years old 
##   0.05020921   0.13807531   0.23012552   0.22594142   0.25104603   0.10460251 
## 
## $family
## Single (never married)      Single (divorced)     Married (no child) 
##             0.07729469             0.03864734             0.12560386 
## Married (two children) 
##             0.75845411 
## 
## $race
##          White       Hispanic Asian American          Black 
##      0.6725664      0.1283186      0.0000000      0.1991150 
## 
## $experience
##      None   4 years   8 years  12 years 
## 0.1966527 0.2259414 0.1548117 0.4225941 
## 
## $party
## Dem Rep 
##   1   0 
## 
## $pos_security
##     Cut military budget Maintain strong defense 
##              0.98557692              0.01442308

We can estimate the pAMCE with design_pAMCE with target_type = "marginal". Use factor_name to specify for which factors we estimate the pAMCE.

out_design_mar <- 
  design_pAMCE(formula = Y ~ gender + age + family + race + experience + party + pos_security,
               factor_name = c("gender", "age", "experience"),
               data = OnoBurden_data_pr,
               pair_id = OnoBurden_data_pr$pair_id,
               cluster_id = OnoBurden_data_pr$id,
               target_dist  = target_dist_marginal, target_type = "marginal")
summary(out_design_mar)
## 
## ----------------
## Population AMCEs:
## ----------------
##  target_dist     factor        level     Estimate  Std. Error p value    
##       target     gender       Female  0.027987587 0.005861738   0.000 ***
##       target        age 44 years old  0.019219282 0.014421828   0.183    
##       target        age 52 years old -0.008792916 0.013765415   0.523    
##       target        age 60 years old -0.006826945 0.013875303   0.623    
##       target        age 68 years old  0.011247969 0.013569292   0.407    
##       target        age 76 years old -0.052741541 0.014775629   0.000 ***
##       target experience     12 years  0.041672460 0.007627281   0.000 ***
##       target experience      4 years  0.046173813 0.008868432   0.000 ***
##       target experience      8 years  0.040752213 0.009313376   0.000 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Use plot to visualize the estimated pAMCEs.

plot(out_design_mar, factor_name = c("gender", "experience"))

Case 2: Use Combination of Marginal and Partial Joint Distributions for Target Profile Distribution

The use of partial joint distributions is useful because it can relax the assumption of no three-way or higher-order interactions (see de la Cuesta, Egami, and Imai (2019+)).

When using a combination of marginal and partial joint distributions, target_dist should be a list and each element should be a numeric vector (if marginal) or an array/table (if partial joint). Then, use argument partial_joint_name to specify which factors are marginal and partial joints. In the following example, c("gender", "age", "family") has the partial joint distributions over the three factors. race and party are based on the marginal distributions, respectively. c("experience", "pos_security") has the partial joint distributions over the two factors. Within each list, a numeric vector or an array/table should have the same level names as those in data.

target_dist_partial <- OnoBurden$target_dist_partial
target_dist_partial
## $`gender:age:family`
## , , family = Single (never married)
## 
##         age
## gender   36 years old 44 years old 52 years old 60 years old 68 years old
##   Male    0.004184100  0.004184100  0.004184100  0.004184100  0.008368201
##   Female  0.000000000  0.004184100  0.004184100  0.008368201  0.016736402
##         age
## gender   76 years old
##   Male    0.004184100
##   Female  0.004184100
## 
## , , family = Single (divorced)
## 
##         age
## gender   36 years old 44 years old 52 years old 60 years old 68 years old
##   Male    0.004184100  0.000000000  0.004184100  0.004184100  0.004184100
##   Female  0.000000000  0.004184100  0.004184100  0.004184100  0.000000000
##         age
## gender   76 years old
##   Male    0.000000000
##   Female  0.004184100
## 
## , , family = Married (no child)
## 
##         age
## gender   36 years old 44 years old 52 years old 60 years old 68 years old
##   Male    0.008368201  0.008368201  0.025104603  0.008368201  0.029288703
##   Female  0.000000000  0.000000000  0.004184100  0.008368201  0.012552301
##         age
## gender   76 years old
##   Male    0.000000000
##   Female  0.004184100
## 
## , , family = Married (two children)
## 
##         age
## gender   36 years old 44 years old 52 years old 60 years old 68 years old
##   Male    0.025104603  0.079497908  0.117154812  0.092050209  0.092050209
##   Female  0.004184100  0.020920502  0.050209205  0.062761506  0.033472803
##         age
## gender   76 years old
##   Male    0.041841004
##   Female  0.037656904
## 
## 
## $race
##          White       Hispanic Asian American          Black 
##      0.6725664      0.1283186      0.0000000      0.1991150 
## 
## $party
## Dem Rep 
##   1   0 
## 
## $`experience:pos_security`
##           pos_security
## experience Cut military budget Maintain strong defense
##   None             0.066945607             0.000000000
##   4 years          0.221757322             0.004184100
##   8 years          0.154811715             0.000000000
##   12 years         0.414225941             0.008368201
partial_joint_name  <- list(c("gender", "age", "family"), "race", "party", c("experience", "pos_security"))

We can estimate the pAMCE with design_pAMCE with target_type = "partial_joint" and appropriate partial_joint_name. The function can use factor_name to specify for which factors we estimate the pAMCE.

out_design_par <- 
      design_pAMCE(formula = Y ~ gender + age + family + race + experience + party + pos_security,
                   factor_name = c("gender", "age", "race"),
                   data = OnoBurden_data_pr,
                   pair_id = OnoBurden_data_pr$pair_id,
                   cluster_id = OnoBurden_data_pr$id,
                   target_dist  = target_dist_partial, target_type = "partial_joint",
                   partial_joint_name = partial_joint_name)
summary(out_design_par)
## 
## ----------------
## Population AMCEs:
## ----------------
##  target_dist factor        level     Estimate  Std. Error p value    
##       target gender       Fe

Related Skills

View on GitHub
GitHub Stars8
CategoryDesign
Updated4mo ago
Forks3

Languages

R

Security Score

67/100

Audited on Nov 13, 2025

No findings