dospert is a pacakge used to aid the analysis of DOSPERT data. It includes three functions:
make sure you have devtools
installed and run the following code:
library(devtools) devtools::install_github("schafik/dospert")
DOSPERT has five risk domains and three risk types. These are taken as arguments for d_sum, and are used for variable names.
Risk domains are abbreviated as follows:
Risk types are:
Data for analysis is raw data downloaded from qualtrics. File formats supported are .csv and .xml, with .csv as default. The main difference is that when downloaded, .csv files include first row of column names/questions but .xml files do not. There is no need to manipulate the variable names before using any of the functions. However, for the responses of the dospert questions, variable names should be in "(domain)(risk type)_Question number" (e. g. finRT_1) format.
# setwd("/git_repositories/betterment/data/raw_data/") #reading in libraries############################################## library(readr) library(plyr) library(dplyr) library(XML) library(reshape2) library(xtable) library(texreg) library(stargazer) library(dospert)
dcsv <- read.csv("pilot_data.csv", header = TRUE) # [d]ospert in [csv] head(dcsv[, 1:10])
dxml <- xmlToDataFrame("pilot_data.xml", stringsAsFactors = F) %>% filter(uid != "") # [d]ospert in [xml] # unique identifying variable for this dataset is 'uid' # removed observations without uid variable value head(dxml[, 1:10])
d_clean function takes three arguments: raw dataframe, unique identifying variable and file type and returns a panel format dataframe, which can be used, for example, for data inspection purposes.
# source("/git_repositories/betterment_project/dospert/R/d_clean.R") # source("/git_repositories/betterment_project/dospert/R/d_sum.R") # source("/git_repositories/betterment_project/dospert/R/d_score.R")
In the sample .csv format dataframe, first column uniquely identifies the respondents.
csvdclean <- d_clean(dcsv, "V1", file_type = "csv") head(csvdclean)
For the .xml format dataframe, the usage is similar: unique identifying variable in this sample dataset is 'uid', and file type is xml.
xmldclean <- d_clean(dxml, "uid", file_type = "xml") head(xmldclean)
d_sum takes five arguments: raw dataframe, unique identifying variable, risk domain, risk type (or scale) and file type, and returns the sum of the respondents DOSPERT response of the designated risk domain and type by unique id.
csvdsum <- d_sum(dcsv, "V1", "fin", "RT", file_type = "csv") head(csvdsum)
xmldsum <- d_sum(dxml, "uid", "fin", "RB", file_type = "xml") head(xmldsum)
d_score takes same three arguments as d_clean, calculates the risk attitude coefficients and returns the results attached to the last columns of input dataframe.
The risk attitude coefficients are named "(domain)_int'', "(domain)_RB'', "(domain)_RP''
csvdscore <- d_score(dcsv, "V1", file_type = "csv") head(csvdscore %>% dplyr::select(unique_id, fin_int, fin_RB, fin_RP))
xmldscore <- d_score(dxml, "uid", file_type = "xml") head(xmldscore %>% dplyr::select(unique_id, fin_int, fin_RB, fin_RP))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.