1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
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"
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"
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" )
46 SetText(SC_STRLOAD(RID_STATISTICS_DLGS
, STR_TTEST
));
49 ScTTestDialog::~ScTTestDialog()
52 bool ScTTestDialog::Close()
54 return DoClose( ScTTestDialogWrapper::GetChildWindowId() );
57 sal_Int16
ScTTestDialog::GetUndoNameId()
59 return STR_TTEST_UNDO_NAME
;
62 ScRange
ScTTestDialog::ApplyOutput(ScDocShell
* pDocShell
)
64 AddressWalkerWriter
aOutput(mOutputAddress
, pDocShell
, mDocument
,
65 formula::FormulaGrammar::mergeToGrammar( formula::FormulaGrammar::GRAM_ENGLISH
, mAddressDetails
.eConv
));
66 FormulaTemplate
aTemplate(mDocument
);
68 boost::scoped_ptr
<DataRangeIterator
> pVariable1Iterator
;
69 if (mGroupedBy
== BY_COLUMN
)
70 pVariable1Iterator
.reset(new DataRangeByColumnIterator(mVariable1Range
));
72 pVariable1Iterator
.reset(new DataRangeByRowIterator(mVariable1Range
));
74 boost::scoped_ptr
<DataRangeIterator
> pVariable2Iterator
;
75 if (mGroupedBy
== BY_COLUMN
)
76 pVariable2Iterator
.reset(new DataRangeByColumnIterator(mVariable2Range
));
78 pVariable2Iterator
.reset(new DataRangeByRowIterator(mVariable2Range
));
80 aTemplate
.autoReplaceRange(strWildcardVariable1Range
, pVariable1Iterator
->get());
81 aTemplate
.autoReplaceRange(strWildcardVariable2Range
, pVariable2Iterator
->get());
84 aOutput
.writeBoldString(SC_STRLOAD(RID_STATISTICS_DLGS
, STR_TTEST_UNDO_NAME
));
88 aOutput
.writeString("Alpha");
90 aOutput
.writeValue(0.05);
91 aTemplate
.autoReplaceAddress("%ALPHA%", aOutput
.current());
94 // Hypothesized mean difference
95 aOutput
.writeString(SC_STRLOAD(RID_STATISTICS_DLGS
, STR_TTEST_HYPOTHESIZED_MEAN_DIFFERENCE
));
97 aOutput
.writeValue(0);
98 aTemplate
.autoReplaceAddress("%HYPOTHESIZED_MEAN_DIFFERENCE%", aOutput
.current());
101 aOutput
.nextColumn();
102 aOutput
.writeBoldString(SC_STRLOAD(RID_STATISTICS_DLGS
, STR_VARIABLE_1_LABEL
));
103 aOutput
.nextColumn();
104 aOutput
.writeBoldString(SC_STRLOAD(RID_STATISTICS_DLGS
, STR_VARIABLE_2_LABEL
));
107 aOutput
.writeString(SC_STRLOAD(RID_STATISTICS_DLGS
, STRID_CALC_MEAN
));
108 aOutput
.nextColumn();
109 aTemplate
.setTemplate("=AVERAGE(%VAR1_RANGE%)");
110 aOutput
.writeFormula(aTemplate
.getTemplate());
111 aOutput
.nextColumn();
112 aTemplate
.setTemplate("=AVERAGE(%VAR2_RANGE%)");
113 aOutput
.writeFormula(aTemplate
.getTemplate());
116 aOutput
.writeString(SC_STRLOAD(RID_STATISTICS_DLGS
, STRID_CALC_VARIANCE
));
117 aOutput
.nextColumn();
118 aTemplate
.setTemplate("=VAR(%VAR1_RANGE%)");
119 aOutput
.writeFormula(aTemplate
.getTemplate());
120 aOutput
.nextColumn();
121 aTemplate
.setTemplate("=VAR(%VAR2_RANGE%)");
122 aOutput
.writeFormula(aTemplate
.getTemplate());
126 aOutput
.writeString(SC_STRLOAD(RID_STATISTICS_DLGS
, STR_FTEST_OBSERVATIONS_LABEL
));
127 aOutput
.nextColumn();
128 aTemplate
.setTemplate("=COUNT(%VAR1_RANGE%)");
129 aOutput
.writeFormula(aTemplate
.getTemplate());
130 aOutput
.nextColumn();
131 aTemplate
.setTemplate("=COUNT(%VAR2_RANGE%)");
132 aOutput
.writeFormula(aTemplate
.getTemplate());
135 // Pearson Correlation
136 aOutput
.writeString(SC_STRLOAD(RID_STATISTICS_DLGS
, STR_TTEST_PEARSON_CORRELATION
));
137 aOutput
.nextColumn();
138 aTemplate
.setTemplate("=CORREL(%VAR1_RANGE%;%VAR2_RANGE%)");
139 aOutput
.writeFormula(aTemplate
.getTemplate());
142 // Observed mean difference
143 aOutput
.writeString(SC_STRLOAD(RID_STATISTICS_DLGS
, STR_TTEST_OBSERVED_MEAN_DIFFERENCE
));
144 aOutput
.nextColumn();
145 aTemplate
.setTemplate("=AVERAGE(IF(ISODD(IF(ISNUMBER(%VAR1_RANGE%); 1; 0) * IF(ISNUMBER(%VAR2_RANGE%); 1; 0)); %VAR1_RANGE% - %VAR2_RANGE%; \"NA\"))");
146 aOutput
.writeMatrixFormula(aTemplate
.getTemplate());
147 aTemplate
.autoReplaceAddress("%OBSERVED_MEAN_DIFFERENCE%", aOutput
.current());
150 // Variance of the Differences
151 aOutput
.writeString(SC_STRLOAD(RID_STATISTICS_DLGS
, STR_TTEST_VARIANCE_OF_THE_DIFFERENCES
));
152 aOutput
.nextColumn();
153 aTemplate
.setTemplate("=VAR(IF(ISODD(IF(ISNUMBER(%VAR1_RANGE%); 1; 0) * IF(ISNUMBER(%VAR2_RANGE%); 1; 0)); %VAR1_RANGE% - %VAR2_RANGE%; \"NA\"))");
154 aOutput
.writeMatrixFormula(aTemplate
.getTemplate());
155 aTemplate
.autoReplaceAddress("%VARIANCE_OF_DIFFERENCES%", aOutput
.current());
159 aOutput
.writeString(SC_STRLOAD(RID_STATISTICS_DLGS
, STR_ANOVA_LABEL_DF
));
160 aOutput
.nextColumn();
161 aTemplate
.setTemplate("=SUM(IF(ISNUMBER(%VAR1_RANGE%); 1; 0) * IF(ISNUMBER(%VAR2_RANGE%); 1; 0)) - 1");
162 aOutput
.writeMatrixFormula(aTemplate
.getTemplate());
163 aTemplate
.autoReplaceAddress("%DEGREE_OF_FREEDOM%", aOutput
.current());
167 aOutput
.writeString(SC_STRLOAD(RID_STATISTICS_DLGS
, STR_TTEST_T_STAT
));
168 aOutput
.nextColumn();
169 aTemplate
.setTemplate("=(%OBSERVED_MEAN_DIFFERENCE% - %HYPOTHESIZED_MEAN_DIFFERENCE%) / (%VARIANCE_OF_DIFFERENCES% / ( %DEGREE_OF_FREEDOM% + 1)) ^ 0.5");
170 aOutput
.writeFormula(aTemplate
.getTemplate());
171 aTemplate
.autoReplaceAddress("%T_STAT%", aOutput
.current());
175 aOutput
.writeString(SC_STRLOAD(RID_STATISTICS_DLGS
, STR_TTEST_P_ONE_TAIL
));
176 aOutput
.nextColumn();
177 aTemplate
.setTemplate("=TDIST(ABS(%T_STAT%); %DEGREE_OF_FREEDOM%; 1)");
178 aOutput
.writeFormula(aTemplate
.getTemplate());
181 // T critical one-tail
182 aOutput
.writeString(SC_STRLOAD(RID_STATISTICS_DLGS
, STR_TTEST_T_CRITICAL_ONE_TAIL
));
183 aOutput
.nextColumn();
184 aTemplate
.setTemplate("=TINV(2*%ALPHA%; %DEGREE_OF_FREEDOM%)");
185 aOutput
.writeFormula(aTemplate
.getTemplate());
189 aOutput
.writeString(SC_STRLOAD(RID_STATISTICS_DLGS
, STR_TTEST_P_TWO_TAIL
));
190 aOutput
.nextColumn();
191 aTemplate
.setTemplate("=TDIST(ABS(%T_STAT%); %DEGREE_OF_FREEDOM%; 2)");
192 aOutput
.writeFormula(aTemplate
.getTemplate());
195 // T critical two-tail
196 aOutput
.writeString(SC_STRLOAD(RID_STATISTICS_DLGS
, STR_TTEST_T_CRITICAL_TWO_TAIL
));
197 aOutput
.nextColumn();
198 aTemplate
.setTemplate("=TINV(%ALPHA%; %DEGREE_OF_FREEDOM%)");
199 aOutput
.writeFormula(aTemplate
.getTemplate());
201 return ScRange(aOutput
.mMinimumAddress
, aOutput
.mMaximumAddress
);
204 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */