Please consider the following code to obtain scores from the alpha
function
library(psych)
vars <- c('mpg', 'cyl', 'disp', 'hp')
df <- mtcars[0:10, vars]
alpha_results <- alpha(df, check.keys=TRUE)
df$scores <- alpha_results$scores
print(head(df))
mpg cyl disp hp scores
21.0 6 160 110 154.750
21.0 6 160 110 154.750
22.8 4 108 93 136.550
21.4 6 258 110 179.150
18.7 8 360 175 222.075
18.1 6 225 105 170.475
Item statistics
n raw.r std.r r.cor r.drop mean sd
mpg- 10 0.86 0.94 0.91 0.85 343.6 2.9
cyl 10 0.91 0.96 0.94 0.91 5.8 1.5
disp 10 0.98 0.93 0.90 0.85 208.6 90.4
hp 10 0.94 0.95 0.94 0.85 122.8 51.4
Based on the doc of the function:
Scores are by default simply the average response for all items that a participant took. If cumulative=TRUE, then these are sum scores. Note, this is dangerous if there are lots of missing values
However, for example, the score for the first obs should be
(-21.0 + 6 + 160 + 110)/4 = 63.75
But the result was 154.750 instead.
The alpha
function is not really meant for finding scores of data with drastically different ranges. Based upon the normal use case of items with equal ranges, it adjusts reversed scored items by subtracting those items from the maximum value + minimum value. In the case of the cars example, that is 360 + 4 or 364. Thus, the first value is (364 - 21 + 6 + 160 + 110)/4 = 154.75 as reported.
The scoreItems
function will find the local min and max (as does alpha
); or you can specify them.
To get what you want, you should use the scoreItems
function and specify the keys as well as min=0
and max=0
.
Note that I use the cs
function (from psychTools
, adapted from Hmisc
) to add ""
in the keys <- list
command
keys <- list(cars=cs(-mpg, cyl,disp,hp))
test1 <-scoreItems(keys,df,min=0,max=0)
test1$scores
cars
Mazda RX4 63.750
Mazda RX4 Wag 63.750
Datsun 710 45.550
Hornet 4 Drive 88.150
Hornet Sportabout 131.075