Home Libraries People FAQ More

##### 為了在單樣本的學生t測試中獲取有效的結果，估算標準大小應變為多大(Estimating how large a sample size would have to become in order to give a significant Students-t test result with a single sample test)

```// 需要的頭文件:
#include <boost/math/distributions/students_t.hpp>
#include <iostream>
#include <iomanip>
// 為了簡化使用，將所有的名字都引入全局名字空間:
using namespace boost::math;
using namespace std;

void single_sample_find_df(
double M,          // M = true mean.
double Sm,         // Sm = Sample Mean.
double Sd)         // Sd = Sample Standard Deviation.
{
```

```double alpha[] = { 0.5, 0.25, 0.1, 0.05, 0.01, 0.001, 0.0001, 0.00001 };
```

```cout << "\n\n"
"_______________________________________________________________\n"
"Confidence       Estimated          Estimated\n"
" Value (%)      Sample Size        Sample Size\n"
"              (one sided test)    (two sided test)\n"
"_______________________________________________________________\n";
```

```   for(unsigned i = 0; i < sizeof(alpha)/sizeof(alpha[0]); ++i)
{
// 置信水平:
cout << fixed << setprecision(3) << setw(10) << right << 100 * (1-alpha[i]);
// calculate df for single sided test:
double df = students_t::find_degrees_of_freedom(
fabs(M - Sm), alpha[i], alpha[i], Sd);
// 轉換為樣本大小:
double size = ceil(df) + 1;
// 打印大小:
cout << fixed << setprecision(0) << setw(16) << right << size;
// calculate df for two sided test:
df = students_t::find_degrees_of_freedom(
fabs(M - Sm), alpha[i]/2, alpha[i], Sd);
// 轉換為樣本大小:
size = ceil(df) + 1;
// 打印大小:
cout << fixed << setprecision(0) << setw(16) << right << size << endl;
}
cout << endl;
}
```

```_____________________________________________________________
Estimated sample sizes required for various confidence levels
_____________________________________________________________

True Mean                               =  38.90000
Sample Mean                             =  37.80000
Sample Standard Deviation               =  0.96437

_______________________________________________________________
Confidence       Estimated          Estimated
Value (%)      Sample Size        Sample Size
(one sided test)    (two sided test)
_______________________________________________________________
50.000               2               3
75.000               4               5
90.000               8              10
95.000              12              14
99.000              21              23
99.900              36              38
99.990              51              54
99.999              67              69
```