| Title: | Two-Sample Calibrated Imputation for Survey Data |
|---|---|
| Description: | Provides utilities for preparing, validating, summarising, and documenting survey data from primary and auxiliary samples, and for specifying and checking survey-design information. The package supports data-management and design-specification steps in two-sample calibrated imputation workflows. For background on survey calibration, see Deville and Särndal (1992) <doi:10.1080/01621459.1992.10475217>. |
| Authors: | Samuel Joel Kamun [aut, cre] |
| Maintainer: | Samuel Joel Kamun <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.1.0 |
| Built: | 2026-11-08 20:44:54 UTC |
| Source: | https://github.com/cran/TwoSampleCalImpute |
Displays a summary of a standardized TSCI data object, including its sample type, number of observations, and number of variables.
## S3 method for class 'tsci_data' print(x, ...)## S3 method for class 'tsci_data' print(x, ...)
x |
A |
... |
Additional arguments (currently unused). |
Invisibly returns x, the original tsci_data object.
The object retains its tsci_data and list classes.
survey_data <- data.frame( id = 1:3, outcome = c(1, 0, 1) ) obj <- tsci_import_data( survey_data, sample_type = "primary", id = "id" ) print(obj)survey_data <- data.frame( id = 1:3, outcome = c(1, 0, 1) ) obj <- tsci_import_data( survey_data, sample_type = "primary", id = "id" ) print(obj)
Displays the sample type, number of observations, and whether sampling weights have been specified for a survey-design object.
## S3 method for class 'tsci_design' print(x, ...)## S3 method for class 'tsci_design' print(x, ...)
x |
A |
... |
Additional arguments (currently unused). |
Invisibly returns x, the original tsci_design object.
The object retains its tsci_design and list classes.
survey_data <- data.frame( id = 1:3, outcome = c(1, 0, 1), weight = c(1, 1, 1) ) data_object <- tsci_import_data( survey_data, sample_type = "primary", id = "id" ) design_object <- tsci_create_design( data_object, weights = "weight" ) print(design_object)survey_data <- data.frame( id = 1:3, outcome = c(1, 0, 1), weight = c(1, 1, 1) ) data_object <- tsci_import_data( survey_data, sample_type = "primary", id = "id" ) design_object <- tsci_create_design( data_object, weights = "weight" ) print(design_object)
Performs controlled data-cleaning operations on a tsci_data
object without silently changing observed values. Completely empty
rows or columns are removed only when the corresponding option is
set to TRUE.
tsci_clean_data(data, remove_empty_rows = FALSE, remove_empty_columns = FALSE)tsci_clean_data(data, remove_empty_rows = FALSE, remove_empty_columns = FALSE)
data |
A |
remove_empty_rows |
Logical; whether completely empty rows
should be removed. Defaults to |
remove_empty_columns |
Logical; whether completely empty
columns should be removed. Defaults to |
A cleaned tsci_data object containing the resulting data,
with its sample type, identifier, and metadata preserved. The
variable list is updated to include only variables still present.
survey_data <- data.frame( id = c(1, 2, 3), outcome = c(1, NA, 0), auxiliary = c(NA, NA, NA) ) imported <- tsci_import_data( survey_data, sample_type = "primary", id = "id" ) cleaned <- tsci_clean_data( imported, remove_empty_columns = TRUE ) names(cleaned$data)survey_data <- data.frame( id = c(1, 2, 3), outcome = c(1, NA, 0), auxiliary = c(NA, NA, NA) ) imported <- tsci_import_data( survey_data, sample_type = "primary", id = "id" ) cleaned <- tsci_clean_data( imported, remove_empty_columns = TRUE ) names(cleaned$data)
Extracts the name of the cluster variable stored in a
tsci_design object. If no cluster variable was specified,
the function returns NULL. The stored specification is
validated against the variables in the design data before the
name is returned.
tsci_clusters(design)tsci_clusters(design)
design |
A |
A character string of length one containing the name
of the cluster variable, or NULL if no cluster variable
was specified. An error is raised if design is not a
tsci_design object, if the stored cluster specification
is invalid, or if the specified variable is absent from the
design data.
survey_data <- data.frame( id = 1:4, outcome = c(1, 0, 1, 0), weight = c(1, 1, 1, 1), cluster = c("C1", "C1", "C2", "C2") ) data_object <- tsci_import_data( survey_data, sample_type = "primary", id = "id" ) design_object <- tsci_create_design( data_object, weights = "weight", clusters = "cluster" ) tsci_clusters(design_object) design_without_clusters <- tsci_create_design( data_object, weights = "weight" ) tsci_clusters(design_without_clusters)survey_data <- data.frame( id = 1:4, outcome = c(1, 0, 1, 0), weight = c(1, 1, 1, 1), cluster = c("C1", "C1", "C2", "C2") ) data_object <- tsci_import_data( survey_data, sample_type = "primary", id = "id" ) design_object <- tsci_create_design( data_object, weights = "weight", clusters = "cluster" ) tsci_clusters(design_object) design_without_clusters <- tsci_create_design( data_object, weights = "weight" ) tsci_clusters(design_without_clusters)
Constructs a tsci_design object from a tsci_data object and
specified survey-design variables. The weight variable must contain
numeric, finite, strictly positive values. If supplied, the
finite-population-correction (FPC) variable must also contain
numeric, finite, strictly positive values.
tsci_create_design(data, weights, strata = NULL, clusters = NULL, fpc = NULL)tsci_create_design(data, weights, strata = NULL, clusters = NULL, fpc = NULL)
data |
A |
weights |
Character string naming the survey-weight variable
in |
strata |
Optional character string naming the stratification
variable in |
clusters |
Optional character string naming the cluster
variable in |
fpc |
Optional character string naming the finite-population-
correction variable in |
An object of class tsci_design, represented as a list
containing the survey-design information constructed from the input
data object. The object includes the survey weights and the
specified design variables, with the underlying data-object
information retained according to the package's design-object
constructor.
survey_data <- data.frame( id = 1:4, outcome = c(1, 0, 1, 0), weight = c(1, 2, 1, 2), stratum = c("A", "A", "B", "B") ) data_object <- tsci_import_data( survey_data, sample_type = "primary", id = "id" ) design_object <- tsci_create_design( data_object, weights = "weight", strata = "stratum" ) print(design_object)survey_data <- data.frame( id = 1:4, outcome = c(1, 0, 1, 0), weight = c(1, 2, 1, 2), stratum = c("A", "A", "B", "B") ) data_object <- tsci_import_data( survey_data, sample_type = "primary", id = "id" ) design_object <- tsci_create_design( data_object, weights = "weight", strata = "stratum" ) print(design_object)
Produces a structured data dictionary describing the variables
in a tsci_data object.
tsci_data_dictionary(data)tsci_data_dictionary(data)
data |
A |
A data frame with one row per variable and five columns:
variable (variable name), class (R class or classes),
n (number of observations), missing (number of missing
values), and description (variable description from the
object's metadata, or NA if no matching description is available).
survey_data <- data.frame( id = 1:3, outcome = c(1, NA, 0), age = c(35, 42, 29) ) imported <- tsci_import_data( survey_data, sample_type = "primary", id = "id", metadata = list( description = c( outcome = "Recorded outcome", age = "Age in years" ) ) ) tsci_data_dictionary(imported)survey_data <- data.frame( id = 1:3, outcome = c(1, NA, 0), age = c(35, 42, 29) ) imported <- tsci_import_data( survey_data, sample_type = "primary", id = "id", metadata = list( description = c( outcome = "Recorded outcome", age = "Age in years" ) ) ) tsci_data_dictionary(imported)
Checks the structural and numerical validity of a tsci_design
object, including its underlying data object, number of observations,
survey weights, stratification variable, and finite-population
correction values.
tsci_design_check(design)tsci_design_check(design)
design |
A |
An object of class c("tsci_design_check", "list")
containing:
Logical value indicating whether all checks pass.
Named list of logical results for the individual structural and numerical checks.
Sample type recorded in the underlying
tsci_data object.
Number of rows in the underlying data.
Identifier information stored in the design object.
Stratification variable information stored in the design object.
Logical value indicating whether survey weights are stored.
Logical value indicating whether finite-population correction values are stored.
The valid field is TRUE only when every individual check passes.
An input that is not a tsci_design object causes an error.
survey_data <- data.frame( id = 1:4, outcome = c(1, 0, 1, 0), weight = c(1, 2, 1, 2), stratum = c("A", "A", "B", "B") ) data_object <- tsci_import_data( survey_data, sample_type = "primary", id = "id" ) design_object <- tsci_create_design( data_object, weights = "weight", strata = "stratum" ) check_result <- tsci_design_check(design_object) check_result$valid check_result$checkssurvey_data <- data.frame( id = 1:4, outcome = c(1, 0, 1, 0), weight = c(1, 2, 1, 2), stratum = c("A", "A", "B", "B") ) data_object <- tsci_import_data( survey_data, sample_type = "primary", id = "id" ) design_object <- tsci_create_design( data_object, weights = "weight", strata = "stratum" ) check_result <- tsci_design_check(design_object) check_result$valid check_result$checks
Produces a base-R plot indicating whether weights, strata,
clusters, and finite-population correction (FPC) values are
specified in a tsci_design object. A component is
displayed as specified when its corresponding stored value
is not NULL.
tsci_design_plot(design)tsci_design_plot(design)
design |
A |
The input tsci_design object, returned invisibly.
The function also produces a plot with one indicator for each
of the four design components: weights, strata, clusters,
and FPC. An error is raised if design is not a
tsci_design object.
survey_data <- data.frame( id = 1:4, outcome = c(1, 0, 1, 0), weight = c(1, 2, 1, 2), stratum = c("A", "A", "B", "B") ) data_object <- tsci_import_data( survey_data, sample_type = "primary", id = "id" ) design_object <- tsci_create_design( data_object, weights = "weight", strata = "stratum" ) returned_design <- tsci_design_plot(design_object) stopifnot(inherits(returned_design, "tsci_design"))survey_data <- data.frame( id = 1:4, outcome = c(1, 0, 1, 0), weight = c(1, 2, 1, 2), stratum = c("A", "A", "B", "B") ) data_object <- tsci_import_data( survey_data, sample_type = "primary", id = "id" ) design_object <- tsci_create_design( data_object, weights = "weight", strata = "stratum" ) returned_design <- tsci_design_plot(design_object) stopifnot(inherits(returned_design, "tsci_design"))
Produces a concise summary of the main components of a
tsci_design object.
tsci_design_summary(design)tsci_design_summary(design)
design |
A |
A one-row data frame with seven columns:
sample_type (type of sample represented by the underlying data),
observations (number of observations),
variables (number of variables in the data),
id (identifier variable name, or NA if none is specified),
weights (logical indicator of whether survey weights are stored),
strata (stratification variable name, or NA if none is specified),
and fpc (logical indicator of whether finite-population-correction
values are stored).
survey_data <- data.frame( id = 1:4, outcome = c(1, 0, 1, 0), weight = c(1, 2, 1, 2), stratum = c("A", "A", "B", "B") ) data_object <- tsci_import_data( survey_data, sample_type = "primary", id = "id" ) design_object <- tsci_create_design( data_object, weights = "weight", strata = "stratum" ) tsci_design_summary(design_object)survey_data <- data.frame( id = 1:4, outcome = c(1, 0, 1, 0), weight = c(1, 2, 1, 2), stratum = c("A", "A", "B", "B") ) data_object <- tsci_import_data( survey_data, sample_type = "primary", id = "id" ) design_object <- tsci_create_design( data_object, weights = "weight", strata = "stratum" ) tsci_design_summary(design_object)
Exports the data contained in a tsci_data object to a CSV file.
tsci_export_data(data, file, row_names = FALSE)tsci_export_data(data, file, row_names = FALSE)
data |
A |
file |
Character path for the output CSV file. The filename must
have a |
row_names |
Logical; whether row names should be written. |
Invisibly returns the original tsci_data object supplied
through data. The object retains its existing data and metadata.
survey_data <- data.frame( id = 1:3, outcome = c(1, 0, 1) ) imported <- tsci_import_data( survey_data, sample_type = "primary", id = "id" ) output_file <- tempfile(fileext = ".csv") returned <- tsci_export_data(imported, file = output_file) file.exists(output_file) identical(returned, imported) unlink(output_file)survey_data <- data.frame( id = 1:3, outcome = c(1, 0, 1) ) imported <- tsci_import_data( survey_data, sample_type = "primary", id = "id" ) output_file <- tempfile(fileext = ".csv") returned <- tsci_export_data(imported, file = output_file) file.exists(output_file) identical(returned, imported) unlink(output_file)
Extracts and validates the finite-population-correction (FPC)
values stored in a tsci_design object.
tsci_fpc(design)tsci_fpc(design)
design |
A |
A numeric vector containing one finite, strictly positive
FPC value per observation, in the stored order, or NULL
if no FPC was specified. An error is raised if design
is not a tsci_design object, or if the stored FPC is
non-numeric, has an incorrect length, contains missing or
non-finite values, or contains non-positive values.
survey_data <- data.frame( id = 1:4, outcome = c(1, 0, 1, 0), weight = c(1, 2, 1, 2), fpc = c(100, 100, 200, 200) ) data_object <- tsci_import_data( survey_data, sample_type = "primary", id = "id" ) design_object <- tsci_create_design( data_object, weights = "weight", fpc = "fpc" ) tsci_fpc(design_object) design_without_fpc <- tsci_create_design( data_object, weights = "weight" ) tsci_fpc(design_without_fpc)survey_data <- data.frame( id = 1:4, outcome = c(1, 0, 1, 0), weight = c(1, 2, 1, 2), fpc = c(100, 100, 200, 200) ) data_object <- tsci_import_data( survey_data, sample_type = "primary", id = "id" ) design_object <- tsci_create_design( data_object, weights = "weight", fpc = "fpc" ) tsci_fpc(design_object) design_without_fpc <- tsci_create_design( data_object, weights = "weight" ) tsci_fpc(design_without_fpc)
Imports a data set from a supported file or an existing R object
and returns it as a standardized tsci_data object.
tsci_import_data( data, sample_type = c("primary", "auxiliary"), id = NULL, variables = NULL, metadata = list() )tsci_import_data( data, sample_type = c("primary", "auxiliary"), id = NULL, variables = NULL, metadata = list() )
data |
A data frame, or a character path to a CSV file. |
sample_type |
Either |
id |
Optional character identifier variable. |
variables |
Optional character vector of variables to retain. |
metadata |
Optional list of metadata. |
A tsci_data object, represented as a list containing the
imported data, sample type, identifier, variable names, and metadata.
survey_data <- data.frame( id = 1:3, outcome = c(1, 0, 1), age = c(35, 42, 29) ) imported <- tsci_import_data( survey_data, sample_type = "primary", id = "id" ) print(imported)survey_data <- data.frame( id = 1:3, outcome = c(1, 0, 1), age = c(35, 42, 29) ) imported <- tsci_import_data( survey_data, sample_type = "primary", id = "id" ) print(imported)
Merge two TSCI data objects
tsci_merge_data(x, y, by, all = FALSE)tsci_merge_data(x, y, by, all = FALSE)
x |
A |
y |
A second |
by |
Character vector of variables used as merge keys. |
all |
Logical value indicating whether to retain unmatched
observations from both objects. Defaults to |
Merges two tsci_data objects using one or more key variables.
A tsci_data object containing the merged data. The result
retains the sample type and metadata from x; its identifier is
retained only if that identifier is present in the merged data.
primary_data <- data.frame( id = c(1, 2, 3), outcome = c(1, 0, 1) ) auxiliary_data <- data.frame( id = c(1, 2, 3), age_group = c("young", "middle", "older") ) primary <- tsci_import_data( primary_data, sample_type = "primary", id = "id" ) auxiliary <- tsci_import_data( auxiliary_data, sample_type = "auxiliary", id = "id" ) merged <- tsci_merge_data( primary, auxiliary, by = "id" ) names(merged$data)primary_data <- data.frame( id = c(1, 2, 3), outcome = c(1, 0, 1) ) auxiliary_data <- data.frame( id = c(1, 2, 3), age_group = c("young", "middle", "older") ) primary <- tsci_import_data( primary_data, sample_type = "primary", id = "id" ) auxiliary <- tsci_import_data( auxiliary_data, sample_type = "auxiliary", id = "id" ) merged <- tsci_merge_data( primary, auxiliary, by = "id" ) names(merged$data)
Produces a variable-level summary of missing and observed values
in a tsci_data object.
tsci_missing_summary(data, variables = NULL)tsci_missing_summary(data, variables = NULL)
data |
A |
variables |
Optional character vector of variables to summarize.
If |
A data frame with one row per selected variable and five
columns: variable (variable name), n (total number of
observations), missing (number of missing values), observed
(number of non-missing values), and missing_percent (percentage
of observations with missing values, on a 0–100 scale).
survey_data <- data.frame( id = 1:4, outcome = c(1, NA, 0, NA), group = c("A", "B", "A", "B") ) imported <- tsci_import_data( survey_data, sample_type = "primary", id = "id" ) tsci_missing_summary(imported, variables = "outcome")survey_data <- data.frame( id = 1:4, outcome = c(1, NA, 0, NA), group = c("A", "B", "A", "B") ) imported <- tsci_import_data( survey_data, sample_type = "primary", id = "id" ) tsci_missing_summary(imported, variables = "outcome")
Prepares a tsci_data object for subsequent validation and
Two-Sample Calibrated Imputation analysis by applying the
specified empty-row and empty-column cleaning operations.
tsci_prepare_data( data, remove_empty_rows = FALSE, remove_empty_columns = FALSE )tsci_prepare_data( data, remove_empty_rows = FALSE, remove_empty_columns = FALSE )
data |
A |
remove_empty_rows |
Logical; whether completely empty rows
should be removed. Defaults to |
remove_empty_columns |
Logical; whether completely empty
columns should be removed. Defaults to |
A prepared tsci_data object. Its data reflect the
requested cleaning operations; its sample type, identifier,
and metadata are preserved by the cleaning operation.
survey_data <- data.frame( id = c(1, 2, 3), outcome = c(1, NA, 0), empty_variable = c(NA, NA, NA) ) imported <- tsci_import_data( survey_data, sample_type = "primary", id = "id" ) prepared <- tsci_prepare_data( imported, remove_empty_columns = TRUE ) names(prepared$data)survey_data <- data.frame( id = c(1, 2, 3), outcome = c(1, NA, 0), empty_variable = c(NA, NA, NA) ) imported <- tsci_import_data( survey_data, sample_type = "primary", id = "id" ) prepared <- tsci_prepare_data( imported, remove_empty_columns = TRUE ) names(prepared$data)
Applies user-supplied recoding functions to one or more variables
in a tsci_data object.
tsci_recode_variables(data, recodes)tsci_recode_variables(data, recodes)
data |
A |
recodes |
A named list of functions. Each name must identify
a variable in |
A tsci_data object containing the recoded variables.
The sample type, identifier, variable list, and metadata are
preserved from the input object.
survey_data <- data.frame( id = 1:4, response = c("Yes", "No", "Yes", "No") ) imported <- tsci_import_data( survey_data, sample_type = "primary", id = "id" ) recoded <- tsci_recode_variables( imported, recodes = list(response = function(x) { ifelse(x == "Yes", 1, 0) }) ) recoded$data$responsesurvey_data <- data.frame( id = 1:4, response = c("Yes", "No", "Yes", "No") ) imported <- tsci_import_data( survey_data, sample_type = "primary", id = "id" ) recoded <- tsci_recode_variables( imported, recodes = list(response = function(x) { ifelse(x == "Yes", 1, 0) }) ) recoded$data$response
Extracts the name of the stratification variable stored in a
tsci_design object. If no stratification variable was
specified, the function returns NULL. The stored
specification is validated against the variables in the
design data before the name is returned.
tsci_strata(design)tsci_strata(design)
design |
A |
A character string of length one containing the name
of the stratification variable, or NULL if no
stratification variable was specified. An error is raised if
design is not a tsci_design object, if the stored
stratification specification is invalid, or if the specified
variable is absent from the design data.
survey_data <- data.frame( id = 1:4, outcome = c(1, 0, 1, 0), weight = c(1, 1, 1, 1), stratum = c("A", "A", "B", "B") ) data_object <- tsci_import_data( survey_data, sample_type = "primary", id = "id" ) design_object <- tsci_create_design( data_object, weights = "weight", strata = "stratum" ) tsci_strata(design_object) design_without_strata <- tsci_create_design( data_object, weights = "weight" ) tsci_strata(design_without_strata)survey_data <- data.frame( id = 1:4, outcome = c(1, 0, 1, 0), weight = c(1, 1, 1, 1), stratum = c("A", "A", "B", "B") ) data_object <- tsci_import_data( survey_data, sample_type = "primary", id = "id" ) design_object <- tsci_create_design( data_object, weights = "weight", strata = "stratum" ) tsci_strata(design_object) design_without_strata <- tsci_create_design( data_object, weights = "weight" ) tsci_strata(design_without_strata)
Subsets observations in a tsci_data object using a logical
condition while preserving the TSCI data-object structure.
tsci_subset_data(data, subset)tsci_subset_data(data, subset)
data |
A |
subset |
A logical vector identifying observations to retain. |
A tsci_data object containing the selected observations.
The sample type, identifier, and metadata are preserved, and the
variable list is restricted to variables present in the result.
survey_data <- data.frame( id = 1:4, outcome = c(1, 0, 1, 0) ) imported <- tsci_import_data( survey_data, sample_type = "primary", id = "id" ) selected <- tsci_subset_data( imported, subset = c(TRUE, FALSE, TRUE, FALSE) ) nrow(selected$data) selected$data$idsurvey_data <- data.frame( id = 1:4, outcome = c(1, 0, 1, 0) ) imported <- tsci_import_data( survey_data, sample_type = "primary", id = "id" ) selected <- tsci_subset_data( imported, subset = c(TRUE, FALSE, TRUE, FALSE) ) nrow(selected$data) selected$data$id
Applies a user-supplied transformation function to one or more
variables in a tsci_data object while preserving the TSCI data
object structure.
tsci_transform_data(data, variables, fun, ...)tsci_transform_data(data, variables, fun, ...)
data |
A |
variables |
Character vector of variable names to transform. |
fun |
A function used to transform each selected variable. |
... |
Additional arguments passed to |
A tsci_data object containing the transformed variables.
x <- data.frame(id = 1:3, y = c(2, 4, 6)) obj <- tsci_import_data(x, sample_type = "primary", id = "id") tsci_transform_data(obj, variables = "y", fun = log)x <- data.frame(id = 1:3, y = c(2, 4, 6)) obj <- tsci_import_data(x, sample_type = "primary", id = "id") tsci_transform_data(obj, variables = "y", fun = log)
Updates selected components of an existing tsci_design object.
Non-NULL values supplied for weights, strata, and fpc
replace the corresponding stored components. The clusters
argument is passed to the design-object constructor, so omitting it
does not guarantee that a previously specified cluster setting is
retained.
tsci_update_design( design, weights = NULL, strata = NULL, clusters = NULL, fpc = NULL )tsci_update_design( design, weights = NULL, strata = NULL, clusters = NULL, fpc = NULL )
design |
A |
weights |
Optional character string naming the survey-weight
variable in the underlying data. If |
strata |
Optional character string naming the stratification
variable in the underlying data. If |
clusters |
Optional character string naming the cluster
variable in the underlying data. Defaults to |
fpc |
Optional character string naming the finite-population-
correction variable in the underlying data. If |
An object of class tsci_design containing the updated
survey-design components and the underlying data-object
information. The weights and FPC values are checked for numeric,
finite, strictly positive values when their variable names are
supplied. Invalid design inputs cause an error.
survey_data <- data.frame( id = 1:4, outcome = c(1, 0, 1, 0), weight = c(1, 2, 1, 2), updated_weight = c(2, 2, 3, 3), stratum = c("A", "A", "B", "B") ) data_object <- tsci_import_data( survey_data, sample_type = "primary", id = "id" ) design_object <- tsci_create_design( data_object, weights = "weight", strata = "stratum" ) updated_design <- tsci_update_design( design_object, weights = "updated_weight" ) print(updated_design)survey_data <- data.frame( id = 1:4, outcome = c(1, 0, 1, 0), weight = c(1, 2, 1, 2), updated_weight = c(2, 2, 3, 3), stratum = c("A", "A", "B", "B") ) data_object <- tsci_import_data( survey_data, sample_type = "primary", id = "id" ) design_object <- tsci_create_design( data_object, weights = "weight", strata = "stratum" ) updated_design <- tsci_update_design( design_object, weights = "updated_weight" ) print(updated_design)
Performs data-integrity checks before Two-Sample Calibrated Imputation analysis, including input validation, optional variable and weight checks, missingness summaries, and duplicate-key checks.
tsci_validate_data( data, data_name = "data", variables = NULL, weights = NULL, key = NULL )tsci_validate_data( data, data_name = "data", variables = NULL, weights = NULL, key = NULL )
data |
A data frame to validate. |
data_name |
A character label used in validation messages.
Defaults to |
variables |
Optional character vector of variables to validate.
Defaults to |
weights |
Optional survey weights to validate. Defaults to |
key |
Optional character vector identifying records for duplicate
checks. Defaults to |
An object of class c("tsci_validation", "list") containing
valid (validation status), data_name (the supplied data label),
n (number of rows), p (number of columns), variables (column
names), missing (missingness-check results), and duplicates
(duplicate-key-check results). Invalid inputs cause an error.
survey_data <- data.frame( id = 1:4, outcome = c(1, 0, NA, 1), weight = c(1, 1, 1, 1) ) validation <- tsci_validate_data( survey_data, variables = c("outcome", "weight"), weights = survey_data$weight, key = "id" ) validation$valid validation$n validation$missingsurvey_data <- data.frame( id = 1:4, outcome = c(1, 0, NA, 1), weight = c(1, 1, 1, 1) ) validation <- tsci_validate_data( survey_data, variables = c("outcome", "weight"), weights = survey_data$weight, key = "id" ) validation$valid validation$n validation$missing
Produces descriptive summaries for selected variables in a
tsci_data object.
tsci_variable_summary(data, variables = NULL)tsci_variable_summary(data, variables = NULL)
data |
A |
variables |
Optional character vector of variables to summarize.
If |
A data frame with one row per selected variable and five
columns: variable (variable name), class (R class),
n (number of observations), missing (number of missing
values), and unique (number of distinct non-missing values).
survey_data <- data.frame( id = 1:4, outcome = c(1, 0, NA, 1), group = c("A", "B", "A", "B") ) imported <- tsci_import_data( survey_data, sample_type = "primary", id = "id" ) tsci_variable_summary(imported, variables = "outcome")survey_data <- data.frame( id = 1:4, outcome = c(1, 0, NA, 1), group = c("A", "B", "A", "B") ) imported <- tsci_import_data( survey_data, sample_type = "primary", id = "id" ) tsci_variable_summary(imported, variables = "outcome")
Extracts and validates the numeric survey weights stored in a
tsci_design object.
tsci_weights(design)tsci_weights(design)
design |
A |
A numeric vector containing the survey weights in their stored order. The vector has one element per observation and contains only finite, strictly positive values. An error is raised if the design object is invalid, weights are absent, weights are non-numeric, or any weight is missing, non-finite, or non-positive.
survey_data <- data.frame( id = 1:4, outcome = c(1, 0, 1, 0), weight = c(1, 2, 1, 2) ) data_object <- tsci_import_data( survey_data, sample_type = "primary", id = "id" ) design_object <- tsci_create_design( data_object, weights = "weight" ) tsci_weights(design_object)survey_data <- data.frame( id = 1:4, outcome = c(1, 0, 1, 0), weight = c(1, 2, 1, 2) ) data_object <- tsci_import_data( survey_data, sample_type = "primary", id = "id" ) design_object <- tsci_create_design( data_object, weights = "weight" ) tsci_weights(design_object)