-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregressionAnalysis.java
More file actions
89 lines (79 loc) · 2.5 KB
/
regressionAnalysis.java
File metadata and controls
89 lines (79 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
public class regressionAnalysis {
static ArrayList<ArrayList<String>> eaten_data = new ArrayList<>();
public regressionAnalysis() {
// 저장된 파일 이차원어레이리스트에 저장
try {
// 파일 객체 생성
File file = new File("Eaten_Food.txt");
// 입력 스트림 생성
FileReader filereader = new FileReader(file);
// 입력 버퍼 생성
BufferedReader bufReader = new BufferedReader(filereader);
String line = "";
while ((line = bufReader.readLine()) != null) {
String[] tmp = line.split(" ");
ArrayList<String> tmpArrayList = new ArrayList<>(Arrays.asList(tmp));
eaten_data.add(tmpArrayList);
}
// .readLine()은 끝에 개행문자를 읽지 않는다.
bufReader.close();
} catch (FileNotFoundException e) {
// TODO: handle exception
} catch (IOException e) {
System.out.println(e);
}
}
public static double LinearAnalayis(int idx) {
double sumx = 0, sumy = 0, sumx2 = 0;
int n = eaten_data.size();
for (int i = 0; i < n; i++) {
sumx += i + 1;
sumx2 += (i + 1) * (i + 1);
sumy += Double.parseDouble(eaten_data.get(i).get(idx));
}
double xbar = sumx / n;
double ybar = sumy / n;
double xxbar = 0.0, yybar = 0.0, xybar = 0.0;
for (int i = 0; i < n; i++) {
xxbar += (i + 1 - xbar) * (i + 1 - xbar);
yybar += (Double.parseDouble(eaten_data.get(i).get(idx)) - ybar)
* (Double.parseDouble(eaten_data.get(i).get(idx)) - ybar);
xybar += (i + 1 - xbar) * (Double.parseDouble(eaten_data.get(i).get(idx)) - ybar);
}
double beta1 = xybar / xxbar;
double beta0 = ybar - beta1 * xbar;
return beta1 * (n + 1) + beta0;
}
public static double getFinalResult(double input, int idx) {
switch (idx) {
case 1: break;
case 2: idx=5; break;
case 3: idx=6; break;
case 4: idx=7; break;
default : break;
}
return LinearAnalayis(idx)-input;
}
public static int classficate(double Finalresult) {
double sumplus=0;
double summinus=0;
for(int i=0;i<eaten_data.size();i++) {
if(eaten_data.get(i).get(10)=="1") {
sumplus+=Double.parseDouble(eaten_data.get(i).get(11));
}
else {
summinus+=Double.parseDouble(eaten_data.get(i).get(11));
}
}
sumplus=Math.abs(sumplus-Finalresult);
summinus=Math.abs(summinus-Finalresult);
return sumplus<summinus ? 1 : 0;
}
}