Answers:
set.seed(20); y = rnorm(20); x = y + rnorm(20, 0, 0.2) # generate correlated data
summary(lm(y ~ x))                  # original model
summary(lm(y ~ x, offset= 1.00*x))  # testing against slope=1
summary(lm(y-x ~ x))                # testing against slope=1
输出:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  0.01532    0.04728   0.324     0.75    
x            0.91424    0.04128  22.148 1.64e-14 ***
            Estimate Std. Error t value Pr(>|t|)  
(Intercept)  0.01532    0.04728   0.324   0.7497  
x           -0.08576    0.04128  -2.078   0.0523 .
            Estimate Std. Error t value Pr(>|t|)  
(Intercept)  0.01532    0.04728   0.324   0.7497  
x           -0.08576    0.04128  -2.078   0.0523 .
对于这些类型的假设,您可以使用linearHypothesispackage car中的 function :
set.seed(20); y = rnorm(20); x = y + rnorm(20, 0, 0.2) # generate correlated data
mod <- lm(y ~ x))                  # original model
> linearHypothesis(mod,matrix(c(0,1),nrow=1),rhs=c(1))
Linear hypothesis test
Hypothesis:
x = 1
Model 1: restricted model
Model 2: y ~ x
  Res.Df     RSS Df Sum of Sq      F  Pr(>F)  
1     19 0.96022                              
2     18 0.77450  1   0.18572 4.3162 0.05234 .
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 
测试的重点是您要拒绝原假设,而不是对其进行确认。没有显着差异的事实,决不能证明不存在显着差异。为此,您必须定义您认为合理的效果大小以拒绝null。
set.seed(20); y = rnorm(20); x = y + rnorm(20, 0, 0.2)
model <- lm(y~x)
coefx <- coef(summary(model))[2,1]
seslope <- coef(summary(model))[2,2]
DF <- model$df.residual
# normal test
p <- (1 - pt(coefx/seslope,DF) )*2
# test whether different from 1
p2 <- (1 - pt(abs(coefx-1)/seslope,DF) )*2
现在,您应该知道一个事实,即差异变得显着的效果大小是
> qt(0.975,DF)*seslope
[1] 0.08672358
假设我们对斜率的标准误差有一个合理的估计。因此,如果您决定仅应从0.1处检测出显着差异,则可以按以下方式计算必要的DF:
optimize(
    function(x)abs(qt(0.975,x)*seslope - 0.1),
    interval=c(5,500)
) 
$minimum
[1] 6.2593
请注意,这很大程度上取决于边坡的估计。为了获得对边坡的更好估计,您可以对数据进行重新采样。天真的方法是:
n <- length(y)
seslope2 <-
  mean(
    replicate(n,{
      id <- sample(seq.int(n),1)
      model <- lm(y[-id]~x[-id])
      coef(summary(model))[2,2]
    })
  )
将seslope2放入优化函数中,返回:
$minimum
[1] 6.954609
所有这一切将告诉您,数据集将以比您认为必要的速度更快的速度返回重要结果,并且如果您要确保不重要意味着您想要什么,则只需要7个自由度(在这种情况下为9个观察值)。手段。