-
Notifications
You must be signed in to change notification settings - Fork 1
STD added to numericQuestionAnalysis #314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,12 +16,15 @@ export default function NumericQuestionAnalytics({ | |
| question, | ||
| responses, | ||
| }: Props) { | ||
| const { mean, median, mode } = useMemo(() => { | ||
| const { mean, median, mode, standardDeviation } = useMemo(() => { | ||
| let mean: number; | ||
| let median: number; | ||
| let standardDeviation: number; | ||
| let mode: number[] = []; | ||
| const numbers: number[] = []; | ||
| let total = 0; | ||
| let sdTotal = 0; | ||
| let stdMean: number; | ||
| const occurrences: number[] = []; | ||
| let maxOccur = 2; | ||
|
|
||
|
|
@@ -35,6 +38,11 @@ export default function NumericQuestionAnalytics({ | |
| const number = questionResponse.answer; | ||
| total += number; | ||
| numbers.push(number); | ||
|
|
||
| // calculate mean for std | ||
| stdMean = ((total / numbers.length) * 100) / 100; | ||
| sdTotal += Math.pow(number - stdMean, 2); | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove line 43, and move line 44 into a separate loop |
||
| if (!occurrences[number]) occurrences[number] = 0; | ||
| occurrences[number]++; | ||
| } | ||
|
|
@@ -68,10 +76,17 @@ export default function NumericQuestionAnalytics({ | |
| } | ||
| }); | ||
|
|
||
| // calculate standard deviation | ||
| standardDeviation = Math.sqrt(sdTotal / numbers.length); | ||
|
|
||
| // Rounds standard deviation to hundredths place | ||
| standardDeviation = Math.round(standardDeviation * 100) / 100; | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You'll want to calculate the mean here, your line 43 should work I believe -> |
||
| return { | ||
| mean, | ||
| mode, | ||
| median, | ||
| standardDeviation, | ||
| }; | ||
| }, [responses]); | ||
|
|
||
|
|
@@ -88,6 +103,9 @@ export default function NumericQuestionAnalytics({ | |
| Mode: {mode.length ? mode.join(', ') : 'None'} | ||
| </ListItemText> | ||
| </ListItem> | ||
| <ListItem> | ||
| <ListItemText>Standard Deviation: {standardDeviation}</ListItemText> | ||
| </ListItem> | ||
| </List> | ||
| ); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
keep this :)