0

ウェルチ検定を行うプログラムをJavaで作っていますが、途中からExcelの関数(T.INV.2T関数)が使いたいと思いapache poiをダウンロードしてみました。
そこで、cellに計算式を入れようとしたのですが、以下のようにエラーが返ってきてしまいます。

初心者ということもあり、汚いコーディングだとは思いますが、許してください。
ぜひ、お願いします。

エラーメッセージ

Exception in thread "main" org.apache.poi.ss.formula.FormulaParseException: Dotted range (full row or column) expression 'T.INV' must have exactly 2 dots.
    at org.apache.poi.ss.formula.FormulaParser.parseRangeable(FormulaParser.java:569)
    at org.apache.poi.ss.formula.FormulaParser.parseRangeExpression(FormulaParser.java:322)
    at org.apache.poi.ss.formula.FormulaParser.parseSimpleFactor(FormulaParser.java:1548)
    at org.apache.poi.ss.formula.FormulaParser.percentFactor(FormulaParser.java:1506)
    at org.apache.poi.ss.formula.FormulaParser.powerFactor(FormulaParser.java:1493)
    at org.apache.poi.ss.formula.FormulaParser.Term(FormulaParser.java:1867)
    at org.apache.poi.ss.formula.FormulaParser.additiveExpression(FormulaParser.java:1994)
    at org.apache.poi.ss.formula.FormulaParser.concatExpression(FormulaParser.java:1978)
    at org.apache.poi.ss.formula.FormulaParser.comparisonExpression(FormulaParser.java:1935)
    at org.apache.poi.ss.formula.FormulaParser.intersectionExpression(FormulaParser.java:1908)
    at org.apache.poi.ss.formula.FormulaParser.unionExpression(FormulaParser.java:1889)
    at org.apache.poi.ss.formula.FormulaParser.parseSimpleFactor(FormulaParser.java:1534)
    at org.apache.poi.ss.formula.FormulaParser.percentFactor(FormulaParser.java:1506)
    at org.apache.poi.ss.formula.FormulaParser.powerFactor(FormulaParser.java:1493)
    at org.apache.poi.ss.formula.FormulaParser.Term(FormulaParser.java:1867)
    at org.apache.poi.ss.formula.FormulaParser.additiveExpression(FormulaParser.java:1994)
    at org.apache.poi.ss.formula.FormulaParser.concatExpression(FormulaParser.java:1978)
    at org.apache.poi.ss.formula.FormulaParser.comparisonExpression(FormulaParser.java:1935)
    at org.apache.poi.ss.formula.FormulaParser.intersectionExpression(FormulaParser.java:1908)
    at org.apache.poi.ss.formula.FormulaParser.unionExpression(FormulaParser.java:1889)
    at org.apache.poi.ss.formula.FormulaParser.parse(FormulaParser.java:2036)
    at org.apache.poi.ss.formula.FormulaParser.parse(FormulaParser.java:170)
    at org.apache.poi.xssf.usermodel.XSSFCell.setFormula(XSSFCell.java:550)
    at org.apache.poi.xssf.usermodel.XSSFCell.setCellFormulaImpl(XSSFCell.java:526)
    at org.apache.poi.ss.usermodel.CellBase.setCellFormula(CellBase.java:132)
    at Main.Welch_test(Main.java:66)
    at Main.main(Main.java:130)

コード

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.*;
import java.util.Scanner;

public class Main
{
    public static void Welch_test ()
    {
        System.out.println ("Test population mean in two different populations are same or not");
        System.out.println ("This program works only for two-tailed ");

        Scanner in = new Scanner(System.in);
        System.out.println ("Do you want population with same variation(Type 1) or different variation(Type 2):");
        int type = in.nextInt();

        System.out.println ("How many samples did you take from first population:");
        double na = in.nextDouble();

        System.out.println ("How many samples did you take from second population:");
        double nb = in.nextDouble();

        System.out.println ("What is your Sample mean for first population:");
        double Xa = in.nextDouble();

        System.out.println ("What is your Sample mean for second population:");
        double Xb = in.nextDouble();

        System.out.println ("What is unbiased variance for first population:");
        double Ua = in.nextDouble();

        System.out.println ("What is unbiased variance for second population:");
        double Ub = in.nextDouble();

        System.out.println ("What is Level of Significance (Type with %-value)");
        double L = in.nextDouble();
        double l = L/100;

        if (type == 1)
        {
            double k = na+nb-2;
            double p = Math.abs(Xa-Xb);
            double one = ((1/na)+(1/nb));
            double two = ((((Ua)*(na-1))+((Ub)*(nb-1)))/(na+nb-2));
            double three = one*two;
            double q = Math.sqrt(three);
            double T = p/q;
            System.out.println ("Degree of freedom is " + k);
            System.out.println ("Test statistic is " + T);

            Workbook wb = new XSSFWorkbook();
            Sheet sheet = wb.createSheet();
            Row row1 = sheet.createRow(1);
            Row row2 = sheet.createRow(2);

            Cell cell1_1 = row1.createCell(1);
            Cell cell1_2 = row1.createCell(2);
            Cell cell1_3 = row1.createCell(3);
            Cell cell2_3 = row2.createCell(3);

            cell1_1.setCellValue(l);
            cell1_2.setCellValue(k);
            cell2_3.setCellFormula("T.INV.2T(B2,C2)");

            FileOutputStream out = null;
            try {
                out = new FileOutputStream("T-inverse.xlsx");
                wb.write(out);
            } catch(IOException e) {
                System.out.println(e.toString());
            } finally {
                try {
                    out.close();
                } catch(IOException e) {
                    System.out.println(e.toString());
                }
            }
        }
        else
        {
            double x = Math.abs(Xa-Xb);
            double y = Math.sqrt(((Ua)/na)+((Ub)/nb));
            double z = x/y;
            System.out.println (z);
            double parta = ((((Ua)/na)+((Ub)/nb))*(((Ua)/na)+((Ub)/nb)));
            double partb = ((((Ub)/nb)*((Ub)/nb))/(nb-1));
            double partc = ((((Ua)/na)*((Ua)/na))/(na-1));
            double partd = partc+partb;
            double j = parta/partd;
            j = Math.round(j);
            System.out.println ("Degree of Freedom is " + j);
            System.out.println ("Test statistic is " + z);

            Workbook wb = new XSSFWorkbook();
            Sheet sheet = wb.createSheet();
            Row row1 = sheet.createRow(1);
            Row row2 = sheet.createRow(2);

            Cell cell1_1 = row1.createCell(1);
            Cell cell1_2 = row1.createCell(2);
            Cell cell1_3 = row1.createCell(3);
            Cell cell2_3 = row2.createCell(3);

            cell1_1.setCellValue(l);
            cell1_2.setCellValue(j);
            cell2_3.setCellFormula("T.INV.2T(B2,C2)");

            FileOutputStream out = null;
            try {
                out = new FileOutputStream("T-inverse.xlsx");
                wb.write(out);
            } catch(IOException e) {
                System.out.println(e.toString());
            } finally {
                try {
                    out.close();
                } catch(IOException e) {
                    System.out.println(e.toString());
                }
            }
        }
    }
    public static void main(String[] args)
    {
        Welch_test();
    }
}

1 件の回答 1

1

T.INV.2T(B2,C2) の代わりに _xlfn.T.INV.2T(B2,C2) とするとエラー無く終了するようですが、所望の結果が得られているでしょうか。
(ウェルチ検定について知らないので正しい結果になっているのかはわかりませんでした)

参考:

コード差分

0

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。