forked from horden316/FinancialReportAnalyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw.R
182 lines (160 loc) · 7.8 KB
/
draw.R
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
draw <- function() {
################################################################################
# plot title, color
ratio <- c("負債佔資產比率", "長期資金佔固定資產比率", "流動比率", "速動比率",
"利息保障倍數", "應收帳款週轉率", "平均收現日數", "存貨週轉率", "平均售貨日數",
"固定資產週轉率", "總資產週轉率", "資產報酬率", "股東權益報酬率", "純益率", "每股盈餘",
"現金流量比率", "現金再投資比率")
# ratio <- c("負債佔資產比率","長期資金佔固定資產比率","流動比率","速動比率",
# "利息保障倍數", "應收帳款週轉率")
color <- c("orange", "blue")
#
################################################################################
#
# define array
rnum <- length(ratio) # Ratio - row
cnum <- length(year)-1 # Year - column
dnum <- length(stock) # Stock - dimension
dtax <- array(numeric(), c(rnum, cnum, dnum)) # 3D array
dimnames(dtax) <- list(ratio, year[2:(cnum+1)], stock) # named array
#
################################################################################
#
# ratio calculate
ry <- 2:length(year) # draw year
dy <- 1:(length(year)-1) # data year
#負債佔資產比率
Ratio <- balance_dta[which(mAcc_balances == " 負債總額"), ry, ] / (
balance_dta[which(mAcc_balances == " 資產總額"), ry, ] )
Ratio <- as.data.frame(t(Ratio))
dtax[1, , 1] <- unlist(Ratio[1, ])
dtax[1, , 2] <- unlist(Ratio[2, ])
#長期資金佔固定資產比率
Ratio <- (balance_dta[which(mAcc_balances == " 權益總額"), ry, ]+balance_dta[which(mAcc_balances == " 非流動負債合計"), ry, ]) /
balance_dta[which(mAcc_balances == " 非流動資產合計"), ry, ]
Ratio <- as.data.frame(t(Ratio))
dtax[2, , 1] <- unlist(Ratio[1, ])
dtax[2, , 2] <- unlist(Ratio[2, ])
#
#
#流動比率
Ratio <- (balance_dta[which(mAcc_balances == " 流動資產合計"), ry, ]) /
balance_dta[which(mAcc_balances == " 流動負債合計"), ry, ]
Ratio <- as.data.frame(t(Ratio))
dtax[3, , 1] <- unlist(Ratio[1, ])
dtax[3, , 2] <- unlist(Ratio[2, ])
#速動比率
Ratio <- (balance_dta[which(mAcc_balances == " 流動資產合計"), ry, ]-balance_dta[which(mAcc_balances == " 存貨"), ry, ]-balance_dta[which(mAcc_balances == " 預付款項"), ry, ]) /
balance_dta[which(mAcc_balances == " 流動負債合計"), ry, ]
Ratio <- as.data.frame(t(Ratio))
dtax[4, , 1] <- unlist(Ratio[1, ])
dtax[4, , 2] <- unlist(Ratio[2, ])
#利息保障倍數
Ratio <- (income_dta[which(mAcc_income == "稅前淨利(淨損)"), ry, ]+income_dta[which(mAcc_income == "所得稅費用(利益)合計"), ry, ]) /
cash_dta[which(mAcc_cash == " 利息費用"), ry, ]
Ratio <- as.data.frame(t(Ratio))
dtax[5, , 1] <- unlist(Ratio[1, ])
dtax[5, , 2] <- unlist(Ratio[2, ])
#應收帳款週轉率
Ratio <- income_dta[which(mAcc_income == " 銷貨收入淨額"), ry, ] /
((
balance_dta[which(mAcc_balances == " 應收票據淨額"), ry,] +
balance_dta[which(mAcc_balances == " 應收帳款淨額"), ry, ] +
balance_dta[which(mAcc_balances == " 應收帳款-關係人淨額"), ry, ] +
balance_dta[which(mAcc_balances == " 應收票據淨額"), dy,] +
balance_dta[which(mAcc_balances == " 應收帳款淨額"), dy, ] +
balance_dta[which(mAcc_balances == " 應收帳款-關係人淨額"), dy, ]
) / 2
)
Ratio <- as.data.frame(t(Ratio))
dtax[6, , 1] <- unlist(Ratio[1, ])
dtax[6, , 2] <- unlist(Ratio[2, ])
#平均收現日數
dtax[7, , ] <- 365 / dtax[6, , ]
#存貨週轉率
Ratio <- income_dta[which(mAcc_income == " 銷貨成本"), ry, ] /
((balance_dta[which(mAcc_balances == " 存貨"), ry,] + balance_dta[which(mAcc_balances == " 存貨"), dy, ]) / 2)
Ratio <- as.data.frame(t(Ratio))
dtax[8, , 1] <- unlist(Ratio[1,])
dtax[8, , 2] <- unlist(Ratio[2,])
#平均售貨日數
dtax[9, , ] <- 365 / dtax[8, , ]
#固定資產週轉率
Ratio <- income_dta[which(mAcc_income == " 銷貨收入淨額"), ry, ] /
balance_dta[which(mAcc_balances == " 不動產、廠房及設備"), ry,]
Ratio <- as.data.frame(t(Ratio))
dtax[10, , 1] <- unlist(Ratio[1,])
dtax[10, , 2] <- unlist(Ratio[2,])
#總資產週轉率
Ratio <- income_dta[which(mAcc_income == " 銷貨收入淨額"), ry, ] /
balance_dta[which(mAcc_balances == " 資產總額"), ry,]
Ratio <- as.data.frame(t(Ratio))
dtax[11, , 1] <- unlist(Ratio[1,])
dtax[11, , 2] <- unlist(Ratio[2,])
#資產報酬率
Ratio <- (income_dta[which(mAcc_income == "本期淨利(淨損)"), ry,] + cash_dta[which(mAcc_cash == " 利息費用"), ry, ] *
(1 - (income_dta[which(mAcc_income == "所得稅費用(利益)合計"), ry,] / income_dta[which(mAcc_income == "稅前淨利(淨損)"), ry,]))) /
((balance_dta[which(mAcc_balances == " 資產總額"), ry, ] + balance_dta[which(mAcc_balances == " 資產總額"), dy, ]) /
2)
# Ratio <- income_dta[which(mAcc_income == "本期淨利(淨損)"), ry,]/
# ((balance_dta[which(mAcc_balances == " 資產總額"), ry, ] + balance_dta[which(mAcc_balances == " 資產總額"), dy, ]) /
# 2)
Ratio <- as.data.frame(t(Ratio))
dtax[12, , 1] <- unlist(Ratio[1,])
dtax[12, , 2] <- unlist(Ratio[2,])
#股東權益報酬率
Ratio <- income_dta[which(mAcc_income == "本期淨利(淨損)"), ry, ] /
((balance_dta[which(mAcc_balances == " 權益總額"), ry,] + balance_dta[which(mAcc_balances == " 權益總額"), dy,]) /
2)
Ratio <- as.data.frame(t(Ratio))
dtax[13, , 1] <- unlist(Ratio[1,])
dtax[13, , 2] <- unlist(Ratio[2,])
# 純益率
Ratio <- income_dta[which(mAcc_income == "本期淨利(淨損)"), ry,] /
income_dta[which(mAcc_income == " 銷貨收入淨額"), ry,]
Ratio <- as.data.frame(t(Ratio))
dtax[14, , 1] <- unlist(Ratio[1,])
dtax[14, , 2] <- unlist(Ratio[2,])
#每股盈餘
Ratio <- income_dta[which(mAcc_income == " 基本每股盈餘"), ry,]
Ratio <- as.data.frame(t(Ratio))
dtax[15, , 1] <- unlist(Ratio[1,])
dtax[15, , 2] <- unlist(Ratio[2,])
#現金流量比率
Ratio <- (cash_dta[which(mAcc_cash == "營業活動之淨現金流入(流出)"), ry, ]) /
balance_dta[which(mAcc_balances == " 流動負債合計"), ry, ]
Ratio <- as.data.frame(t(Ratio))
dtax[16, , 1] <- unlist(Ratio[1, ])
dtax[16, , 2] <- unlist(Ratio[2, ])
#現金再投資比率
Ratio <- (cash_dta[which(mAcc_cash == "營業活動之淨現金流入(流出)"), ry, ]-cash_dta[which(mAcc_cash == " 發放現金股利"), ry, ]) /
(balance_dta[which(mAcc_balances == " 非流動資產合計"), ry, ]+balance_dta[which(mAcc_balances == " 流動資產合計"), ry, ]-balance_dta[which(mAcc_balances == " 流動負債合計"), ry, ])
Ratio <- as.data.frame(t(Ratio))
dtax[17, , 1] <- unlist(Ratio[1, ])
dtax[17, , 2] <- unlist(Ratio[2, ])
#
################################################################################
#
# draw
# print(dtax)
for (r in 1:rnum) {
Yrange <- c(min(dtax[r, , ]) / 1.1, max(dtax[r, , ]) * 1.1) # Y
#print(paste("r: ",r))
for (d in 1:dnum) {
#print(paste("d: ",d))
plot(unlist(dtax[r, , d]),
type = "l", col = paste(color[d]), lwd = 2, main = paste(ratio[r]),
ylim = Yrange, ylab = "", xlab = "年", xaxt = "n"
)
points(unlist(dtax[r, , d]), pch = 16, cex = 1.25, col = paste(color[d]))
if (d != dnum) {
par(new = T)
}
}
axis(
1, 1:(length(year)-1),
format(as.Date(as.character(year[2:6]), format = "%Y"), "%Y")
) # X
}
return(dtax)
}