I have been banging my head against synthpop rules for a few days now and I can’t quite understand the documentation for defining rules. I needed float columns to obey these rules: float1 <= float2 <= float3. I have tried adjusting the visit sequence and predictor matrix to ensure float1 and float3 are synthesized first but nothing seems to work.
Error: Variable(s) used in missing data rules for float2 have to be synthesised BEFORE the variables they apply to.
library(synthpop)
# Create a dummy dataset
size <- 100 # Number of rows
float1 <- runif(size, min = 0, max = 50)
float2 <- runif(size, min = 0, max = 100)
float3 <- runif(size, min = 50, max = 100)
df <- data.frame(float1 = float1, float2 = float2, float3 = float3)
# Define the rules for restricted values
rules <- list(
float2 = c("float2 < float1", "float2 > float3")
)
rvalues <- list(
float2 = c("float1", "float3")
)
# Set the visit sequence to ensure float1 and float3 are synthesized before float2
visit.sequence <- c("float1", "float3", "float2")
# Adjust predictor matrix to ensure proper dependency
predictor.matrix <- matrix(0, ncol = ncol(df), nrow = ncol(df))
colnames(predictor.matrix) <- rownames(predictor.matrix) <- colnames(df)
predictor.matrix["float2", "float1"] <- 1
predictor.matrix["float2", "float3"] <- 1
# Use the syn function with the custom rule
syn_data <- syn(df, rules = rules, rvalues = rvalues, visit.sequence = visit.sequence, predictor.matrix = predictor.matrix, seed = 123)
New contributor
user26365787 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.