You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R中对CSV多测点位移数据进行线性/多项式回归?

在R中处理干涉测量数据的回归分析

1. 数据导入与初步检查

首先导入CSV文件并确认数据结构:

# 导入数据
data <- read.csv("your_interferometry_data.csv")

# 查看数据结构,确认日期列和位移列的格式
str(data)

如果日期列是字符型,需转换为日期格式:

data$Date <- as.Date(data$Date)

2. 数据格式转换(宽转长)

你的数据是宽格式(每个测点占单独一列),建议转换为长格式,方便批量处理每个测点的回归分析:

library(tidyr)
data_long <- pivot_longer(
  data,
  cols = starts_with("Point"),  # 选中所有以Point开头的列
  names_to = "Point",           # 新列:测点名称
  values_to = "Displacement"    # 新列:位移数据
)

3. 线性回归分析

单个测点的线性回归

以Point1为例,拟合位移随日期变化的线性模型:

# 拟合模型
lm_model <- lm(Displacement ~ Date, data = subset(data_long, Point == "Point1"))

# 查看模型系数、显著性等结果
summary(lm_model)

批量处理所有测点的线性回归

用purrr包批量为每个测点拟合模型:

library(purrr)
library(dplyr)

# 按测点分组,逐个拟合线性模型
lm_models <- data_long %>%
  group_split(Point) %>%
  map(~ lm(Displacement ~ Date, data = .x))

# 查看每个模型的详细结果
map(lm_models, summary)

4. 多项式回归分析

如果位移随时间呈非线性变化,可拟合多项式模型,以二次多项式为例:

方式1:正交多项式(推荐,避免变量共线性)

# 单个测点二次多项式回归
poly_model <- lm(Displacement ~ poly(Date, 2), data = subset(data_long, Point == "Point1"))
summary(poly_model)

# 批量处理所有测点的二次多项式回归
poly_models <- data_long %>%
  group_split(Point) %>%
  map(~ lm(Displacement ~ poly(Date, 2), data = .x))

方式2:直接使用变量幂次

# 单个测点二次多项式回归
poly_model2 <- lm(Displacement ~ Date + I(Date^2), data = subset(data_long, Point == "Point1"))
summary(poly_model2)

若需要更高次多项式,只需修改poly(Date, n)中的n值,或添加I(Date^3)等项即可。

5. 是否需要添加坐标列?

  • 若仅关注单个测点随时间的位移趋势,不需要坐标列,每个测点的时间序列是独立分析对象。
  • 若需要结合空间位置研究位移规律(比如分析位移的空间分布、建立包含空间变量的回归模型),则需要添加坐标列。你需要准备包含Point、X、Y(或经纬度)的坐标表,合并到数据中后建模:
# 假设坐标表为coords.csv,包含Point、X、Y列
coords <- read.csv("coords.csv")
data_with_coords <- merge(data_long, coords, by = "Point")

# 拟合包含空间坐标的模型示例
spatial_model <- lm(Displacement ~ Date + X + Y, data = data_with_coords)
summary(spatial_model)

内容的提问来源于stack exchange,提问作者stef

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 23:25:20