itemresp: Data Structure for Item Response Data

Description Usage Arguments Details Value See Also Examples

View source: R/itemresp.R

Description

A class for representing data from questionnaires along with methods for many generic functions.

Usage

1

Arguments

data

matrix or data frame. A matrix or data frame with integer values or factors where the rows correspond to subjects and the columns to items. See below for details.

mscale

integer or character. A list of vectors (either integer or character) giving the measurement scale. See below for details. By default guessed from data.

labels

character. A vector of character labels for the items. By default, the column names of data are used or, if these are not available, the string "item" along with numbers 1, 2, ... is used.

names

character. A vector of names (or IDs) for the subjects. By default, no subject names are used.

Details

itemresp is designed for item response data of n subjects for k items.

The item responses should be coded in a matrix data with n rows (subjects) and k columns (items). Alternatively, data can be a data frame with n rows (subjects) and k variables (items), which can be either factors or integer valued vectors.

mscale provides the underlying measurement scale either as integer or character vector(s). If all items are measured on the same scale, mscale can be a vector. Alternatively, it can be provided as a named list of vectors for each item. If the list contains one unnamed element, this element will be used as the measurement scale for items that have not been named. Integers or characters not present in mscale but in data will be replaced by NA. All items must be measured with at least 2 categories. By default, mscale is set to the full range of observed values for all integer items (see example below) and the corresponding levels for all factor items in data.

Methods to standard generic functions include: str, length (number of subjects), dim (number of subjects and items), is.na (only TRUE if all item responses are NA for a subject), print (see print.itemresp for details), summary and plot (see summary.itemresp for details), subsetting via [ and subset (see subset.itemresp for details), is.itemresp and various coercion functions to other classes (see as.list.itemresp for details).

Extracting/replacing properties is available through: labels for the item labels, mscale for the measurement scale, names for subject names/IDs.

Value

itemresp returns an object of class "itemresp" which is a matrix (data transformed to integers 0, 1, ...) plus an attribute "mscale" as a named list for each item (after being checked and potentially suitably coerced or transformed to all integer or all character).

See Also

print.itemresp, summary.itemresp, as.list.itemresp, subset.itemresp

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
## binary responses to three items, coded as matrix
x <- cbind(c(1, 0, 1, 0), c(1, 0, 0, 0), c(0, 1, 1, 1))
## transformed to itemresp object
xi <- itemresp(x)

## printing (see also ?print.itemresp)
print(xi)
print(xi, labels = TRUE)

## subsetting/indexing (see also ?subset.itemresp)
xi[2]
xi[c(TRUE, TRUE, FALSE, FALSE)]
subset(xi, items = 1:2)
dim(xi)
length(xi)

## summary/visualization (see also ?summary.itemresp)
summary(xi)
plot(xi)

## query/set measurement scale labels
## extract mscale (tries to collapse to vector)
mscale(xi)
## extract as list
mscale(xi, simplify = FALSE)
## replacement by list
mscale(xi) <- list(item1 = c("no", "yes"),
  item2 = c("nay", "yae"), item3 = c("-", "+"))
xi
mscale(xi)
## replacement with partially named list plus default
mscale(xi) <- list(item1 = c("n", "y"), 0:1)
mscale(xi)
## replacement by vector (if number of categories constant)
mscale(xi) <- c("-", "+")
mscale(xi, simplify = FALSE)

## query/set item labels and subject names
labels(xi)
labels(xi) <- c("i1", "i2", "i3")
names(xi)
names(xi) <- c("John", "Joan", "Jen", "Jim")
print(xi, labels = TRUE)

## coercion (see also ?as.list.itemresp)
## to integer matrix
as.matrix(xi)
## to data frame with single itemresp column
as.data.frame(xi)
## to list of factors
as.list(xi)
## to data frame with factors
as.list(xi, df = TRUE)


## polytomous responses with missing values and unequal number of
## categories in a data frame
d <- data.frame(
  q1 = c(-2, 1, -1, 0, NA, 1, NA),
  q2 = c(3, 5, 2, 5, NA, 2, 3),
  q3 = factor(c(1, 2, 1, 2, NA, 3, 2), levels = 1:3,
    labels = c("disagree", "neutral", "agree")))
di <- itemresp(d)
di

## auto-completion of mscale: full range (-2, ..., 2) for q1, starting
## from smallest observed (negative) value (-2) to the same (positive)
## value (2), full (positive) range for q2, starting from smallest
## observed value (2) to largest observed value (5), missing category of
## 4 is detected, for q3 given factor levels are used
mscale(di)

## set mscale for q2 and add category 1, q1 and q3 are auto-completed:
di <- itemresp(d, mscale = list(q2 = 1:5))

## is.na.itemresp - only true for observation 5 (all missing)
is.na(di)

## illustration for larger data set
data("VerbalAggression", package = "psychotools")
r <- itemresp(VerbalAggression$resp[, 1:12])
str(r)
head(r)
plot(r)
summary(r)
prop.table(summary(r), 1)

## dichotomize response
r2 <- r
mscale(r2) <- c(0, 1, 1)
plot(r2)

## transform to "likert" package
if(require("likert")) {
lik <- likert(as.data.frame(as.list(r)))
lik
plot(lik)
}

Example output

[1] {1,1,0} {0,0,1} {1,0,1} {0,0,1}
[1] {item1:1,item2:1,item3:0} {item1:0,item2:0,item3:1}
[3] {item1:1,item2:0,item3:1} {item1:0,item2:0,item3:1}
[1] {0,0,1}
[1] {1,1,0} {0,0,1}
[1] {1,1} {0,0} {1,0} {0,0}
[1] 4 3
[1] 4
      0 1
item1 2 2
item2 3 1
item3 1 3
[1] 0 1
$item1
[1] 0 1

$item2
[1] 0 1

$item3
[1] 0 1

[1] {yes,yae,-} {no,nay,+}  {yes,nay,+} {no,nay,+} 
$item1
[1] "no"  "yes"

$item2
[1] "nay" "yae"

$item3
[1] "-" "+"

$item1
[1] "n" "y"

$item2
[1] "0" "1"

$item3
[1] "0" "1"

$item1
[1] "-" "+"

$item2
[1] "-" "+"

$item3
[1] "-" "+"

[1] "item1" "item2" "item3"
NULL
            John             Joan              Jen              Jim 
{i1:+,i2:+,i3:-} {i1:-,i2:-,i3:+} {i1:+,i2:-,i3:+} {i1:-,i2:-,i3:+} 
     i1 i2 i3
John  1  1  0
Joan  0  0  1
Jen   1  0  1
Jim   0  0  1
          xi
John {+,+,-}
Joan {-,-,+}
Jen  {+,-,+}
Jim  {-,-,+}
$i1
John Joan  Jen  Jim 
   +    -    +    - 
Levels: - +

$i2
John Joan  Jen  Jim 
   +    -    -    - 
Levels: - +

$i3
John Joan  Jen  Jim 
   -    +    +    + 
Levels: - +

     i1 i2 i3
John  +  +  -
Joan  -  -  +
Jen   +  -  +
Jim   -  -  +
          1           2           3           4           5           6 
{-2,3,dsgr}  {1,5,ntrl} {-1,2,dsgr}  {0,5,ntrl}  {NA,NA,NA}  {1,2,agre} 
          7 
{NA,3,ntrl} 
$q1
[1] "-2" "-1" "0"  "1"  "2" 

$q2
[1] "2" "3" "4" "5"

$q3
[1] "disagree" "neutral"  "agree"   

    1     2     3     4     5     6     7 
FALSE FALSE FALSE FALSE  TRUE FALSE FALSE 
 Item response data from 316 subjects for 12 items.
 S1WantCurse: 0, 1, 2
 S1DoCurse: 0, 1, 2
 S1WantScold: 0, 1, 2
 S1DoScold: 0, 1, 2
 S1WantShout: 0, 1, 2
 S1DoShout: 0, 1, 2
 S2WantCurse: 0, 1, 2
 S2DoCurse: 0, 1, 2
 S2WantScold: 0, 1, 2
 S2DoScold: 0, 1, 2
 S2WantShout: 0, 1, 2
 S2DoShout: 0, 1, 2
[1] {0,1,0,0,0,1,0,1,0,0,0,0} {0,0,0,0,0,0,0,0,0,0,0,0}
[3] {1,0,1,1,1,1,1,0,0,0,1,1} {1,1,1,1,1,1,1,2,1,1,1,1}
[5] {1,1,0,1,1,0,1,1,0,0,0,0} {2,2,2,0,0,0,2,2,0,0,0,0}
              0   1   2
S1WantCurse  91  95 130
S1DoCurse    91 108 117
S1WantScold 126  86 104
S1DoScold   136  97  83
S1WantShout 154  99  63
S1DoShout   208  68  40
S2WantCurse  67 112 137
S2DoCurse   109  97 110
S2WantScold 118  93 105
S2DoScold   162  92  62
S2WantShout 158  84  74
S2DoShout   238  53  25
                    0         1          2
S1WantCurse 0.2879747 0.3006329 0.41139241
S1DoCurse   0.2879747 0.3417722 0.37025316
S1WantScold 0.3987342 0.2721519 0.32911392
S1DoScold   0.4303797 0.3069620 0.26265823
S1WantShout 0.4873418 0.3132911 0.19936709
S1DoShout   0.6582278 0.2151899 0.12658228
S2WantCurse 0.2120253 0.3544304 0.43354430
S2DoCurse   0.3449367 0.3069620 0.34810127
S2WantScold 0.3734177 0.2943038 0.33227848
S2DoScold   0.5126582 0.2911392 0.19620253
S2WantShout 0.5000000 0.2658228 0.23417722
S2DoShout   0.7531646 0.1677215 0.07911392
Loading required package: likert
Loading required package: ggplot2
Loading required package: xtable

psychotools documentation built on Sept. 28, 2021, 5:09 p.m.