Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / sc / source / ui / StatisticsDialogs / TTestDialog.cxx
blob228063d6a07be3c1fc25c5cffbcc8c4998cba683
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 */
11 #include <sfx2/dispatch.hxx>
12 #include <svl/zforlist.hxx>
13 #include <svl/undo.hxx>
14 #include <boost/random.hpp>
15 #include <boost/scoped_ptr.hpp>
17 #include "formulacell.hxx"
18 #include "rangelst.hxx"
19 #include "scitems.hxx"
20 #include "docsh.hxx"
21 #include "document.hxx"
22 #include "uiitems.hxx"
23 #include "reffact.hxx"
24 #include "strload.hxx"
25 #include "docfunc.hxx"
26 #include "StatisticsDialogs.hrc"
27 #include "TableFillingAndNavigationTools.hxx"
29 #include "TTestDialog.hxx"
31 namespace
34 static const OUString strWildcardVariable1Range("%VAR1_RANGE%");
35 static const OUString strWildcardVariable2Range("%VAR2_RANGE%");
39 ScTTestDialog::ScTTestDialog(
40 SfxBindings* pSfxBindings, SfxChildWindow* pChildWindow,
41 Window* pParent, ScViewData* pViewData ) :
42 ScStatisticsTwoVariableDialog(
43 pSfxBindings, pChildWindow, pParent, pViewData,
44 "TTestDialog", "modules/scalc/ui/ttestdialog.ui" )
47 ScTTestDialog::~ScTTestDialog()
50 bool ScTTestDialog::Close()
52 return DoClose( ScTTestDialogWrapper::GetChildWindowId() );
55 sal_Int16 ScTTestDialog::GetUndoNameId()
57 return STR_TTEST_UNDO_NAME;
60 ScRange ScTTestDialog::ApplyOutput(ScDocShell* pDocShell)
62 AddressWalkerWriter aOutput(mOutputAddress, pDocShell, mDocument,
63 formula::FormulaGrammar::mergeToGrammar( formula::FormulaGrammar::GRAM_ENGLISH, mAddressDetails.eConv));
64 FormulaTemplate aTemplate(mDocument, mAddressDetails);
66 boost::scoped_ptr<DataRangeIterator> pVariable1Iterator;
67 if (mGroupedBy == BY_COLUMN)
68 pVariable1Iterator.reset(new DataRangeByColumnIterator(mVariable1Range));
69 else
70 pVariable1Iterator.reset(new DataRangeByRowIterator(mVariable1Range));
72 boost::scoped_ptr<DataRangeIterator> pVariable2Iterator;
73 if (mGroupedBy == BY_COLUMN)
74 pVariable2Iterator.reset(new DataRangeByColumnIterator(mVariable2Range));
75 else
76 pVariable2Iterator.reset(new DataRangeByRowIterator(mVariable2Range));
78 aTemplate.autoReplaceRange(strWildcardVariable1Range, pVariable1Iterator->get());
79 aTemplate.autoReplaceRange(strWildcardVariable2Range, pVariable2Iterator->get());
82 aOutput.writeBoldString(SC_STRLOAD(RID_STATISTICS_DLGS, STR_TTEST_UNDO_NAME));
83 aOutput.newLine();
85 aOutput.writeString("Alpha");
86 aOutput.nextColumn();
87 aOutput.writeValue(0.05);
88 aTemplate.autoReplaceAddress("%ALPHA%", aOutput.current());
89 aOutput.newLine();
91 aOutput.nextColumn();
92 aOutput.writeBoldString(SC_STRLOAD(RID_STATISTICS_DLGS, STR_VARIABLE_1_LABEL));
93 aOutput.nextColumn();
94 aOutput.writeBoldString(SC_STRLOAD(RID_STATISTICS_DLGS, STR_VARIABLE_2_LABEL));
95 aOutput.newLine();
97 aOutput.writeString(SC_STRLOAD(RID_STATISTICS_DLGS, STRID_CALC_MEAN));
98 aOutput.nextColumn();
99 aTemplate.setTemplate("=AVERAGE(%VAR1_RANGE%)");
100 aOutput.writeFormula(aTemplate.getTemplate());
101 aOutput.nextColumn();
102 aTemplate.setTemplate("=AVERAGE(%VAR2_RANGE%)");
103 aOutput.writeFormula(aTemplate.getTemplate());
104 aOutput.newLine();
106 aOutput.writeString(SC_STRLOAD(RID_STATISTICS_DLGS, STRID_CALC_VARIANCE));
107 aOutput.nextColumn();
108 aTemplate.setTemplate("=VAR(%VAR1_RANGE%)");
109 aOutput.writeFormula(aTemplate.getTemplate());
110 aOutput.nextColumn();
111 aTemplate.setTemplate("=VAR(%VAR2_RANGE%)");
112 aOutput.writeFormula(aTemplate.getTemplate());
113 aOutput.newLine();
115 // Observations
116 aOutput.writeString(SC_STRLOAD(RID_STATISTICS_DLGS, STR_FTEST_OBSERVATIONS_LABEL));
117 aOutput.nextColumn();
118 aTemplate.setTemplate("=COUNT(%VAR1_RANGE%)");
119 aOutput.writeFormula(aTemplate.getTemplate());
120 aOutput.nextColumn();
121 aTemplate.setTemplate("=COUNT(%VAR2_RANGE%)");
122 aOutput.writeFormula(aTemplate.getTemplate());
123 aOutput.newLine();
125 // Pearson Correlation
126 aOutput.writeString(SC_STRLOAD(RID_STATISTICS_DLGS, STR_TTEST_PEARSON_CORRELATION));
127 aOutput.nextColumn();
128 aTemplate.setTemplate("=CORREL(%VAR1_RANGE%;%VAR2_RANGE%)");
129 aOutput.writeFormula(aTemplate.getTemplate());
130 aOutput.newLine();
132 // Hypothesized mean difference
133 aOutput.writeString(SC_STRLOAD(RID_STATISTICS_DLGS, STR_TTEST_HYPOTHESIZED_MEAN_DIFFERENCE));
134 aOutput.nextColumn();
135 aOutput.writeValue(2);
136 aTemplate.autoReplaceAddress("%HYPOTHESIZED_MEAN_DIFFERENCE%", aOutput.current());
137 aOutput.newLine();
139 // Observed mean difference
140 aOutput.writeString(SC_STRLOAD(RID_STATISTICS_DLGS, STR_TTEST_OBSERVED_MEAN_DIFFERENCE));
141 aOutput.nextColumn();
142 aTemplate.setTemplate("=AVERAGE(IF(ISODD(IF(ISNUMBER(%VAR1_RANGE%); 1; 0) * IF(ISNUMBER(%VAR2_RANGE%); 1; 0)); %VAR1_RANGE% - %VAR2_RANGE%; \"NA\"))");
143 aOutput.writeMatrixFormula(aTemplate.getTemplate());
144 aTemplate.autoReplaceAddress("%OBSERVED_MEAN_DIFFERENCE%", aOutput.current());
145 aOutput.newLine();
147 // Variance of the Differences
148 aOutput.writeString(SC_STRLOAD(RID_STATISTICS_DLGS, STR_TTEST_VARIANCE_OF_THE_DIFFERENCES));
149 aOutput.nextColumn();
150 aTemplate.setTemplate("=VAR(IF(ISODD(IF(ISNUMBER(%VAR1_RANGE%); 1; 0) * IF(ISNUMBER(%VAR2_RANGE%); 1; 0)); %VAR1_RANGE% - %VAR2_RANGE%; \"NA\"))");
151 aOutput.writeMatrixFormula(aTemplate.getTemplate());
152 aTemplate.autoReplaceAddress("%VARIANCE_OF_DIFFERENCES%", aOutput.current());
153 aOutput.newLine();
155 // df
156 aOutput.writeString(SC_STRLOAD(RID_STATISTICS_DLGS, STR_ANOVA_LABEL_DF));
157 aOutput.nextColumn();
158 aTemplate.setTemplate("=SUM(IF(ISNUMBER(%VAR1_RANGE%); 1; 0) * IF(ISNUMBER(%VAR2_RANGE%); 1; 0)) - 1");
159 aOutput.writeMatrixFormula(aTemplate.getTemplate());
160 aTemplate.autoReplaceAddress("%DEGREE_OF_FREEDOM%", aOutput.current());
161 aOutput.newLine();
163 // t stat
164 aOutput.writeString(SC_STRLOAD(RID_STATISTICS_DLGS, STR_TTEST_T_STAT));
165 aOutput.nextColumn();
166 aTemplate.setTemplate("=(%OBSERVED_MEAN_DIFFERENCE% - %HYPOTHESIZED_MEAN_DIFFERENCE%) / (%VARIANCE_OF_DIFFERENCES% / ( %DEGREE_OF_FREEDOM% + 1)) ^ 0.5");
167 aOutput.writeFormula(aTemplate.getTemplate());
168 aTemplate.autoReplaceAddress("%T_STAT%", aOutput.current());
169 aOutput.newLine();
171 // P one-tail
172 aOutput.writeString(SC_STRLOAD(RID_STATISTICS_DLGS, STR_TTEST_P_ONE_TAIL));
173 aOutput.nextColumn();
174 aTemplate.setTemplate("=TDIST(ABS(%T_STAT%); %DEGREE_OF_FREEDOM%; 1)");
175 aOutput.writeFormula(aTemplate.getTemplate());
176 aOutput.newLine();
178 // T critical one-tail
179 aOutput.writeString(SC_STRLOAD(RID_STATISTICS_DLGS, STR_TTEST_T_CRITICAL_ONE_TAIL));
180 aOutput.nextColumn();
181 aTemplate.setTemplate("=TINV(2*%ALPHA%; %DEGREE_OF_FREEDOM%)");
182 aOutput.writeFormula(aTemplate.getTemplate());
183 aOutput.newLine();
185 // P two-tail
186 aOutput.writeString(SC_STRLOAD(RID_STATISTICS_DLGS, STR_TTEST_P_TWO_TAIL));
187 aOutput.nextColumn();
188 aTemplate.setTemplate("=TDIST(ABS(%T_STAT%); %DEGREE_OF_FREEDOM%; 2)");
189 aOutput.writeFormula(aTemplate.getTemplate());
190 aOutput.newLine();
192 // T critical two-tail
193 aOutput.writeString(SC_STRLOAD(RID_STATISTICS_DLGS, STR_TTEST_T_CRITICAL_TWO_TAIL));
194 aOutput.nextColumn();
195 aTemplate.setTemplate("=TINV(%ALPHA%; %DEGREE_OF_FREEDOM%)");
196 aOutput.writeFormula(aTemplate.getTemplate());
198 return ScRange(aOutput.mMinimumAddress, aOutput.mMaximumAddress);
201 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */