首页 > 解决方案 > 如何测试我的因变量是否从一年到另一年增加或减少?

问题描述

我有一个名为 的数据集data,其中包含关于contaminant我的因变量的数据。一年中的所有观察都独立于接下来的几年。我的预测指标是Species(三个级别)和Year(三个级别):基本上,我需要分别查看每个物种的污染物数据是否随着时间的推移而增加/减少。

到目前为止,我尝试了这段代码

    model1<- lm(contaminant~Species*Year,data=data)

            #using Year as numerical (covariate): Indeed, I do not care about 
            the difference in contaminants load among species in each year. 
            I simply want to test if the slopes of each species are significant.

第一个问题:我通过将年份视为数字来正确地做到这一点?还是有另一种特定的方式/代码来处理时间序列?我实际上想要一个 p 值,它告诉我下图中的 Series1(由每组的平均值制成)随着时间的推移显着增加。 在此处输入图像描述

我的摘要输出如下所示:

Call: lm(formula = Contaminant ~ Species * Year, data = data)

Residuals:
    Min      1Q  Median      3Q     Max 
-5.1135 -1.3595 -0.1475  1.3225  7.3652 

Coefficients:
                Estimate Std. Error t value Pr(>|t|)    
(Intercept)    -588.6625   996.6024  -0.591 0.556036    
Species2       -823.3590  1320.9209  -0.623 0.534451    
Species3      -4798.0032  1393.0990  -3.444 0.000830 ***
Year              0.2930     0.4941   0.593 0.554484    
Species2:Year     0.4092     0.6549   0.625 0.533462    
Species3:Year     2.3802     0.6907   3.446 0.000824 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 2.21 on 103 degrees of freedom
Multiple R-squared:  0.3853,    Adjusted R-squared:  0.3555 
F-statistic: 12.91 on 5 and 103 DF,  p-value: 9.428e-10

第二个问题:为什么我的摘要输出只给了我两个交互?为什么它也不提供 Species1:Year?

第三个问题:有人知道如何在 r 中制作这样的图表吗?到目前为止,我只能使用每组的平均值在 excel 中做到这一点

谢谢

标签: rtime-series

解决方案


1) 如果您的观察只包括整年而不包括完整日期,则可以将年份添加为变量。只要您不将其转换为因子,它就会假定每年持续增长。

2) Dummy - 或 one-hot - 编码总是测试组与基线组的差异。这意味着 S2 测试 S2-S1,S3 测试 S3-S1。这同样适用于交互项。

3)有多种可能,但会超过1-2行代码。参见例如http://www.cookbook-r.com/Graphs/Bar_and_line_graphs_(ggplot2)/#line-graphs-1


推荐阅读