2021年3月2日 星期二

Python統計分析與最佳化計算課程

課程介紹:

課程採用免費Python軟體為主,主題包括基礎的資料結合、資料摘要與繪圖技巧、統計檢定、邏輯斯迴歸、高維度影像資料計算、線性與非線性最佳化應用。課程將透過案例示範與實際操作練習以增進學習成效。

課程目標:

使學員熟悉Python語言進行統計分析與最佳化計算,課程包括Python與資料庫連結、資料摘要與繪圖技巧、統計分析、線性與非線性模型最佳化計算。

報名: https://www.beclass.com/rid=2443e965faa62b76ba01



2021年1月21日 星期四

水文建模shiny應用 (Hydrological Modelling with shiny app)

主題

R在水文模型具有廣泛應用,包括水文資料的讀取與下載,水文資料分析與建模,詳細可參考 CRAN-Hydrology  [https://cran.r-project.org/web/views/Hydrology.html]。 其中 AirGR套件提供水文建模工具,該套件包括降雨徑流模型,積雪和融雪模型以及相關的校準和評估功能。另 airGRteaching套件,將建模結果以互動式網頁呈現,提供使用者更進一步的水文分析與應用。如果需修改為職場的內容,可修改 "C:\Users\88697\Documents\R\win-library\4.0\airGRteaching\ShinyGR"資料夾的 ui.R,server.R 二個檔案。

執行結果1: 水文模型


執行結果2: 水文模型綱要圖





關鍵字

Hydrological Modelling 
# shiny

套件

1. airGR 套件 - 建立水文建模 https://cran.r-project.org/web/packages/airGR/index.html
2. airGRteaching 套件- 提供水文建模互動式網頁呈現 https://cran.r-project.org/web/packages/airGRteaching/index.html
3. shiny套件 - 提供互動式網頁功能 https://cran.r-project.org/web/packages/shiny/index.html

R程式碼下載


R程式碼

# title   : 水文建模shiny應用
# author  : Ming-Chang Lee
# email   : alan9956@gmail.com
# RWEPA   : http://rwepa.blogspot.tw/
# GitHub  : https://github.com/rwepa
# resource: https://rwepa.blogspot.com/2021/01/shiny-hydrological-modelling-with-shiny.html

# 安裝2個基本套件
# install.packages(c("airGR", "airGRteaching"))

# 使用remotes套件, 安裝最新版htmlwidgets套件
# 一定要安裝最新版htmlwidgets套件, 否則會有錯誤.
# install.packages("remotes")
# remotes::install_github("ramnathv/htmlwidgets")

library(airGRteaching)

# 載入L0123001資料集
data(L0123001, package = "airGR")

# 顯示物件清單
ls()
# [1] "BasinInfo" "BasinObs"

head(BasinObs)
# "DatesR": 年-月-日, POSIXct物件
# "P"  : average precipitation [mm/time step] 每日平均降雨量
# "T"  : catchment average air temperature [℃]  平均氣溫
# "E"  : catchment average potential evapotranspiration [mm/day] 平均潛在蒸發量
# "Qls": outlet discharge [l/s] 出水口排出量
# "Qmm": outlet discharge [mm/day] 出水口排出量

# 建立低地盆地資料
BV_L0123001 <- BasinObs[0001:6000, c("DatesR", "P", "E", "Qmm", "T")]

# 建立多山盆地資料
BV_L0123002 <- BasinObs[5000:9999, c("DatesR", "P", "E", "Qmm", "T")]
BI_L0123002 <- BasinInfo

# 建立互動式網頁資料分析
ShinyGR(ObsDF = list("Low-land basin" = BV_L0123001, "Mountainous basin" = BV_L0123002),
        ZInputs = list(NULL, median(BI_L0123002$HypsoData)),
        HypsoData = list(NULL, BI_L0123002$HypsoData),
        NLayers = list(5, 5),
        SimPer = list(c("1994-01-01", "1998-12-31"), c("2004-01-01", "2006-12-31")),
        theme = "United")

# theme 預設值為 "RStudio",可修改為以下值:
# ["Cerulean""Cyborg""Flatly""Inrae""Saclay""United" or "Yeti"]

2020年9月26日 星期六

ggplot2-新增數學式-以迴歸分析模型為例 (regression line equation)

ggplot2-regression line equation



感謝Joey提供此問題, 本篇文章說明 ggplot2 繪圖新增數學式-以迴歸分析模型為例 (regression line equation)

主題

1. 使用 group_by 與 do 建立迴歸分析
2. 方法1:使用文字型態建立註釋(annotation)
3. 方法2:使用 expression 建立註釋(annotation),加上 parse = TRUE
4. 使用 annotate 新增迴歸分析數學式標示
5. 使用 ggtitle 建立標題
6. 使用 element_text(hjust = 0.5) 設定標題置中排列

關鍵字

# group_by
# do
# paste0
# round
# ifelse
# geom_point
# geom_smooth
# annotate
# ggtitle
# theme

套件

1. ggplot2 - 使用 ggplot2 套件繪圖
2. dplyr - 使用 %>% 與 group_by 進行資料處理

R程式碼下載


R程式碼


# title        : ggplot2-新增數學式-以迴歸分析模型為例
# author    : Ming-Chang Lee
# email     : alan9956@gmail.com
# RWEPA : http://rwepa.blogspot.tw/
# GitHub  : https://github.com/rwepa
# resource : https://rwepa.blogspot.com/2020/09/ggplot2-equation.html

library(ggplot2)
library(dplyr)

head(iris)

# 計算群組lm
fitted_models <- iris %>%
  group_by(Species) %>% 
  do(model = summary(lm(Petal.Length ~ Petal.Width, data = .)))

# levels:取出Species欄位的所有可能等級
names(fitted_models$model) <- levels(iris$Species)

# 檢視成果
fitted_models

# 查看群組lm結果(全部)
fitted_models$model

# 查看群組lm結果(setosa)
fitted_models$model$setosa

# 方法1:使用文字型態建立註釋(annotation)
mylabel <- c()
for (i in 1:length(fitted_models$model)) {
  mylabel <- c(mylabel, paste0(names(fitted_models$model[i]), ': Petal.Length = ',
                               round(fitted_models$model[[i]]$coefficients[1], 2), " ",
                               ifelse(fitted_models$model[[i]]$coefficients[2] >= 0, '+ ', ''),
                               round(fitted_models$model[[i]]$coefficients[2], 2), ' * Petal.Width,',
                               ' R2 = ', round(fitted_models$model[[i]]$r.squared, 2)))
}
mylabel
gg_color_hue <- function(n) {
  hues = seq(15, 375, length = n + 1) # seq(0, 360, ...)
  hcl(h = hues, l = 65, c = 100)[1:n]
}
# 繪製群組迴歸模型
p <- ggplot(iris, aes(Petal.Width, Petal.Length, group=Species)) +
  geom_point(aes(color=Species), size=2) +
  geom_smooth(aes(color=Species), method=lm, se=FALSE) +
  annotate('text', label = mylabel, x = 0.7, y = c(2.5, 2, 1.5), size = 4, hjust = 0, color = gg_color_hue(n = 3)) +
  ggtitle("iris群組線性模型統計圖") +
  theme(plot.title = element_text(hjust = 0.5)) # 設定標題置中排列
p

# 方法2:使用expression建立註釋(annotation),加上 parse = TRUE
mylabel <- c()
for (i in 1:length(fitted_models$model)) {
  mylabel <- c(mylabel, paste0(names(fitted_models$model[i]), ': ', 'Petal.Length == ',
                               round(fitted_models$model[[i]]$coefficients[1], 2), " ",
                               ifelse(fitted_models$model[[i]]$coefficients[2] >= 0, '+ ', ''),
                               round(fitted_models$model[[i]]$coefficients[2], 2), ' * Petal.Width ', '~', 
                               'R^{2} == ', round(fitted_models$model[[i]]$r.squared, 2)))
}
mylabel
p <- ggplot(iris, aes(Petal.Width, Petal.Length, group=Species)) +
  geom_point(aes(color=Species), size=2) +
  geom_smooth(aes(color=Species), method=lm, se=FALSE) +
  annotate('text', label = mylabel, x = 0.7, y = c(2.5, 2, 1.5), size = 4, hjust = 0, color = gg_color_hue(n = 3), parse = TRUE) +
  ggtitle("iris群組線性模型統計圖-使用 parse參數") +
  theme(plot.title = element_text(hjust = 0.5))
p
# end

2020年9月20日 星期日

ggplot2 套件 - hcl 客製化繪圖顏色 (customized color)










# ggplot2
# scale
# hcl
# ggtitle
# theme

本篇文章說明 ggplot2 繪圖的顏色主題,內容包括:
1. 使用標準單一顏色.
2. 使用ggplot2內建群組顏色.
3. 使用scales套件,檢視繪圖資訊.
4. 使用客製化 hcl {grDevices}

套件:
1. ggplot2 - 使用ggplot2套件繪圖
2. scales - 擷取ggplot2套件的繪圖資訊
3. gridExtra - 進行多列多行 ggplot2 繪圖


R程式碼下載:

R程式碼:
library(ggplot2)

# 方法1 使用標準單一顏色
ggplot(iris, aes(Petal.Width, Petal.Length)) +
  geom_point(size=2) +
  ggtitle("圖1 ggplot2-使用標準單一顏色") +
  theme(plot.title = element_text(hjust = 0.5))

圖1 ggplot2-使用標準單一顏色



# 方法2 使用ggplot2內建群組顏色
ggplot(iris, aes(Petal.Width, Petal.Length, color=Species)) +
  geom_point(size=2) +
  ggtitle("圖2 ggplot2-使用內建群組顏色") +
  theme(plot.title = element_text(hjust = 0.5))

圖2 ggplot2-使用內建群組顏色




















# 方法3 使用scales套件,檢視繪圖資訊.
library(scales)

p <- ggplot(iris, aes(Petal.Width, Petal.Length, group=Species)) +
  geom_point(aes(color=Species), size=2)

# ggplot2使用的繪圖顏色
# ggplot_build {scale} 可擷取ggplot2繪圖的相關資訊等
col <- unique(ggplot_build(p)$data[[1]]$colour)

# 顯示ggplot2使用的繪圖顏色
show_col(col) # "#F8766D" "#00BA38" "#619CFF" (紅綠藍)

# 將所有顏色轉換成 factor, 加上 levels 參數, 以免顏色異常.
mycol <- factor(ggplot_build(p)$data[[1]]$colour, 
                levels = c("#F8766D", "#00BA38", "#619CFF"))

# 使用 gridExtra 套件進行多列,多行繪圖, 類似 par(mfrow=c(1,2))功能
library(gridExtra)

p1 <- ggplot(iris, aes(Petal.Width, Petal.Length)) +
  geom_point(aes(color=Species), size=2) +
  ggtitle("圖3 ggplot2-使用預設顏色") +
  theme(plot.title = element_text(hjust = 0.5))

p2 <- ggplot(iris, aes(Petal.Width, Petal.Length)) +
  geom_point(aes(color=mycol), size=2) +
  ggtitle("圖4 ggplot2-使用scales套件(二者相同") +
  theme(plot.title = element_text(hjust = 0.5))

# 設定1列, 2行繪圖
# 右側圖例有改善空間!
grid.arrange(p1, p2, nrow=1, ncol=2)

圖3, 圖4 ggplot2-使用scales套件(二者相同



# 方法4 使用客製化 hcl {grDevices}

# ggplot2 內部使用 HCL 顏色規範,參考孟塞爾顏色系統 (Munsell Color System)


# 色相(hue)指的是色彩的外相,表示在不同波長的光照射下,人眼所感覺不同的顏色.
# 在HSL和HSV色彩空間中, H指的就是色相,以紅色為0度(360度);黃色為60度;綠色為120度;青色為180度;藍色為240度;品紅色為300度.

# hcl(h = 0, c = 35, l = 85, alpha, fixup = TRUE)

# h(hue 色相): The hue of the color specified as an angle in the range [0,360].
# 0 yields red, 120 yields green 240 yields blue, etc.

# c(chroma 色度): The chroma of the color. The upper bound for chroma depends on hue and luminance.

# l(value 明度): A value in the range [0,100] giving the luminance of the colour. For a given combination of hue and chroma, only a subset of this range is possible.

# hue 色相    : 0~360度表示
# chroma 色度 : 中間為0, 向外擴散增加
# value 明度  : 南北上下軸表示明度(value)的深淺, 從全黑(N0)至全灰(N5)到全白(N10)




gg_color_hue <- function(n) {
  hues = seq(15, 375, length = n + 1) # seq(0, 360, ...)
  hcl(h = hues, l = 65, c = 100)[1:n]
}

n <- 3

gg_color_hue(n) # "#F8766D" "#00BA38" "#619CFF"

cols <- factor(rep(gg_color_hue(n), each = 50),
               levels = c("#F8766D", "#00BA38", "#619CFF"),
               labels = c("setosa", "versicolor", "virginica"))

ggplot(iris, aes(Petal.Width, Petal.Length, color=cols)) +
  geom_point(size=2) +
  ggtitle("圖5 ggplot2-使用客製化hcl函數") +
  theme(plot.title = element_text(hjust = 0.5)) +
  scale_colour_discrete("Species") +
  theme(legend.title.align=0.5)



# end

2020年7月13日 星期一

Python機器學習應用課程

Python機器學習應用課程-確定開課~歡迎報名參加


課程網址:

http://www.asia-analytics.com.tw/tw/edu/e-hot-1090829-30.jsp

課程介紹:

本課程採用開放源始碼的Python語言、免費Spyder軟體,課程主軸包括機器學習標準流程與機器學習應用,引導學員從資料探索式分析集群分析數值與分類預測應用,課程將透過案例示範與操作練習以增進學習成效。

開課單位:台灣析數

課程目標:

1. 熟悉Python語言做為資料分析發展平台。
2. 學習基礎統計理論與應用情境。
3. 熟悉探索式資料分析方法,了解資料隱涵的價值。
4. 使用Python強化機器學習的使用,完成企業級機器學習應用。

課程資訊:

課程時間:2020/08/29(六)-08/30(日)
課程地址:台北市內湖區內湖路一段356號5樓
課程天數:2天;上午 9:30 至下午16:30 (中午休息1小時)

#Python
#Anaconda
#Machine Learning, ML

2020年6月17日 星期三

R-4.0.1-Rcmdr套件開啟異常

問題: 近日有網友提及使用 R-4.0.1 時, 載入 Rcmdr套件異常, 無法正常載入.



方法: 考慮 Windows 環境, 一般遇到套件無法載入, 可參考以下方法:
  1. 在 R的原生環境下重新安裝套件
  2. 使用檔案總管, 刪除套件所在資料夾, 再重新安裝.
  3. 安裝較低版本的套件
  4. 安裝較低版本的R
本例採用方法4, 重新安裝較低版本的R, 即改用 R-4.0.0  即可正常開啟 Rcmdr視窗.


參考完整說明:
# end



2020年6月9日 星期二

2020 R語言資料物件操作與線性模型應用 直播課程+ 線上Q&A

2020 R語言資料物件操作與線性模型應用 直播課程 + 線上Q&A


課程網址


課程日期

正式直播課程:2020/06/23 (二) 14:00-15:30 (13:30系統開放入場)
課前測試設備:2020/06/22 (一) 14:00-15:00 (學員可自由參加)

課程目標

1.理解企業在進行資料分析時,如何善用R語言出關鍵資料,分析成果。
2.完成課程後,能夠將所學案例實際應用在職場中,達到立即成效。
3.擺脫熟背R函數的困境,直覺式使用R語言進行資料科學運算。

課程對象

1.使用R語言進行資料科學的部門主管、資訊人員、行政人員
2.想提升R語言資料科學應用能力者(無需程式設計能力)