@@ -1437,85 +1437,101 @@ umx_factor <- umxFactor
14371437# ' @param na.rm Whether to delete NAs when computing scores (Default = TRUE) Note: Choice affects mean!
14381438# ' @param minManifests If score = factor, how many missing items to tolerate for an individual?
14391439# ' @param alpha print Cronbach's alpha? (TRUE)
1440- # ' @param mapStrings For input like True/False can map to 0,1 NULL
1440+ # ' @param mapStrings For input like "No"/"Maybe"/"Yes" -> 0,1,2
14411441# ' @return - scores
14421442# ' @export
14431443# ' @family Data Functions
14441444# ' @md
14451445# ' @examples
14461446# ' library(psych)
1447+ # ' library(psychTools)
14471448# ' data(bfi)
1448- # '
1449+ # '
14491450# ' # ==============================
14501451# ' # = Score Agreeableness totals =
14511452# ' # ==============================
1452- # '
1453+ # '
14531454# ' # Handscore subject 1
1454- # ' # A1(Reversed) + A2 + A3 + A4 + A5
1455- # ' # (6+1)-2 + 4 + 3 + 4 + 4 = 20
1456- # '
1455+ # ' # A1(R)+A2+A3+A4+A5 = (6+1)-2 +4+3+4+4 = 20
1456+ # '
14571457# ' tmp = umx_score_scale("A", pos = 2:5, rev = 1, max = 6, data= bfi, name = "A")
1458- # ' tmp[1, namez(tmp, "A",ignore.case= FALSE)]
1458+ # ' tmp[1, namez(tmp, "A",ignore.case = FALSE)]
14591459# ' # A1 A2 A3 A4 A5 A
14601460# ' # 2 4 3 4 4 20
1461- # '
1462- # ' # =================================================================================
1463- # ' # = Note: (as of a fix in 2020-05-08) items not reversed in the returned data set =
1464- # ' # =================================================================================
1465- # ' tmp = umx_score_scale("A", pos = 1, rev = 2:5, max = 6, data= bfi, name = "A")
1466- # ' tmp[1, namez(tmp, "A",ignore.case=FALSE)]
1467- # ' # A1 A2 A3 A4 A5 A
1468- # ' # 2 4 3 4 4 = 15
1469- # '
1461+ # '
1462+ # ' # ====================
1463+ # ' # = Request the mean =
1464+ # ' # ====================
14701465# ' tmp = umx_score_scale("A", pos = 2:5, rev = 1, max = 6, data= bfi, name = "A", score="mean")
1471- # ' tmp$A[1] # subject 1 mean = 4
1472- # '
1473- # ' # ===========================================
1474- # ' # = How does mean react to a missing value? =
1475- # ' # ===========================================
1466+ # ' tmp$A[1] # = 4
1467+ # '
1468+ # ' # ==================
1469+ # ' # = na.rm = TRUE ! =
1470+ # ' # ==================
14761471# ' tmpDF = bfi
14771472# ' tmpDF[1, "A1"] = NA
1478- # ' tmp = umx_score_scale("A", pos = 2:5, rev = 1, max = 6, data= tmpDF, name = "A", score="mean")
1479- # ' tmp$A [1] # NA: (na.rm defaults to FALSE)
1473+ # ' tmp = umx_score_scale("A", pos = 2:5, rev = 1, max = 6, data= tmpDF, score="mean")
1474+ # ' tmp$A_score [1] # 3.75
14801475# '
1481- # ' tmp = umx_score_scale("A", pos = 2:5, rev = 1, max = 6, data= tmpDF,
1482- # ' name = "A", score="mean", na.rm=TRUE )
1483- # ' tmp$A [1] # 3.75
1476+ # ' tmp= umx_score_scale("A", pos= 2:5, rev= 1, max = 6, data = tmpDF,
1477+ # ' score="mean", na.rm=FALSE )
1478+ # ' tmp$A_score [1] # NA (reject cases with missing items)
14841479# '
14851480# ' # ===============
14861481# ' # = Score = max =
14871482# ' # ===============
1488- # ' tmp = umx_score_scale("A", pos = 2:5, rev = 1, max = 6, data= bfi, name = "A", score="max")
1489- # ' tmp$A[1] # subject 1 max = 5 (the reversed item 1)
1483+ # ' tmp = umx_score_scale("A", pos = 2:5, rev = 1, max = 6,
1484+ # ' data = bfi, name = "A", score = "max")
1485+ # ' tmp$A[1] # Subject 1 max = 5 (reversed) item 1
14901486# '
1491- # ' tmp = umx_score_scale("E", pos = c(3,4,5), rev = c(1,2), max = 6, data= tmp)
1492- # ' tmp$E_score[1] # default scale name
1487+ # ' # Default scale name
1488+ # ' tmp = umx_score_scale("E", pos = 3:5, rev = 1:2, max = 6,
1489+ # ' data= tmp, score = "mean", na.rm=FALSE)
1490+ # ' tmp$E_score[1]
14931491# '
14941492# ' # Using @BillRevelle's psych package: More diagnostics, including alpha
14951493# ' scores= psych::scoreItems(items = bfi, min = 1, max = 6, keys = list(
14961494# ' E = c("-E1","-E2", "E3", "E4", "E5"),
14971495# ' A = c("-A1", "A2", "A3", "A4", "A5")
14981496# ' ))
14991497# ' summary(scores)
1500- # ' scores$scores[1,]
1498+ # ' scores$scores[1, ]
15011499# ' # E A
15021500# ' # 3.8 4.0
15031501# '
15041502# ' # Compare output
15051503# ' # (note, by default psych::scoreItems replaces NAs with the sample median...)
1506- # ' RevelleE = as.numeric(scores$scores[,"E"]) * 5
1504+ # ' RevelleE = as.numeric(scores$scores[,"E"])
15071505# ' all(RevelleE == tmp[,"E_score"], na.rm = TRUE)
15081506# '
1509- umx_score_scale <- function (base = NULL , pos = NULL , rev = NULL , min = 1 , max = NULL , data = NULL , score = c(" total" , " mean" , " max" , " factor" ), name = NULL , na.rm = FALSE , minManifests = NA , alpha = FALSE , mapStrings = NULL ) {
1507+ # ' # =======================
1508+ # ' # = MapStrings examples =
1509+ # ' # =======================
1510+ # ' mapStrings = c(
1511+ # ' "Very Inaccurate", "Moderately Inaccurate",
1512+ # ' "Slightly Inaccurate", "Slightly Accurate",
1513+ # ' "Moderately Accurate", "Very Accurate")
1514+ # ' bfi$As1 = factor(bfi$A1, levels = 1:6, labels = mapStrings)
1515+ # ' bfi$As2 = factor(bfi$A2, levels = 1:6, labels = mapStrings)
1516+ # ' bfi$As3 = factor(bfi$A3, levels = 1:6, labels = mapStrings)
1517+ # ' bfi$As4 = factor(bfi$A4, levels = 1:6, labels = mapStrings)
1518+ # ' bfi$As5 = factor(bfi$A5, levels = 1:6, labels = mapStrings)
1519+ # ' bfi= umx_score_scale(name="A" , base="A", pos=2:5, rev=1, max=6, data=bfi)
1520+ # ' bfi= umx_score_scale(name="As", base="As", pos=2:5, rev=1, mapStrings = mapStrings, data= bfi)
1521+ # ' all(bfi$A == bfi$As)
1522+ # '
1523+ # ' # copes with bad name requests
1524+ # ' umx_score_scale(base = "NotPresent", pos=2:5, rev=1, max=6, data=bfi)
1525+ umx_score_scale <- function (base = NULL , pos = NULL , rev = NULL , min = 1 , max = NULL , data = NULL , score = c(" total" , " mean" , " max" , " factor" ), name = NULL , na.rm = TRUE , minManifests = NA , alpha = FALSE , mapStrings = NULL ) {
15101526 score = match.arg(score )
15111527 if (is.null(name )){ name = paste0(base , " _score" ) }
15121528 oldData = data
1513-
1529+ umx_check_names( namesNeeded = paste0( base , c( pos , rev )), data = data )
15141530 if (! is.null(mapStrings )){
15151531 if (! is.null(max )){
1516- # check min max matches mapstrings
1532+ # check min max matches mapStrings
15171533 if (! (length(mapStrings ) == length(min : max ))){
1518- stop(paste0(" polite note: You set the max and min, but " , min , " to " , max , " must equal the number of map strings: " , length(mapStrings )))
1534+ stop(paste0(" You set the max and min, but " , min , " to " , max , " must equal the number of map strings: " , length(mapStrings )))
15191535 }
15201536 }else {
15211537 min = 1
@@ -1529,8 +1545,8 @@ umx_score_scale <- function(base= NULL, pos = NULL, rev = NULL, min= 1, max = NU
15291545 notFound = unique_values [which(! (unique_values %in% mapStrings ))]
15301546 stop(" Some values in column " , omxQuotes(thisCol ), " not in mapStrings, e.g.. :" , omxQuotes(notFound ))
15311547 }
1532- data [, thisCol ] = factor (data [, thisCol , drop = TRUE ], labels = mapStrings , levels = mapStrings )
1533- data [, thisCol ] = as.numeric(data [, thisCol , drop = TRUE ] )
1548+ tmp = factor (data [, thisCol , drop = TRUE ], levels = mapStrings , labels = min : max )
1549+ data [, thisCol ] = as.numeric(as.character( tmp ) )
15341550 }
15351551 }
15361552 mins = umx_apply(" min" , data [ , paste0(base , c(pos , rev )), drop = FALSE ], by = " columns" , na.rm = TRUE )
@@ -3469,7 +3485,7 @@ umx_update_OpenMx <- install.OpenMx
34693485# ' @description
34703486# ' Easily run devtools "install", "release", "win", "examples" etc.
34713487# '
3472- # ' @param what whether to "install", "release" to CRAN, check on "win", "check", "rhub", "spell", or check "examples"))
3488+ # ' @param what whether to "install", "release" to CRAN, "test", " check" test on "win" or "rhub", "spell", or "examples"))
34733489# ' @param pkg the local path to your package. Defaults to my path to umx.
34743490# ' @param check Whether to run check on the package before release (default = TRUE).
34753491# ' @param run If what is "examples", whether to also run examples marked don't run. (default FALSE)
@@ -3494,13 +3510,17 @@ umx_update_OpenMx <- install.OpenMx
34943510# ' umx_make(what = "release") # Release to CRAN
34953511# ' tmp = umx_make(what = "lastRhub") # View rhub result
34963512# ' }
3497- umx_make <- function (what = c(" quick_install" , " install_full" , " spell" , " run_examples" , " check" , " win" , " rhub" , " lastRhub" , " release" , " travisCI" , " sitrep" ), pkg = " ~/bin/umx" , check = TRUE , run = FALSE , start = NULL , spelling = " en_US" , which = c(" win" , " mac" , " linux" , " solaris" ), spell = TRUE ) {
3498- what = match.arg(what )
3513+ umx_make <- function (what = c(" load " , " quick_install" , " install_full" , " spell" , " run_examples" , " check" , " test " , " win" , " rhub" , " lastRhub" , " release" , " travisCI" , " sitrep" ), pkg = " ~/bin/umx" , check = TRUE , run = FALSE , start = NULL , spelling = " en_US" , which = c(" win" , " mac" , " linux" , " solaris" ), spell = TRUE ) {
3514+ what = match.arg(what )
34993515 which = match.arg(which )
35003516 if (what == " lastRhub" ){
35013517 prev = rhub :: list_package_checks(package = pkg , howmany = 4 )
35023518 check_id = prev $ id [1 ]
35033519 return (rhub :: get_check(check_id ))
3520+ }else if (what == " test" ){
3521+ devtools :: test(pkg = pkg )
3522+ }else if (what == " load" ){
3523+ devtools :: load_all(path = pkg )
35043524 }else if (what == " install_full" ){
35053525 devtools :: document(pkg = pkg ); devtools :: install(pkg = pkg );
35063526 devtools :: load_all(path = pkg )
0 commit comments