scores = function(...,fcn='mean',minvalid=1,score=NULL,minval=0,maxval=0) # # scores() calculates scores according to the argument of fcn using variables # given at ... . If the number of valid values of ... is less than minvalid # the resulting score will be set to missing. # # If fcn='mean' arguments of score= can be used to request a transformation of # the scores to z-scores, to scores centered at the overall mean, or to POMP # (= percent of maximum possible) scores with 0 and 100 as minimum and maximum # possible values (see: Cohen, P., Cohen, J., Aiken, L.S. & West, S.G. (1999). # The problem of units and the circumstance for POMP. Multivariate Behavioral # Research, 34, 315-346). The latter requires to specify the minimum and maxi- # mum possible values of the items given at ... by using minval= and maxval=. # # fcn : possible arguments are 'min', 'max', 'sum', 'sd', and 'mean' # (default: 'mean') # score : score= can only be specified if fcn='mean'. Possible arguments (trans- # formations) are 'centered' or 'c' (mean scores centered at the overall # mean), 'z' (z-scores), or 'pomp' or 'p' (pomp scores). The latter # requires to specify the minimum and maximum possible value of ... by # using minval= and maxval=, as well. # # Examples: see "http://www2.jura.uni-hamburg.de/instkrim/kriminologie/Mitarbeiter/Enzmann/Software/test_sc.r" { if (!(fcn %in% c('min','max','sd','sum','mean'))) { stop(paste("'",fcn,"' is no valid argument of fcn",sep="")) } if (length(score)>0) { if (fcn!='mean') { stop(paste("score='",score,"' is only valid if fcn='mean'",sep="")) } if (!(score %in% c('centered','c','z','p','pomp'))) { stop(paste("'",score,"' is no valid argument of score",sep="")) } if (((score=='pomp') | (score=='p')) & (minval==maxval)) { stop("minval and maxval options should allow minval to be less than maxval") } } if (minvalid < 1) { stop("value specified in minvalid may not by less than 1") } temp = cbind(...) if (dim(temp)[2] < minvalid) { stop(paste("value specified in minvalid (= ",minvalid,") may not be greater than\nthe number of variables (= ",dim(temp)[2],")",sep="")) } valid = apply(!is.na(temp),1,sum) sc = suppressWarnings(apply(temp,1,fcn,na.rm=T)) sc[valid < minvalid] = NA if (length(score)>0) { if ((score=='centered') | (score=='c')) { sc = sc-mean(sc,na.rm=T) } if (score=='z') { sc.sd = sd(sc,na.rm=T) if (sc.sd==0) { sc = NA stop("variance of scores = 0") } else { sc = (sc-mean(sc,na.rm=T))/sc.sd } } if ((score=='pomp') | (score=='p')) { sc = 100*(sc-minval)/(maxval-minval) if (min(temp,na.rm=T) < minval) { warning("Value(s) of variables less than minval!") } if (max(temp,na.rm=T) > maxval) { warning("Value(s) of variables greater than maxval!") } } } sc }