トップ 言語 Java アルゴリズム 計算プログラム入門 重回帰式

重回帰式を求めるJavaプログラム

重回帰式を求めるJavaのサンプルプログラムをまとめています。

▲記事トップへ

目次

このページの目次です。

1. 重回帰式(p=2)
2. 重回帰式(p=n)
3. 寄与率
4. 重回帰式の説明変数自動選択

参考書籍
もっと知識を広げるための参考
更新履歴

1. 重回帰式(p=2)

p=2(説明変数が2つ)の場合の重回帰式は以下の式で表現されます。

p=2(説明変数が2つ)の場合の重回帰式

p=2(説明変数が2つ)の場合の重回帰式を計算するJavaのプログラムのコードです。 統計の知識に関しては、『多変量解析法入門 (ライブラリ新数学大系) 』(永田靖)の第5章 重回帰分析を参考にしています。

計算クラス(Calculator.java)

計算を行うクラスです。

import java.util.ArrayList;
import java.util.List;

/**
 * 計算クラス
 */
public class Calculator {

	/**
	 * 説明変数が2つの場合の切片を計算する
	 * @param itemXi 項目リスト(Xi)
	 * @param itemYi 項目リスト(Yi)
	 * @return 結果
	 */
	public Double sectionForExpVar2(
			final List<Double> itemsXi1, final List<Double> itemsXi2, final List<Double> itemsYi) {
		Double x1bar = average(itemsXi1);
		Double x2bar = average(itemsXi2);
		Double ybar = average(itemsYi);
		List<Double> batas = partialRegressionCoefficientsForExpVar2(itemsXi1, itemsXi2, itemsYi);
		return ybar - (batas.get(0) * x1bar) - (batas.get(1) * x2bar);
	}

	/**
	 * 説明変数が2つの場合の偏回帰係数を計算する
	 * @param itemsXi1 項目リスト(Xi1)
	 * @param itemsXi2 項目リスト(Xi2)
	 * @param itemsYi 項目リスト(Yi)
	 * @return 結果
	 */
	public List<Double> partialRegressionCoefficientsForExpVar2(
			final List<Double> itemsXi1, final List<Double> itemsXi2, final List<Double> itemsYi) {
		Double s11 = sumOfSquares(itemsXi1);
		Double s22 = sumOfSquares(itemsXi2);
		Double s1y = deviationSumOfProduct(itemsXi1, itemsYi);
		Double s2y = deviationSumOfProduct(itemsXi2, itemsYi);
		Double s12 = deviationSumOfProduct(itemsXi1, itemsXi2);
		Double bata1 = (1 / ((s11*s22) - (s12*s12))) * ((s22*s1y) - (s12*s2y));
		Double bata2 = (1 / ((s11*s22) - (s12*s12))) * ((-1*s12*s1y) + (s11*s2y));
		List<Double> result = new ArrayList<>();
		result.add(bata1);
		result.add(bata2);
		return result;
	}

	/**
	 * 偏差積和を計算する
	 * @param itemXi 項目リスト(Xi)
	 * @param itemYi 項目リスト(Yi)
	 * @return 結果
	 */
	public Double deviationSumOfProduct(final List<Double> itemsXi, final List<Double> itemsYi) {
		List<Double> itemsXiYi = new ArrayList<>();
		int n = itemsXi.size();

		for (int i = 0; i < n; i++) {
			itemsXiYi.add(itemsXi.get(i) * itemsYi.get(i));
		}
		Double xiyiSum = sum(itemsXiYi);
		Double xiSum = sum(itemsXi);
		Double yiSum = sum(itemsYi);
		return xiyiSum - ((xiSum * yiSum) / n);
	}

	/**
	 * 平方和を計算する
	 * @param items 項目リスト
	 * @return 結果
	 */
	public Double sumOfSquares(final List<Double> items) {
		Double xbar = average(items);
		List<Double> squares = new ArrayList<>();

		for (Double item : items) {
			Double sqare = (item - xbar) * (item - xbar);
			squares.add(sqare);
		}
		return sum(squares);
	}

	/**
	 * 平均値を計算する
	 * @param items 項目リスト
	 * @return 結果
	 */
	public Double average(final List<Double> items) {
		return sum(items) / items.size();
	}

	/**
	 * 総和を計算する
	 * @param items 項目リスト
	 * @return 結果
	 */
	public Double sum(final List<Double> items) {
		Double result = 0.0;

		for (Double item : items) {
			result += item;
		}
		return result;
	}
}

テスト用クラス

テスト用のクラスです。

import java.util.ArrayList;
import java.util.List;

/**
 * サンプルのテスト用クラス
 */
public class SampleTest {

	private static double[] xi1 = {
		51, 38, 57, 51, 53, 77, 63, 69, 72, 73};

	private static double[] xi2 = {
		16, 4, 16, 11, 4, 22, 5, 5, 2, 1};

	private static double[] yi = {
		3.0, 3.2, 3.3, 3.9, 4.4, 4.5, 4.5, 5.4, 5.4, 6.0};

	public static void main(String[] args) {
		List<Double> itemsXi1 = prepareTestData(xi1);
		List<Double> itemsXi2 = prepareTestData(xi2);
		List<Double> itemsYi = prepareTestData(yi);
		Calculator calc = new Calculator();
		Double beta0 = calc.sectionForExpVar2(itemsXi1, itemsXi2, itemsYi);
		List<Double> batas = calc.partialRegressionCoefficientsForExpVar2(itemsXi1, itemsXi2, itemsYi);
		Double beta1 = batas.get(0);
		Double beta2 = batas.get(1);
		System.out.println("切片(β0)\t\t:" + beta0);
		System.out.println("偏回帰係数(β1)\t:" + beta1);
		System.out.println("偏回帰係数(β2)\t:" + beta2);
		String mark1 = " + ";
		String mark2 = " + ";

		if (beta1 < 0) {
			mark1 = " ";
		}
		if (beta2 < 0) {
			mark2 = " ";
		}
		System.out.println("重回帰式(p=2)\t\t:y = " + beta0 + mark1 + beta1 + "x1" + mark2 + beta2 + "x2");
	}

	private static List<Double> prepareTestData(double[] sample) {
		List<Double> items = new ArrayList<>();

		for (double data : sample) {
			items.add((double) data);
		}
		return items;
	}
}

実行すると以下のように表示されます。

切片(β0)		:1.020129547203318
偏回帰係数(β1)	:0.06680476622865743
偏回帰係数(β2)	:-0.0808299334202589
重回帰式(p=2)		:y = 1.020129547203318 + 0.06680476622865743x1 -0.0808299334202589x2

2. 重回帰式(p=n)

p=n(説明変数がn個)の場合の重回帰式は以下の式で表現されます。

p=n(説明変数がn個)の場合の重回帰式

p=n(説明変数がn個)の場合の重回帰式を計算するJavaのプログラムのコードです。 統計の知識に関しては、『多変量解析法入門 (ライブラリ新数学大系) 』(永田靖)の第5章 重回帰分析を参考にしています。

計算クラス(Calculator.java)

計算を行うクラスです。

import java.util.ArrayList;
import java.util.List;

/**
 * 計算クラス
 */
public class Calculator {

	/**
	 * 説明変数がn個の場合の切片を計算する
	 * @param itemXin 項目リスト(Xip)
	 * @param itemYi 項目リスト(Yi)
	 * @return 結果
	 */
	public Double sectionForExpVarN(
			List<List<Double>> itemsXip, final List<Double> itemsYi) {
		List<Double> xbars = new ArrayList<>();

		for (List<Double> item : itemsXip) {
			xbars.add(average(item));
		}
		Double ybar = average(itemsYi);
		List<Double> batas = partialRegressionCoefficientsForExpVarN(itemsXip, itemsYi);

		double result = ybar;

		for (int i = 0; i < batas.size(); i++) {
			result -= (batas.get(i) * xbars.get(i));
		}
		return result;
	}

	/**
	 * 説明変数がn個の場合の偏回帰係数を計算する
	 * @param itemsXip 項目リスト(Xip)
	 * @param itemsYi 項目リスト(Yi)
	 * @return 結果
	 */
	public List<Double> partialRegressionCoefficientsForExpVarN(
			List<List<Double>> itemsXip, final List<Double> itemsYi) {
		List<List<Double>> coefficientMatrix = new ArrayList<>();
		int n = itemsXip.size();

		for (int i = 0; i < n; i++) {
			List<Double> row = new ArrayList<>();

			for (int j = 0; j < n; j++) {

				if (i == j) {
					row.add(sumOfSquares(itemsXip.get(i)));

				} else {
					row.add(deviationSumOfProduct(itemsXip.get(i), itemsXip.get(j)));
				}
			}
			row.add(deviationSumOfProduct(itemsXip.get(i), itemsYi));
			coefficientMatrix.add(row);
		}
		return systemOfEquations(coefficientMatrix);
	}

	/**
	 * 連立方程式を計算する
	 * @param coefficientMatrix m行n列の係数行列
	 * @return 結果
	 */
	public List<Double> systemOfEquations(List<List<Double>> coefficientMatrix) {
		int n = coefficientMatrix.size();
		int m = coefficientMatrix.get(0).size();
		double[][] matrix = new double[n][];

		for (int i = 0; i < n; i++) {
			double[] row = new double[m];

			for (int j = 0; j < m; j++) {
				row[j] = coefficientMatrix.get(i).get(j);
			}
			matrix[i] = row;
		}
		return systemOfEquations(matrix);
	}

	/**
	 * 連立方程式を計算する
	 * @param coefficientMatrix m行n列の係数行列
	 * @return 結果
	 */
	public List<Double> systemOfEquations(final double[][] coefficientMatrix) {
		List<Double> result = new ArrayList<>();
		double[][] matrix = coefficientMatrix;
		int n = matrix.length;
		int m = matrix[0].length;

		for (int k = 0; k < n; k++) {
			double p = matrix[k][k];

			for (int j = k; j < m; j++) {
				matrix[k][j] = matrix[k][j]/p;
			}

			for (int i = 0; i < n; i++) {

				if (i != k) {
					double d = matrix[i][k];

					for (int j = k; j < m; j++) {
						matrix[i][j] = matrix[i][j] - d * matrix[k][j];
					}
				}
			}
		}

		for (int i = 0; i < n; i++) {
			result.add(matrix[i][n]);
		}

		return result;
	}

	/**
	 * 偏差積和を計算する
	 * @param itemXi 項目リスト(Xi)
	 * @param itemYi 項目リスト(Yi)
	 * @return 結果
	 */
	public Double deviationSumOfProduct(final List<Double> itemsXi, final List<Double> itemsYi) {
		List<Double> itemsXiYi = new ArrayList<>();
		int n = itemsXi.size();

		for (int i = 0; i < n; i++) {
			itemsXiYi.add(itemsXi.get(i) * itemsYi.get(i));
		}
		Double xiyiSum = sum(itemsXiYi);
		Double xiSum = sum(itemsXi);
		Double yiSum = sum(itemsYi);
		return xiyiSum - ((xiSum * yiSum) / n);
	}

	/**
	 * 平方和を計算する
	 * @param items 項目リスト
	 * @return 結果
	 */
	public Double sumOfSquares(final List<Double> items) {
		Double xbar = average(items);
		List<Double> squares = new ArrayList<>();

		for (Double item : items) {
			Double sqare = (item - xbar) * (item - xbar);
			squares.add(sqare);
		}
		return sum(squares);
	}

	/**
	 * 平均値を計算する
	 * @param items 項目リスト
	 * @return 結果
	 */
	public Double average(final List<Double> items) {
		return sum(items) / items.size();
	}

	/**
	 * 総和を計算する
	 * @param items 項目リスト
	 * @return 結果
	 */
	public Double sum(final List<Double> items) {
		Double result = 0.0;

		for (Double item : items) {
			result += item;
		}
		return result;
	}
}

テスト用クラス

テスト用のクラスです。

import java.util.ArrayList;
import java.util.List;

/**
 * サンプルのテスト用クラス
 */
public class SampleTest {

	public static void main(String[] args) {
		testExp2();
		testExp3();
	}

	static void testExp2() {
		double[][] xip = {
			{51, 38, 57, 51, 53, 77, 63, 69, 72, 73},
			{16, 4, 16, 11, 4, 22, 5, 5, 2, 1}
		};

		double[] yi = {
			3.0, 3.2, 3.3, 3.9, 4.4, 4.5, 4.5, 5.4, 5.4, 6.0
		};
		printResult(xip, yi);
	}

	static void testExp3() {
		double[][] xip = {
			{27.22, 29.74, 50.22, 43.00, 37.19, 46.28, 39.50, 51.80, 54.74, 62.61, 53.50},
			{13.0, 14.0, 5.0, 19.0, 6.0, 13.0, 10.0, 2.0, 2.0, 2.0, 8.0},
			{31.0, 30.0, 13.0, 10.0, 9.0, 8.0, 12.0, 15.0, 15.0, 15.0, 8.0}
		};

		double[] yi = {
			5.6, 6.3, 9.0, 9.2, 9.5, 9.8, 10.0, 11.0, 11.3, 12.5, 13.1
		};
		printResult(xip, yi);
	}

	static void printResult(double[][] xip, double[] yi) {

		// データを準備する
		List<List<Double>> itemsXip = new ArrayList<>();

		for (int i = 0; i < xip.length; i++) {
			itemsXip.add(prepareTestData(xip[i]));
		}
		List<Double> itemsYi = prepareTestData(yi);

		// 計算を行う
		Calculator calc = new Calculator();
		Double beta0 = calc.sectionForExpVarN(itemsXip, itemsYi);
		List<Double> batas =
			calc.partialRegressionCoefficientsForExpVarN(itemsXip, itemsYi);

		// 結果を表示する
		int n = batas.size();
		StringBuilder msg1 = new StringBuilder();
		StringBuilder msg2 = new StringBuilder();
		String fmt1 = "切片(β0)\t\t:%f\n";
		String fmt2 = "偏回帰係数(β%d)\t:%f\n";
		String fmt3 = "重回帰式(p=%d)\t\t:y = %s\n";
		msg1.append(String.format(fmt1, beta0));
		msg2.append(beta0);

		for (int i = 0; i < n; i++) {
			msg1.append(String.format(fmt2, i + 1, batas.get(i)));
			msg2.append(getMark(batas.get(i)) + batas.get(i) + "x" + i + 1);
		}
		System.out.println(
			msg1.toString() + String.format(fmt3, n, msg2.toString()));
	}

	static List<Double> prepareTestData(double[] sample) {
		List<Double> items = new ArrayList<>();

		for (double data : sample) {
			items.add((double) data);
		}
		return items;
	}

	static String getMark(Double data) {
		String mark = " + ";
		if (data < 0) {
			mark = " ";
		}
		return mark;
	}
}

実行すると以下のように表示されます。

切片(β0)		:1.020130
偏回帰係数(β1)	:0.066805
偏回帰係数(β2)	:-0.080830
重回帰式(p=2)		:y = 1.020129547203318 + 0.06680476622865743x01 -0.0808299334202589x11

切片(β0)		:5.947551
偏回帰係数(β1)	:0.127470
偏回帰係数(β2)	:-0.046965
偏回帰係数(β3)	:-0.101857
重回帰式(p=3)		:y = 5.947551115892566 + 0.1274704240732291x01 -0.04696468595352925x11 -0.10185673524514144x21

3. 寄与率

寄与率(または決定係数)を求めるJavaのサンプルプログラムをまとめています。

詳細

4. 重回帰式の説明変数自動選択

重回帰式の説明変数自動選択を行うJavaプログラムをまとめています。

詳細

参考書籍

参考にしている書籍の情報をまとめています。

『入門 統計解析法』(永田靖)

『入門 統計解析法』の基本情報です。

  • タイトル:入門 統計解析法
  • 発売日:1992年04月05日
  • 著者/編集:永田靖
  • 出版社:日科技連/li>
  • サイズ:単行本
  • ページ数:276ページ
  • ISBNコード:4-8171-0266-7

『多変量解析法入門』(永田靖)

以下は『多変量解析法入門』の基本情報です。

  • タイトル:多変量解析法入門 (ライブラリ新数学大系)
  • 発売日:2001年04月10日
  • 著者/編集:永田靖 棟近雅彦
  • 出版社:サイエンス社
  • サイズ:単行本
  • ページ数:245ページ
  • ISBNコード:4-7818-0980-9

もっと知識を広げるための参考

もっと知識を広げるための参考です。

Javaで学ぶ計算プログラム入門

Javaで学ぶ計算プログラムの入門コンテンツをテーマに記事まとめています。 簡単な平均値の計算プログラムから知識を補足しつつ系統立てて説明しています。 標準偏差、相関係数、連立方程式の解法プログラム、回帰分析などの統計解析などの計算プログラムを取り扱っています。

詳細

Java言語

Javaとは?から言語の枠を超えるところまで、Java言語についてまとめています。

詳細

更新履歴

戻る

スポンサーリンク

サイト内のページ

言語
C・C++ /HTML /Java /JavaScript /PHP /シェルスクリプト

開発環境
Ant /Burp /Eclipse /Fiddler /gcc /gdb /Git /g++ /JDK /JMeter /JUnit /Teraterm /ZAP

技術・仕様
Ajax /CORBA /Jakarta EE(旧称J2EE、Java EE) /JNI

ライブラリ/Framework/CMS
bootstrap /jQuery /FuelPHP /Lucene /MyBatis /Seasar2 /Spring /Struts /WordPress

Web API
Google Maps

ITインフラOSとミドルウェア
Linux /Windows /シェル
ActiveMQ /Tomcat /MariaDB /MySQL /Nagios /Redis /Solr

ITインフラサーバー
公開Webサーバー

ITインフラネットワーク
プログラミング /構築

ITインフラセキュリティ
公開サーバーのセキュリティ

PC製品
ZOTAC

SI
ホームページの作り方

その他
IT用語 /ITスキル体系

スポンサーリンク

関連サイト内検索ツール

zealseedsおよび関連サイト内のページが検索できます。

IPアドレス確認ツール

あなたのグローバルIPアドレスは以下です。

18.207.161.212

HTMLの表示色確認ツール

パスワード生成ツール

文字数のプルダウンを選択して、取得ボタンを押すと「a~z、A~Z、0~9」の文字を ランダムに組み合わせた文字列が表示されます。

ここに生成されます。

スポンサーリンク