我的调查有20个问题。每5个问题就会进入综合指数。我想为每个索引计算Cronbach的Alpha,并希望在一个表中看到它们,就像这样。
Index1 Index2 Index3
Alpha .3 .9 .4
我可以弄清楚如何为每个索引单独执行此操作:
cronbach(x[,1:5])
但不是表格。我认为这表明我并不理解一个真正基本的R,所以请随意提出我应该研究的事情。
谢谢!
答案 0 :(得分:0)
如果数据存储在矩阵" x"中,您可以形成一个三维数组" A"然后使用" apply":
library(psy)
library(data.table)
#############################################
# Example data:
set.seed(1)
x <- matrix(sample(1:100,200,replace=TRUE),10,20)
#############################################
# cronbach-Test:
A <- array(x,c(nrow(x),5,ncol(x)/5))
cbTest <- apply(A,3,"cronbach")
setDT(cbTest)
setnames(cbTest,colnames(cbTest),paste(rep("Index",dim(A)[3]),1:(dim(A)[3])))
#############################################
# Result:
cbTest
#############################################
# Just for comparison:
cronbach(x[,1:5])
cronbach(x[,6:10])
cronbach(x[,11:15])
cronbach(x[,16:20])
&#34; A [,, 1]&#34;是矩阵&#34; x [,1:5]&#34;,&#34; A [,, 2]&#34;矩阵&#34; x [,6:11]&#34;等等。 &#34;应用&#34;沿着第三维度将Cronbach-Test应用于这些矩阵中的每一个并形成一个列表&#34; cbTest&#34;。然后&#34; setDT&#34;将此列表转换为表格:
> #############################################
> # Result:
> cbTest
Index 1 Index 2 Index 3 Index 4
1: 10 10 10 10
2: 5 5 5 5
3: -0.983849 -3.324005 0.3437152 -3.317533
> #############################################
> # Just for comparison:
> cronbach(x[,1:5])
$sample.size
[1] 10
$number.of.items
[1] 5
$alpha
[1] -0.983849
> cronbach(x[,6:10])
$sample.size
[1] 10
$number.of.items
[1] 5
$alpha
[1] -3.324005
> cronbach(x[,11:15])
$sample.size
[1] 10
$number.of.items
[1] 5
$alpha
[1] 0.3437152
> cronbach(x[,16:20])
$sample.size
[1] 10
$number.of.items
[1] 5
$alpha
[1] -3.317533
>
这是矩阵&#34; x&#34;我在这个例子中使用过:
> x
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
[1,] 27 21 94 49 83 48 92 34 44 24 66 98 100
[2,] 38 18 22 60 65 87 30 84 72 6 36 74 50
[3,] 58 69 66 50 79 44 46 35 40 65 28 36 49
[4,] 91 39 13 19 56 25 34 34 33 88 100 44 18
[5,] 21 77 27 83 53 8 66 48 76 78 64 15 76
[6,] 90 50 39 67 79 10 26 90 21 80 22 2 46
[7,] 95 72 2 80 3 32 48 87 72 46 13 72 52
[8,] 67 100 39 11 48 52 77 39 13 42 48 11 21
[9,] 63 39 87 73 74 67 9 78 25 82 93 45 23
[10,] 7 78 35 42 70 41 88 97 15 61 60 65 60
[,14] [,15] [,16] [,17] [,18] [,19] [,20]
[1,] 58 69 62 29 64 30 72
[2,] 8 61 56 90 85 20 39
[3,] 4 24 33 45 86 89 11
[4,] 65 26 46 78 40 51 93
[5,] 93 73 51 89 39 88 29
[6,] 60 46 19 42 90 19 60
[7,] 57 18 53 7 65 76 12
[8,] 53 75 8 34 75 73 85
[9,] 99 11 28 73 61 95 32
[10,] 51 87 22 34 91 55 79
>
这就是阵列&#34; A&#34;:
> A
, , 1
[,1] [,2] [,3] [,4] [,5]
[1,] 27 21 94 49 83
[2,] 38 18 22 60 65
[3,] 58 69 66 50 79
[4,] 91 39 13 19 56
[5,] 21 77 27 83 53
[6,] 90 50 39 67 79
[7,] 95 72 2 80 3
[8,] 67 100 39 11 48
[9,] 63 39 87 73 74
[10,] 7 78 35 42 70
, , 2
[,1] [,2] [,3] [,4] [,5]
[1,] 48 92 34 44 24
[2,] 87 30 84 72 6
[3,] 44 46 35 40 65
[4,] 25 34 34 33 88
[5,] 8 66 48 76 78
[6,] 10 26 90 21 80
[7,] 32 48 87 72 46
[8,] 52 77 39 13 42
[9,] 67 9 78 25 82
[10,] 41 88 97 15 61
, , 3
[,1] [,2] [,3] [,4] [,5]
[1,] 66 98 100 58 69
[2,] 36 74 50 8 61
[3,] 28 36 49 4 24
[4,] 100 44 18 65 26
[5,] 64 15 76 93 73
[6,] 22 2 46 60 46
[7,] 13 72 52 57 18
[8,] 48 11 21 53 75
[9,] 93 45 23 99 11
[10,] 60 65 60 51 87
, , 4
[,1] [,2] [,3] [,4] [,5]
[1,] 62 29 64 30 72
[2,] 56 90 85 20 39
[3,] 33 45 86 89 11
[4,] 46 78 40 51 93
[5,] 51 89 39 88 29
[6,] 19 42 90 19 60
[7,] 53 7 65 76 12
[8,] 8 34 75 73 85
[9,] 28 73 61 95 32
[10,] 22 34 91 55 79
>