QGIS PLUGIN · 02

Geographically and Temporally Weighted Regression時空地理加權迴歸

A local modelling approach that considers variation across both space and time.

GTWR

Software required – QGIS & R需下載軟體 – QGIS & R

※ Applicable versions: QGIS 3.16 or later; R 3.0.0 or later※ 適用版本:QGIS 3.16;R 3.0.0 以上

Ⅰ. INTRODUCTIONⅠ. 介紹

What is GTWR?什麼是 GTWR?

GTWR (Geographically and Temporally Weighted Regression) is an extension of GWR (Geographically Weighted Regression). It is a statistical method used in Geographic Information Science (GIS) to explore spatial data relationships.

GTWR(Geographically and Temporally Weighted Regression)時空地理加權迴歸,屬於 GWR 的擴展版本,為地理資訊科學(GIS)中探討空間數據關係的統計方法。

GTWR considers not only the effect of geographical location on the response variable but also the temporal factor, handling spatial and temporal variation simultaneously – for example in disease transmission or real-estate market fluctuations, patterns that may change across both time and space.

GTWR 不僅考量地理位置對於反應變數的影響,也考量到時間因素,並能同時處理空間和時間的變異性,例如疾病傳播、房地產市場波動等,這些模式都可能因時間和空間的不同而有所改變。

Main features and considerations主要特點與考慮因素

  1. Spatial and temporal weights空間和時間的權重

    Geographical and temporal weights are used to calculate regression parameters for each location and time point: observations that are closer (in space or in time) have a greater influence on that point’s parameters.

    使用地理和時間加權來計算每個位置和時間點的迴歸參數,意即相對較近的觀測值(無論在空間或時間上)對該點的迴歸參數影響越大。

  2. Model interpretation模型解釋

    Like GWR, GTWR can produce parameter maps showing how a parameter changes with location and time, helping explain spatial variability.

    GTWR 與 GWR 類似,均能生成參數地圖,顯示某一特定參數如何隨地理位置和時間而變化,利於解釋空間變異性。

  3. Data requirements數據需求

    Because of the model’s complexity, a large amount of data is needed for stable and reliable results, covering a sufficient time span and spatial extent.

    考量 GTWR 模型的複雜性,需要大量數據來確保結果的穩定性和可靠性;同時,數據應涵蓋充足的時間及空間範疇,以便正確估計模型參數。

  4. High computational load計算量高

    GTWR requires more computing resources, since it fits a regression equation for every location and time point.

    GTWR 需要更多的計算資源,因為它在每個地理位置和時間點都有一個迴歸方程式。

Plugin advantagesPlugin 優點

1. User-friendly1. 操作簡單

Install R and QGIS, add the Processing R Provider plugin in QGIS, and copy the script files to the designated folder. Later changes can be made by editing the script files directly.

使用者僅需安裝 R 及 QGIS,在 QGIS 中安裝 Processing R Provider 插件後,再將相關程式文檔複製到指定路徑下即可使用;若後續需調整,可直接更改程式文檔。

2. Time clustering2. 時間分組

The time variable can be grouped – daily, monthly, yearly or by period – e.g. every five days as a new group, or daily data merged into months. Choose the unit (year, month, day or the data’s default unit) and the interval; the model then uses the new grouping as its time variable.

使用者可依需求將時間變數分組,不論每日、每月、每年或期數皆可,例如每五天為一組,或將每日資料合併為每月。可調整單位(年、月、日或預設資料單位)及區間,GTWR 模型會以新建立的時間分組作為時間變數。

3. Model visualization3. 模型視覺化

After the run, T-value images for each explanatory variable at every location and time are saved to the output folder. Significant locations are coloured by T-value; non-significant ones are white.

執行程序後,指定的資料夾會新增各解釋變數在各地點與時間的 T 值視覺化圖片;顯著的地點會依 T 值大小顯示不同顏色,不顯著則為白色。

4. Handles QGIS variable-name truncation4. QGIS 縮減變數名稱問題

QGIS truncates field names longer than ten characters when processing, while the parameter menu shows full names. The tool truncates selected names the same way to avoid mismatches.

QGIS 處理資料時會將超過十個字元的變數名稱縮減,參數選單則仍為完整名稱,因此本程序會依 QGIS 的方式縮減所選變數名稱,避免後端資料與使用者所選不一致。

Notes注意事項

• If the time variable is already on a coarse scale (e.g. years or months), a finer unit (months or days) cannot be selected. For non-date formats such as periods, only the default data unit is available.
• Because QGIS’s truncation depends on field order, problems may still occur if the selected variable order differs from the field order in the original data.

• 若原本資料的時間變數尺度較大(例如年或月),則無法選擇更小的單位(例如月或日);若資料為期數等非日期格式,單位僅能選擇預設資料單位。
• 由於 QGIS 縮減名稱的方式與變數順序有關,若所選變數順序與原始資料的變數順序不同,仍可能出現問題。

T-value visualization example
Visualization example視覺化範例

Ⅱ. INSTALLATION PROCEDUREⅡ. 安裝流程

Install the GTWR tool in QGIS在 QGIS 安裝 GTWR 工具

  1. Step 1

    Install QGIS and R.

    安裝 QGIS 與 R。

  2. Step 2

    Open QGIS and click Plugins › Manage and Install Plugins… in the top menu.

    開啟 QGIS 後,於上方選單點選 Plugins › Manage and Install Plugins…。

    QGIS Plugins menu
  3. Step 3

    Search for Processing R Provider, then click Install Plugin › Close.

    搜尋 Processing R Provider 後,點選 Install Plugin › Close(關閉)。

    Installing Processing R Provider
  4. Step 4

    Restart QGIS, then click Settings › Options… in the top menu.

    重新啟動 QGIS 後,於上方選單點選 Settings › Options…。

    QGIS Settings menu
  5. Step 5

    Click Processing › Providers › R and set the R folder path (e.g. C:\Program Files\R\R-4.3.1; the actual path depends on your system and R version).

    點選 Processing › Providers › R,設定 R folder 路徑(例如:C:\Program Files\R\R-4.3.1,實際路徑可能因系統、版本或安裝位置而異)。

    ※ If the installed R is 64-bit, check Use 64 bit version.※ 若 R 安裝版本為 64 bit,須勾選 Use 64 bit version。

    R provider settings on Windows
    Windows settingsWindows 設定
    R provider settings on macOS
    Mac settingsMac 設定
  6. Step 6

    Open the R scripts folder.

    開啟 R scripts folder 資料夾。

    ※ This folder stores GTWR and other R script files (it is located under the QGIS installation / profile directory).※ 此資料夾用於存放 GTWR 或其他 R 程式檔案(路徑位於 QGIS 的安裝目錄下)。

  7. Step 7

    Unzip the program file and copy gtwr.rsx and gtwr.help into the R scripts folder.

    解壓縮程式檔,將 gtwr.rsx、gtwr.help 兩個檔案放入 R scripts folder 資料夾。

    gtwr.rsx and gtwr.help in the R scripts folder
  8. Step 8

    Restart QGIS and check in the Processing Toolbox that the R category and its GWmodel group are installed. If Geographical and Temporal Weighted Regression appears under GWmodel and opens normally, the installation is complete.

    重新啟動 QGIS,於 Processing Toolbox 確認是否成功出現 R 類別及其下的 GWmodel 資料夾。若在 GWmodel 類別中看到 Geographical and Temporal Weighted Regression 程序並能正常開啟,即表示安裝完成。

    ※ If the tool appears but fails to open, check the R folder path set in Step 5.※ 若出現 Geographical and Temporal Weighted Regression 程序但開啟時發生問題,可能是 Step 5 設定的 R folder 路徑有誤。

    GWmodel tools in the Processing Toolbox
    Example of a completed setup設定完成示範

Ⅲ. EXAMPLE OPERATIONⅢ. 範例操作

Run a GTWR model執行 GTWR 模型

  1. Import data輸入資料
  2. Select parameters選擇參數
  3. Time clustering日期分群
  4. Output results輸出結果

Step 1Import data輸入資料

Import spatiotemporal point or polygon data into QGIS (the sample data can be used to try the tool).匯入時空點資料或時空面資料(可使用範例資料練習)。

Data loaded in QGIS
GTWR data inputGTWR 資料輸入

Open the Geographical and Temporal Weighted Regression tool and choose the input data in Layer.匯入資料後,開啟 Geographical and Temporal Weighted Regression 程序,於 Layer 選取輸入資料。

Layer selection
GTWR data selectionGTWR 選擇資料

Step 2Select parameters選擇參數

This is the main user interface. Choose the inputs and parameters you need, and define the output folder.

此部分為使用者主要操作介面,可根據需求選擇相對應的參數與輸入,並定義輸出資料路徑。

Parameter參數Type類型Description說明
Dependent (response) variable反應變數Single choice單選Select one field as the response variable.由所有欄位中選擇一個作為反應變數。
Explanatory variables解釋變數Multiple choice多選Select one or more fields as explanatory variables.由所有欄位中選擇一個或多個解釋變數。
Observation timetag時間變數Single choice單選The field holding the observation time (date or period).記錄觀測時間(日期或期數)的欄位。
Kernel functionList清單Gaussian, Exponential, Bisquare, Tricube, Boxcar
ApproachList清單Bandwidth selection criterion: CV, AIC, AICc, BIC頻寬選擇準則:CV、AIC、AICc、BIC
Fixed / AdaptiveOption選項Fixed-distance or adaptive (nearest-neighbour) bandwidth.固定距離頻寬或適應性(最近鄰數)頻寬。
Other parameters其他參數Optional選填All have default values; adjust as needed.皆有預設值,可依情況自行調整。
Output folder path輸出資料路徑Required必填Folder where the results are written.指定結果輸出的資料夾。

● Dependent variable● 反應變數

A single-choice list of all fields; select the response variable you need.

所有變數的單選選單,可依需求選擇相應的反應變數。

Dependent variable selector
GTWR dependent variableGTWR 反應變數

● Explanatory variables● 解釋變數

A multiple-choice list of all fields; select the explanatory variables you need.

所有變數的多選選單,可依需求選擇相應的解釋變數。

Explanatory variables selector
GTWR explanatory variablesGTWR 解釋變數

● Observation timetag● 時間變數

Select the field that records when each observation was made.

選擇記錄各觀測值時間的欄位。

Time variable selector
Observation timetag時間變數

● Kernel, Approach and Fixed / Adaptive● Kernel、Approach 以及 Fixed / Adaptive 選擇

These settings are used to calculate the bandwidth and to fit the subsequent model.

選擇計算 Bandwidth 的參數及後續模型的參數。

○ Kernel function – Gaussian, Exponential, Bisquare, Tricube, Boxcar○ Kernel Function – Gaussian、Exponential、Bisquare、Tricube、Boxcar

Kernel function list
GTWR kernel function listGTWR Kernel Function 清單

○ Approach – CV, AIC, AICc, BIC○ GTWR Approach – CV、AIC、AICc、BIC

Approach list
GTWR approach listGTWR Approach 清單

○ Fixed or adaptive bandwidth○ Fixed 或 Adaptive bandwidth

Fixed or adaptive bandwidth option
Fixed or Adaptive bandwidth

● Other parameters● 其他參數選擇

All have default values and can be adjusted according to your situation.

皆有預設值,可以根據情況自行調整。

Other parameters
Other GTWR parametersGTWR 其他參數選擇

● Output folder path● 輸出資料路徑

An output folder must be specified.

需指定資料輸出路徑。

Output folder path
GTWR output folder pathGTWR 輸出資料路徑

Step 3Time clustering日期分群

Optionally group the current time variable – e.g. a new group every five days, monthly data aggregated into years, or daily data into months. Both dates and periods can be grouped. After grouping, the model uses the new grouped time variable.可選擇是否將目前的時間變數分群,例如每五天為新的一組,或將每月資料合併成每年、每日資料合併成每月等。不論資料型態是日期或期數,皆可依需求分組;分組後,模型會使用新的分組時間變數進行建模。

First choose the interval size and the unit. The unit must be equal to or coarser than the time variable’s scale: daily data can use any unit, but monthly data cannot be grouped by day, and so on. For non-date time variables (e.g. periods) choose Default. For date variables, you must also enter the date format.使用者需先選擇區間大小以及單位。單位僅能選擇比時間變數尺度更大(或相同)的,例如時間變數為每日,則單位可選任一種;若時間變數為每月,則無法選擇更小的每日,以此類推。若時間變數並非日期格式(例如期數),單位選擇 Default 即可;若時間變數為日期,則需另外輸入日期格式。

Time variable時間變數Allowed units可選單位Date format needed?需輸入日期格式?
Daily date每日(日期)Day, Month, Year日、月、年Yes是
Monthly date每月(日期)Month, Year月、年Yes是
Yearly date每年(日期)Year年Yes是
Period / non-date期數等非日期DefaultNo否
Time grouping interval
GTWR time clusteringGTWR 時間分組
Time grouping unit menu
Time clustering unit menuGTWR 時間分組單位選單
Date format input
Date formatGTWR 日期格式

Step 4Output results輸出結果

● Log output● 文檔輸出

While the tool runs, QGIS shows two main pieces of output:

在 QGIS 執行程序的過程中,主要會輸出兩部份資料:

  1. Scores for different bandwidths – with a fixed bandwidth selected by CV score, the log lists each candidate bandwidth and its score (left figure).不同 Bandwidth 的分數:由於參數選擇 Fixed 的 Bandwidth 並以 CV 分數進行選擇,輸出結果會列出不同的 Bandwidth 及其對應分數(見左圖)。
  2. Model results – basic model information such as AIC, AICc and R², plus the minimum and maximum of each coefficient (right figure). The same content is also saved as a text file in the output folder.模型的結果:可看到模型的基本資訊,例如 AIC、AICc、R Squared 等,以及各係數的最大、最小值(見右圖),此內容會同時輸出為一個文檔至使用者設定的輸出路徑。
Bandwidth scores in the QGIS log
GTWR bandwidth output in QGISGTWR QGIS Bandwidth 輸出
GTWR model summary
GTWR model resultsGTWR 輸出結果

● Data output● 資料輸出

When the tool finishes, a GTWR_result folder is created in the output path (left figure). It contains three parts:

執行程序完畢後,會在使用者指定的路徑中建立 GTWR_result 資料夾(見左圖),內容共有三大部分:

  1. GTWR_result.txt – the model information document (right figure).GTWR_result.txt:模型的資訊文檔(見右圖)。
  2. Model output shapefile – SDF_result.shp, .shx, .dbf and .prj. Load SDF_result.shp into QGIS to use it. After the tool finishes, QGIS also adds an Output layer with the same content.模型輸出結果 Shapefile:包含 SDF_result.shp、.shx、.dbf 與 .prj 四個檔案,可將 SDF_result.shp 匯入 QGIS 使用。程序完成後 QGIS 也會新增一個 Output 圖層,內容與 SDF_result.shp 相同,皆為模型輸出結果。
Model result data
Model output data模型結果資料
Contents of the GTWR_result folder
Contents of the GTWR_result folderGTWR_result 資料夾內容
  1. T-value visualizations for each explanatory variable at each time point. Non-significant areas or points are left uncoloured; significant ones are coloured and shaded by value. If time clustering was used, the time points differ accordingly. Left: without grouping; right: grouped in three-year intervals.各解釋變數 T 值在各時間點的視覺化結果:若該地區或點的檢定結果不顯著,則不會呈現顏色;若顯著,則會依數值大小呈現不同的顏色與深淺。若使用者有進行時間分組,視覺化結果的時間點也會不同。左圖為未進行時間分組的結果,右圖為三年一組的結果。
T-value maps without time grouping
Visualization without grouping未分組視覺化圖片
T-value maps grouped in three-year intervals
Visualization grouped in three-year intervals三年一組視覺化圖片
Interpretation note

The temporal field, time units, sampling intervals, and spatial coordinate system must be prepared consistently. Carefully assess bandwidth choices, data coverage over time, model diagnostics, and sensitivity before interpreting local coefficients. GTWR does not by itself establish causation.

References參考資料

For more information, see the following:

想了解更多,可參考以下資料:

  1. Chan, T. C., Chiang, P. H., Su, M. D., Wang, H. W., & Liu, M. S. Y. (2014). Geographic disparity in chronic obstructive pulmonary disease (COPD) mortality rates among the Taiwan population. PLoS ONE, 9(5), e98170.
  2. Fotheringham, A. S., Crespo, R., & Yao, J. (2015). Geographical and temporal weighted regression (GTWR). Geographical Analysis, 47(4), 431–452.
  3. Nugroho, W. H., & Sumarminingsih, E. (2021, March). Geographically and Temporally Weighted Regression Model with Gaussian Kernel Weighted Function and Bisquare Kernel Weighted Function. In IOP Conference Series: Materials Science and Engineering (Vol. 1115, No. 1, p. 012063). IOP Publishing.
  4. Wu, B., Li, R., & Huang, B. (2014). A geographically and temporally weighted autoregressive model with application to housing prices. International Journal of Geographical Information Science, 28(5), 1186–1204.