main.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. part1, err := NewPart1([]int{
  7. 5, 6, 6, 6, 6,
  8. 3, 5, 6, 5, 6,
  9. 1, 4, 6, -1, 6,
  10. 3, 4, 2, 6, 5,
  11. })
  12. if err != nil {
  13. panic(err)
  14. }
  15. part2, err := NewPart2([]int{
  16. 0, 6, 5, 1, 6,
  17. 6, 1, 6, 5, 0,
  18. 6, 4, 6, 3,
  19. })
  20. if err != nil {
  21. panic(err)
  22. }
  23. part3, err := NewPart3([]int{
  24. 5, 0, 2, 1, 3,
  25. 0, 1, 2, 5, 0,
  26. 4, 2, 5, 0, 3,
  27. 3, 5, 1,
  28. })
  29. if err != nil {
  30. panic(err)
  31. }
  32. for i, p := range []Part{part1, part2, part3} {
  33. p.Calculate()
  34. fmt.Printf("# Part %d\n\n", i+1)
  35. for k, v := range p.Scores() {
  36. fmt.Printf("%s: %f\n", k, v)
  37. }
  38. fmt.Printf("\n\n")
  39. }
  40. }
  41. type Spec map[string][]int
  42. var Part1Spec = Spec{
  43. "Interference": {2, 3, 4, 8, 9, 13, 14, 17, 19},
  44. "Support": {5, 10, 15},
  45. "PainSeverity": {1, 7, 12},
  46. "LifeControl": {11, 16},
  47. "AffectiveDistress": {6, 18, 20},
  48. }
  49. var Part2Spec = Spec{
  50. "Negative": {1, 4, 7, 10},
  51. "Solicitous": {2, 5, 8, 11, 13, 14},
  52. "Distracting": {3, 6, 9, 12},
  53. }
  54. var Part3Spec = Spec{
  55. "HouseholdChores": {1, 5, 9, 13, 17},
  56. "OutdoorWork": {2, 6, 10, 14, 18},
  57. "ActivitiesAwayFromHome": {3, 7, 11, 15},
  58. "SocialActivities": {4, 8, 12, 16},
  59. "GeneralActivity": {
  60. 1, 2, 3, 4, 5,
  61. 6, 7, 8, 9, 10,
  62. 11, 12, 13, 14, 15,
  63. 16, 17, 18,
  64. },
  65. }
  66. type Responses = []int
  67. type Scores = map[string]float32
  68. type Part interface {
  69. Responses() Responses
  70. Scores() Scores
  71. Calculate()
  72. }
  73. func NewPart1(responses []int) (Part, error) {
  74. if len(responses) != 20 {
  75. return nil, fmt.Errorf("Requires 20 responses")
  76. }
  77. p := &Part1{}
  78. p.PartBase.Responses = responses
  79. p.PartBase.Scores = make(Scores)
  80. return p, nil
  81. }
  82. type Part1 struct {
  83. PartBase
  84. }
  85. func (p *Part1) Responses() Responses {
  86. return p.PartBase.Responses
  87. }
  88. func (p *Part1) Scores() Scores {
  89. return p.PartBase.Scores
  90. }
  91. func (p *Part1) Calculate() {
  92. for k, v := range Part1Spec {
  93. if k == "AffectiveDistress" {
  94. continue
  95. }
  96. p.PartBase.Scores[k] = subMean(p.PartBase.Responses, v)
  97. }
  98. k := "AffectiveDistress"
  99. for _, q := range Part1Spec[k] {
  100. if q < 1 {
  101. panic(fmt.Sprintf("Cannot compute mean for nonpositive question %d", q))
  102. }
  103. if q == 6 {
  104. // Question 6 score is the complement of the response
  105. p.PartBase.Scores[k] +=
  106. 6.0 - float32(p.Responses()[q-1])
  107. continue
  108. }
  109. p.PartBase.Scores[k] += float32(p.Responses()[q-1])
  110. }
  111. p.PartBase.Scores[k] /= float32(len(Part1Spec[k]))
  112. }
  113. func NewPart2(responses []int) (Part, error) {
  114. if len(responses) != 14 {
  115. return nil, fmt.Errorf("Requires 14 responses")
  116. }
  117. p := &Part2{}
  118. p.PartBase.Responses = responses
  119. p.PartBase.Scores = make(Scores)
  120. return p, nil
  121. }
  122. type Part2 struct {
  123. PartBase
  124. }
  125. func (p *Part2) Responses() Responses {
  126. return p.PartBase.Responses
  127. }
  128. func (p *Part2) Scores() Scores {
  129. return p.PartBase.Scores
  130. }
  131. func (p *Part2) Calculate() {
  132. p.PartBase.Calculate(Part2Spec)
  133. }
  134. func NewPart3(responses []int) (Part, error) {
  135. if len(responses) != 18 {
  136. return nil, fmt.Errorf("Requires 18 responses")
  137. }
  138. p := &Part3{}
  139. p.PartBase.Responses = responses
  140. p.PartBase.Scores = make(Scores)
  141. return p, nil
  142. }
  143. type Part3 struct {
  144. PartBase
  145. }
  146. func (p *Part3) Responses() Responses {
  147. return p.PartBase.Responses
  148. }
  149. func (p *Part3) Scores() Scores {
  150. return p.PartBase.Scores
  151. }
  152. func (p *Part3) Calculate() {
  153. p.PartBase.Calculate(Part3Spec)
  154. }
  155. type PartBase struct {
  156. Responses []int
  157. Scores map[string]float32
  158. }
  159. func (p *PartBase) Calculate(spec Spec) {
  160. for k, v := range spec {
  161. p.Scores[k] = subMean(p.Responses, v)
  162. }
  163. }
  164. func subMean(s []int, qs []int) (mean float32) {
  165. for _, q := range qs {
  166. if q < 1 {
  167. panic(fmt.Sprintf("Cannot compute mean for nonpositive question %d", q))
  168. }
  169. mean += float32(s[q-1])
  170. }
  171. mean /= float32(len(qs))
  172. return
  173. }