数据周期性与自相关性分析
立即解锁
发布时间: 2025-09-07 01:59:21 阅读量: 11 订阅数: 47 AIGC 


数据清洗的艺术与实践
### 数据周期性与自相关性分析
#### 1. 引言
在数据分析中,我们常常期望数据具有周期性。当顺序数据中存在多个重叠的周期性时,与周期性模式的偏差可能比原始值更具信息价值,这在时间序列数据中尤为常见。本文将通过分析多年前收集的美国科罗拉多州一所房子内外的温度数据,探讨如何识别和分析数据中的周期性,并进行数据去趋势化处理。
#### 2. 数据加载与初步查看
首先,使用Python函数读取数据到Pandas DataFrame,后续分析和可视化将在R及其Tidyverse中进行。
```python
thermo = read_glarp()
start, end = thermo.timestamp.min(), thermo.timestamp.max()
print("Start:", start)
print(" End:", end)
# Fencepost counting includes ends
print(" Days:", 1 + (end.date() - start.date()).days)
```
输出结果显示数据记录从2003 - 07 - 25 16:04:00开始,到2004 - 07 - 16 15:28:00结束,共358天。
接着查看数据集的几行,发现记录间隔为每3分钟一行,且无缺失值,原始数据中的一些明显记录错误已通过插补值清理。
```R
%%R -i thermo
glarp <- as.tibble(thermo)
glarp
```
数据集包含171,349行5列,部分数据如下:
| timestamp | basement | lab | livingroom | outside |
|---------------------|----------|-----|------------|---------|
| 2003 - 07 - 25 16:04:00 | 24 | 25.2 | 29.8 | 27.5 |
| 2003 - 07 - 25 16:07:00 | 24 | 25.2 | 29.8 | 27.3 |
| 2003 - 07 - 25 16:10:00 | 24 | 25.2 | 29.8 | 27.3 |
| 2003 - 07 - 25 16:13:00 | 24.1 | 25.2 | 29.8 | 27.4 |
| 2003 - 07 - 25 16:16:00 | 24.1 | 25.2 | 29.8 | 27.8 |
#### 3. 数据可视化与去趋势化
为了关注个体测量值与预期的差异,我们先对数据进行可视化并去趋势化。
##### 3.1 室外温度可视化
使用ggplot2绘制室外温度随记录时间的变化图。
```R
%%R
ggplot(glarp, aes(x=timestamp, y=outside)) +
geom_line() + clean_theme +
ggtitle("Outside temperature over recording interval")
```
从图中可以看出,北半球温度一般7月比1月暖和,整体趋势中有很多波动。即使只有一年的数据,根据基本领域知识,我们可以预期其他年份也有类似的年度周期。
##### 3.2 地下室温度可视化
同样使用ggplot2绘制地下室温度随记录时间的变化图。
```R
%%R
ggplot(glarp, aes(x=timestamp, y=basement)) +
geom_line() + clean_theme +
ggtitle("Basement temperature over recording interval")
```
地下室温度相对较窄,大致在14°C到23°C之间。部分夏季高温和冬季低温超出此范围,2003年9月和10月左右可能有供暖系统的变化。
#### 4. 基于领域知识的趋势分析
##### 4.1 室外温度年度去趋势化
考虑到室外温度受季节影响,我们用二次多项式拟合数据,去除年度趋势。
```R
%%R
# Model the data as a second order polynomial
year.model <- lm(outside ~ poly(timestamp, 2), data = glarp)
# Display the regression and
```
0
0
复制全文
相关推荐










