library(scienceverse)
#> 
#> ************
#> Welcome to scienceverse For support and examples visit:
#> http://scienceverse.github.io/
#> - Get and set global package options with: scienceverse_options()
#> ************

Codebooks in scienceverse follow the Psych-DS 0.1.0 format by default as set up in the function faux::codebook(), although you can manually add your own codebook to the JSON file.

Automatic Codebooks for Simulated Data

When you add simulated data to a study object, the codebook is generated by default.

study <- study("Demo") %>%
  add_sim_data("pets",
               between = list(pet = c(cat = "Cat Owners", 
                                      dog = "Dog Owners")),
               n = c(4, 6),
               dv = list(happy = "Happiness Score"),
               id = list(id = "Subject ID"),
               mu = c(10, 12),
               sd = 4)
#> id set to dataType string
#> pet set to dataType string
#> happy set to dataType float

You can retrieve the codebook with the function get_codebook(). If you don’t specify the data_id, it defaults to the first data set in the study.

cb <- get_codebook(study, data_id = "pets")

If you just type the codebook object into the console, you’ll see the info formatted like this.

cb
#> Codebook for pets (Psych-DS 0.1.0)
#> 
#> Dataset Parameters
#> 
#> * name: pets
#> * schemaVersion: Psych-DS 0.1.0
#> 
#> Column Parameters
#> 
#> * id (string): Subject ID
#> * pet (string)
#>   * Levels
#>     * cat: Cat Owners
#>     * dog: Dog Owners
#>   * Ordered: FALSE
#> * happy (float): Happiness Score

But the codebook is actually a nested list formatted like this:

str(cb)
#> List of 5
#>  $ @context        : chr "https://schema.org/"
#>  $ @type           : chr "Dataset"
#>  $ name            : chr "pets"
#>  $ schemaVersion   : chr "Psych-DS 0.1.0"
#>  $ variableMeasured:List of 3
#>   ..$ :List of 4
#>   .. ..$ @type      : chr "PropertyValue"
#>   .. ..$ name       : chr "id"
#>   .. ..$ description: chr "Subject ID"
#>   .. ..$ dataType   : chr "string"
#>   ..$ :List of 6
#>   .. ..$ @type        : chr "PropertyValue"
#>   .. ..$ name         : chr "pet"
#>   .. ..$ description  : chr "pet"
#>   .. ..$ levels       :List of 2
#>   .. .. ..$ cat: chr "Cat Owners"
#>   .. .. ..$ dog: chr "Dog Owners"
#>   .. ..$ dataType     : chr "string"
#>   .. ..$ levelsOrdered: logi FALSE
#>   ..$ :List of 4
#>   .. ..$ @type      : chr "PropertyValue"
#>   .. ..$ name       : chr "happy"
#>   .. ..$ description: chr "Happiness Score"
#>   .. ..$ dataType   : chr "float"
#>  - attr(*, "class")= chr [1:2] "psychds_codebook" "list"

You can also view it in JSON format.

get_codebook(study, data_id = "pets", as_json = TRUE)
#> {
#>     "@context": "https://schema.org/",
#>     "@type": "Dataset",
#>     "name": "pets",
#>     "schemaVersion": "Psych-DS 0.1.0",
#>     "variableMeasured": [
#>         {
#>             "@type": "PropertyValue",
#>             "name": "id",
#>             "description": "Subject ID",
#>             "dataType": "string"
#>         },
#>         {
#>             "@type": "PropertyValue",
#>             "name": "pet",
#>             "description": "pet",
#>             "levels": {
#>                 "cat": "Cat Owners",
#>                 "dog": "Dog Owners"
#>             },
#>             "dataType": "string",
#>             "levelsOrdered": false
#>         },
#>         {
#>             "@type": "PropertyValue",
#>             "name": "happy",
#>             "description": "Happiness Score",
#>             "dataType": "float"
#>         }
#>     ]
#> }
#> 

Run Codebook on Existing Data

You can run the codebook function on existing data, but will need to manually input some things. For now, the supported properties are: “description”, “privacy”, “dataType”, “propertyID”, “minValue”, “maxValue”, “levels”, “levelsOrdered”, “na”, “naValue”, “alternateName”, “unitCode” and “unitText”. You can set unsupported properties, but this will produce a warning.



vardesc <- list(
  description = list(
    mpg = "Miles/(US) gallon",
    cyl = "Number of cylinders",
    disp = "Displacement (cu.in.)",
    hp = "Gross horsepower",
    drat = "Rear axle ratio",
    wt = "Weight (1000 lbs)",
    qsec = "1/4 mile time",
    vs = "Engine (0 = V-shaped, 1 = straight)",
    am = "Transmission (0 = automatic, 1 = manual)",
    gear = "Number of forward gears",
    carb = "Number of carburetors"
  ),
  # min and max values can be set manually or from data
  # min and max are often outside the observed range
  minValue = list(mpg = 0, cyl = min(mtcars$cyl)),
  maxValue = list(cyl = max(mtcars$cyl)),
  levels = list(
    vs = c("0" = "V-shaped", "1" = "straight"),
    am = c("0" = "automatic", "1" = "manual")
  ),
  dataType = list(
    cyl = "integer",
    hp = "integer",
    vs = "factor",
    am = "factor",
    gear = "integer",
    carb = "integer"
  )
)

codebook(mtcars, "Motor Trend Car Road Tests", vardesc)
#> mpg set to dataType float
#> disp set to dataType float
#> drat set to dataType float
#> wt set to dataType float
#> qsec set to dataType float
#> {
#>     "@context": "https://schema.org/",
#>     "@type": "Dataset",
#>     "name": "Motor Trend Car Road Tests",
#>     "schemaVersion": "Psych-DS 0.1.0",
#>     "variableMeasured": [
#>         {
#>             "@type": "PropertyValue",
#>             "name": "mpg",
#>             "description": "Miles/(US) gallon",
#>             "minValue": 0,
#>             "dataType": "float"
#>         },
#>         {
#>             "@type": "PropertyValue",
#>             "name": "cyl",
#>             "description": "Number of cylinders",
#>             "minValue": 4,
#>             "maxValue": 8,
#>             "dataType": "int"
#>         },
#>         {
#>             "@type": "PropertyValue",
#>             "name": "disp",
#>             "description": "Displacement (cu.in.)",
#>             "dataType": "float"
#>         },
#>         {
#>             "@type": "PropertyValue",
#>             "name": "hp",
#>             "description": "Gross horsepower",
#>             "dataType": "int"
#>         },
#>         {
#>             "@type": "PropertyValue",
#>             "name": "drat",
#>             "description": "Rear axle ratio",
#>             "dataType": "float"
#>         },
#>         {
#>             "@type": "PropertyValue",
#>             "name": "wt",
#>             "description": "Weight (1000 lbs)",
#>             "dataType": "float"
#>         },
#>         {
#>             "@type": "PropertyValue",
#>             "name": "qsec",
#>             "description": "1/4 mile time",
#>             "dataType": "float"
#>         },
#>         {
#>             "@type": "PropertyValue",
#>             "name": "vs",
#>             "description": "Engine (0 = V-shaped, 1 = straight)",
#>             "levels": {
#>                 "0": "V-shaped",
#>                 "1": "straight"
#>             },
#>             "dataType": "string",
#>             "levelsOrdered": false
#>         },
#>         {
#>             "@type": "PropertyValue",
#>             "name": "am",
#>             "description": "Transmission (0 = automatic, 1 = manual)",
#>             "levels": {
#>                 "0": "automatic",
#>                 "1": "manual"
#>             },
#>             "dataType": "string",
#>             "levelsOrdered": false
#>         },
#>         {
#>             "@type": "PropertyValue",
#>             "name": "gear",
#>             "description": "Number of forward gears",
#>             "dataType": "int"
#>         },
#>         {
#>             "@type": "PropertyValue",
#>             "name": "carb",
#>             "description": "Number of carburetors",
#>             "dataType": "int"
#>         }
#>     ]
#> }
#>