Skip to content

Commit

Permalink
resolved vignette response_prop issues
Browse files Browse the repository at this point in the history
  • Loading branch information
markolalovic committed Jun 20, 2024
1 parent b6fc063 commit 97cd60b
Show file tree
Hide file tree
Showing 13 changed files with 200 additions and 70 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
export(discretize_density)
export(estimate_params)
export(plot_likert_transform)
export(response_prop)
export(rlikert)
export(simulate_likert)
52 changes: 29 additions & 23 deletions R/utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,37 @@ plot_likert_transform <- function(n_items, n_levels, mean=0, sd=1, skew=0) {
}
}

#' Calculate Response Proportions
#'
#' Returns a table of proportions for each possible response category.
#'
#' @param data numeric vector or matrix of responses.
#' @param n_levels number of response categories.
#' @return A table of response category proportions.
#' @examples
#' data <- c(1, 2, 2, 3, 3, 3)
#' response_prop(data, n_levels = 3)
#'
#' data_matrix <- matrix(c(1, 2, 2, 3, 3, 3), ncol = 2)
#' response_prop(data_matrix, n_levels = 3)
#' @export
response_prop <- function(data, n_levels) {
if (is.vector(data)) {
tab <- pad_levels(prop.table(table(data)), n_levels)
} else {
tab <- t(apply(data, 2, function(x_col) {
pad_levels(prop.table(table(x_col)), n_levels)
}))
dimnames(tab) <- list(Item = rownames(tab), Response = colnames(tab))
}
return(tab)
}

#' Pad Missing Levels
#'
#' Takes a vector of proportions or probabilities across possible responses
#' and pads the missing levels with zeros up to the specified number
#' of response categories.
#' Helper function that takes a vector of proportions or probabilities
#' across possible responses and pads the missing levels with zeros up
#' to the specified number of response categories.
#'
#' @param pr proportions or probabilities across possible responses.
#' @param n_levels number of response categories.
Expand Down Expand Up @@ -204,26 +230,6 @@ cor2cov <- function(corr, s) {
return(diag(s) %*% corr %*% diag(s))
}

#' Get Proportion Table
#'
#' Returns a table of proportions for each possible response.
#'
#' @param data numeric vector or matrix. Responses.
#' @param K integer. Number of response categories.
#' @return numeric vector or matrix. Table of proportions.
#' @noRd
get_prop_table <- function(data, K) {
if (is.vector(data)) {
tab <- pad_levels(prop.table(table(data)), K)
} else {
tab <- t(apply(data, 2, function(x_col) {
pad_levels(prop.table(table(x_col)), K)
}))
dimnames(tab) <- list(Item = rownames(tab), Response = colnames(tab))
}
return(tab)
}

#' Generate Random Centered Parameters
#'
#' Generates random centered parameters for mu, sd, and skewness.
Expand Down
3 changes: 1 addition & 2 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ knitr::opts_chunk$set(

<!-- badges: start -->
[![R-CMD-check](https://github.com/markolalovic/latent2likert/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/markolalovic/latent2likert/actions/workflows/R-CMD-check.yaml)
[![Codecov test coverage](https://codecov.io/gh/markolalovic/latent2likert/branch/main/graph/badge.svg)](https://app.codecov.io/gh/markolalovic/latent2likert?branch=main)
[![codecov](https://codecov.io/gh/markolalovic/latent2likert/branch/main/graph/badge.svg?token=HZTG6RUB2J)](https://codecov.io/gh/markolalovic/latent2likert)
<!-- badges: end -->


## Overview

The R package **latent2likert** is designed to effectively simulate the discretization process inherent to Likert scales while minimizing distortion. It converts continuous latent variables into ordinal categories to generate Likert scale item responses. This is particularly useful for accurately modeling and analyzing survey data that use Likert scales, especially when applying statistical techniques that require metric data.
Expand Down
21 changes: 1 addition & 20 deletions dev/fix_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,13 @@
Script adds:
- `/docs/.nojekyll` for githubpages.
- meta description to index.html
- meta description to index.html DONE
After running pkgdown::build_site()
'''

import os
import sys
import fileinput

def add_meta():
filename = './docs/index.html'
meta_line = '<meta name="description" content="Converting latent variables into Likert scale responses in R. Package latent2likert converts continuous latent variables into ordinal categories to simulate Likert scale item responses." />'

with open(filename, 'r') as f:
in_file = f.readlines()

out_file = []
for line in in_file:
out_file.append(line)
if '</title>' in line:
out_file.append(meta_line)

with open(filename, 'w') as f:
f.writelines(out_file)

if __name__ == '__main__':
os.system("touch ./docs/.nojekyll")
add_meta()
print("Done.")
14 changes: 7 additions & 7 deletions docs/articles/using_latent2likert.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pkgdown: 2.0.9
pkgdown_sha: ~
articles:
using_latent2likert: using_latent2likert.html
last_built: 2024-06-20T21:04Z
last_built: 2024-06-20T21:59Z
urls:
reference: https://markolalovic.github.io/latent2likert/reference
article: https://markolalovic.github.io/latent2likert/articles
Expand Down
5 changes: 5 additions & 0 deletions docs/reference/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 97cd60b

Please sign in to comment.