Skip to content

Commit 1b3257f

Browse files
authored
docs: update README with missing data strategies and advanced usage (#11)
- Add instructions on how to use the new missing_strategy parameter. - Include examples and notes on clamping, adaptive learning rate, etc.
1 parent c7b584d commit 1b3257f

1 file changed

Lines changed: 85 additions & 5 deletions

File tree

README.md

Lines changed: 85 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
21
# IrtRuby
32

4-
IrtRuby is a Ruby gem that provides implementations of the Rasch model, the Two-Parameter model, and the Three-Parameter model for Item Response Theory (IRT). It allows you to estimate the abilities of individuals and the difficulties of items based on their responses to a set of items.
3+
IrtRuby is a Ruby gem that provides implementations of the **Rasch model**, the **Two-Parameter (2PL)** model, and the **Three-Parameter (3PL)** model for Item Response Theory (IRT). It allows you to estimate the **abilities** of individuals and the **difficulties** (and optionally **discriminations** and **guessing** parameters) of items based on their responses.
54

65
## Installation
76

@@ -25,14 +24,18 @@ gem install irt_ruby
2524

2625
## Usage
2726

28-
Here's an example of how to use the IrtRuby gem:
27+
Here's a quick example using the Rasch model:
2928

3029
```ruby
3130
require 'irt_ruby'
3231
require 'matrix'
3332

3433
# Create a sample response matrix
35-
data = Matrix[[1, 0, 1], [0, 1, 0], [1, 1, 1]]
34+
data = Matrix[
35+
[1, 0, 1],
36+
[0, 1, 0],
37+
[1, 1, 1]
38+
]
3639

3740
# Initialize the Rasch model with the response data
3841
model = IrtRuby::RaschModel.new(data)
@@ -41,9 +44,86 @@ model = IrtRuby::RaschModel.new(data)
4144
result = model.fit
4245

4346
# Output the estimated abilities and difficulties
44-
puts "Abilities: #{result[:abilities]}"
47+
puts "Abilities: #{result[:abilities]}"
48+
puts "Difficulties: #{result[:difficulties]}"
49+
```
50+
### Using 2PL and 3PL Models
51+
```ruby
52+
two_pl_model = IrtRuby::TwoParameterModel.new(data)
53+
two_pl_result = two_pl_model.fit
54+
puts two_pl_result[:abilities]
55+
puts two_pl_result[:difficulties]
56+
puts two_pl_result[:discriminations]
57+
58+
three_pl_model = IrtRuby::ThreeParameterModel.new(data)
59+
three_pl_result = three_pl_model.fit
60+
puts three_pl_result[:abilities]
61+
puts three_pl_result[:difficulties]
62+
puts three_pl_result[:discriminations]
63+
puts three_pl_result[:guessings]
64+
```
65+
66+
## Handling Missing Data
67+
Real-world data often has missing responses. Each model (Rasch, 2PL, 3PL) accepts a `missing_strategy: option` to handle nil entries:
68+
69+
- `:ignore` (default): Skip `nil` responses entirely in the log-likelihood and gradient calculations.
70+
- `:treat_as_incorrect`: Interpret `nil` as `0`.
71+
- `:treat_as_correct`: Interpret `nil` as `1`.
72+
73+
For example:
74+
```ruby
75+
data_with_missing = [
76+
[1, nil, 0],
77+
[nil, 1, 0],
78+
[0, 1, 1]
79+
]
80+
81+
model = IrtRuby::RaschModel.new(
82+
data_with_missing,
83+
max_iter: 300,
84+
learning_rate: 0.01,
85+
missing_strategy: :treat_as_incorrect
86+
)
87+
result = model.fit
88+
89+
puts "Abilities: #{result[:abilities]}"
4590
puts "Difficulties: #{result[:difficulties]}"
4691
```
92+
This flexibility helps you handle datasets where missingness might signify a skipped item or an unanswered question.
93+
94+
## Advanced Usage
95+
96+
### Adaptive Learning Rate & Convergence
97+
By default, each model uses a gradient ascent with:
98+
99+
- An adaptive learning rate (if log-likelihood decreases, it reverts the step and reduces the rate).
100+
- Multiple convergence checks (change in log-likelihood and average parameter updates).
101+
102+
You can customize:
103+
104+
- `max_iter`: The maximum number of iterations.
105+
- `tolerance` and `param_tolerance`: Convergence thresholds for log-likelihood change and parameter updates.
106+
- `learning_rate`: Initial learning rate.
107+
- `decay_factor`: Factor by which the learning rate is reduced on a failed step.
108+
109+
Example:
110+
```ruby
111+
IrtRuby::TwoParameterModel.new(
112+
data,
113+
max_iter: 500,
114+
tolerance: 1e-7,
115+
param_tolerance: 1e-7,
116+
learning_rate: 0.05,
117+
decay_factor: 0.5
118+
)
119+
```
120+
### Parameter Clamping
121+
For 2PL and 3PL:
122+
123+
- **Discriminations** (`a`) are clamped between `0.01` and `5.0`.
124+
- **Guessings** (`c`, 3PL only) are clamped to `[0.0, 0.35]`.
125+
126+
This prevents extreme or invalid parameter estimates.
47127

48128
## Development
49129

0 commit comments

Comments
 (0)