R中斜率系数的推论

发布于 2024-12-15 07:48:19 字数 1054 浏览 0 评论 0原文

默认情况下lm汇总测试斜率系数等于0。我的问题非常基本。我想知道如何测试斜率系数等于非零值。一种方法是使用confint,但这不提供p值。我还想知道如何使用 lm 进行单方面测试。

ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
group <- gl(2,10,20, labels=c("Ctl","Trt"))
weight <- c(ctl, trt)
lm.D9 <- lm(weight ~ group)
summary(lm.D9)

Call:
lm(formula = weight ~ group)

Residuals:
    Min      1Q  Median      3Q     Max 
-1.0710 -0.4938  0.0685  0.2462  1.3690 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)   5.0320     0.2202  22.850 9.55e-15 ***
groupTrt     -0.3710     0.3114  -1.191    0.249    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

Residual standard error: 0.6964 on 18 degrees of freedom
Multiple R-squared: 0.07308,    Adjusted R-squared: 0.02158 
F-statistic: 1.419 on 1 and 18 DF,  p-value: 0.249 


confint(lm.D9)
              2.5 %    97.5 %
(Intercept)  4.56934 5.4946602
groupTrt    -1.02530 0.2833003

感谢您的时间和精力。

By default lm summary test slope coefficient equal to zero. My question is very basic. I want to know how to test slope coefficient equal to non-zero value. One approach could be to use confint but this does not provide p-value. I also wonder how to do one-sided test with lm.

ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
group <- gl(2,10,20, labels=c("Ctl","Trt"))
weight <- c(ctl, trt)
lm.D9 <- lm(weight ~ group)
summary(lm.D9)

Call:
lm(formula = weight ~ group)

Residuals:
    Min      1Q  Median      3Q     Max 
-1.0710 -0.4938  0.0685  0.2462  1.3690 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)   5.0320     0.2202  22.850 9.55e-15 ***
groupTrt     -0.3710     0.3114  -1.191    0.249    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

Residual standard error: 0.6964 on 18 degrees of freedom
Multiple R-squared: 0.07308,    Adjusted R-squared: 0.02158 
F-statistic: 1.419 on 1 and 18 DF,  p-value: 0.249 


confint(lm.D9)
              2.5 %    97.5 %
(Intercept)  4.56934 5.4946602
groupTrt    -1.02530 0.2833003

Thanks for your time and effort.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

匿名的好友 2024-12-22 07:48:20

除了所有其他好的答案之外,您还可以使用偏移量。分类预测变量有点棘手,因为您需要知道编码。

lm(weight~group+offset(1*(group=="Trt")))

这里的 1* 是不必要的,但放入它是为了强调您正在测试差异为 1 的假设(如果您想测试差异为 d 的假设)代码>,然后使用d*(group=="Trt")

In addition to all the other good answers, you could use an offset. It's a little trickier with categorical predictors, because you need to know the coding.

lm(weight~group+offset(1*(group=="Trt")))

The 1* here is unnecessary but is put in to emphasize that you are testing against the hypothesis that the difference is 1 (if you want to test against a hypothesis of a difference of d, then use d*(group=="Trt")

肤浅与狂妄 2024-12-22 07:48:20

您可以使用 t.test 对您的数据执行此操作。 mu 参数设置组均值差异的假设。 alternative 参数可让您在单侧测试和两侧测试之间进行选择。

t.test(weight~group,var.equal=TRUE)

        Two Sample t-test

data:  weight by group 
t = 1.1913, df = 18, p-value = 0.249
alternative hypothesis: true difference in means is not equal to 0 
95 percent confidence interval:
 -0.2833003  1.0253003 
sample estimates:
mean in group Ctl mean in group Trt 
            5.032             4.661 



t.test(weight~group,var.equal=TRUE,mu=-1)

        Two Sample t-test

data:  weight by group 
t = 4.4022, df = 18, p-value = 0.0003438
alternative hypothesis: true difference in means is not equal to -1 
95 percent confidence interval:
 -0.2833003  1.0253003 
sample estimates:
mean in group Ctl mean in group Trt 
            5.032             4.661

You can use t.test to do this for your data. The mu parameter sets the hypothesis for the difference of group means. The alternative parameter lets you choose between one and two-sided tests.

t.test(weight~group,var.equal=TRUE)

        Two Sample t-test

data:  weight by group 
t = 1.1913, df = 18, p-value = 0.249
alternative hypothesis: true difference in means is not equal to 0 
95 percent confidence interval:
 -0.2833003  1.0253003 
sample estimates:
mean in group Ctl mean in group Trt 
            5.032             4.661 



t.test(weight~group,var.equal=TRUE,mu=-1)

        Two Sample t-test

data:  weight by group 
t = 4.4022, df = 18, p-value = 0.0003438
alternative hypothesis: true difference in means is not equal to -1 
95 percent confidence interval:
 -0.2833003  1.0253003 
sample estimates:
mean in group Ctl mean in group Trt 
            5.032             4.661
枕花眠 2024-12-22 07:48:20

编写您自己的测试代码。您知道估计系数并且知道标准误差。您可以构建自己的测试统计数据。

Code up your own test. You know the estimated coeffiecient and you know the standard error. You could construct your own test stat.

秋日私语 2024-12-22 07:48:19

正如@power所说,你可以用手做。
这是一个示例:

> est <- summary.lm(lm.D9)$coef[2, 1]
> se <- summary.lm(lm.D9)$coef[2, 2]
> df <- summary.lm(lm.D9)$df[2]
> 
> m <- 0
> 2 * abs(pt((est-m)/se, df))
[1] 0.2490232
> 
> m <- 0.2
> 2 * abs(pt((est-m)/se, df))
[1] 0.08332659

您可以通过省略 2* 进行单面测试。

更新

这里是双边和单边概率的示例:

> m <- 0.2
> 
> # two-side probability
> 2 * abs(pt((est-m)/se, df))
[1] 0.08332659
> 
> # one-side, upper (i.e., greater than 0.2)
> pt((est-m)/se, df, lower.tail = FALSE)
[1] 0.9583367
> 
> # one-side, lower (i.e., less than 0.2)
> pt((est-m)/se, df, lower.tail = TRUE)
[1] 0.0416633

请注意,上限概率和下限概率之和恰好为 1。

as @power says, you can do by your hand.
here is an example:

> est <- summary.lm(lm.D9)$coef[2, 1]
> se <- summary.lm(lm.D9)$coef[2, 2]
> df <- summary.lm(lm.D9)$df[2]
> 
> m <- 0
> 2 * abs(pt((est-m)/se, df))
[1] 0.2490232
> 
> m <- 0.2
> 2 * abs(pt((est-m)/se, df))
[1] 0.08332659

and you can do one-side test by omitting 2*.

UPDATES

here is an example of two-side and one-side probability:

> m <- 0.2
> 
> # two-side probability
> 2 * abs(pt((est-m)/se, df))
[1] 0.08332659
> 
> # one-side, upper (i.e., greater than 0.2)
> pt((est-m)/se, df, lower.tail = FALSE)
[1] 0.9583367
> 
> # one-side, lower (i.e., less than 0.2)
> pt((est-m)/se, df, lower.tail = TRUE)
[1] 0.0416633

note that sum of upper and lower probabilities is exactly 1.

扮仙女 2024-12-22 07:48:19

使用 car 包中的 linearHypothesis 函数。例如,您可以使用以下命令检查groupTrt的系数是否等于-1。

linearHypothesis(lm.D9, "groupTrt = -1")

Linear hypothesis test

Hypothesis:
groupTrt = - 1

Model 1: restricted model
Model 2: weight ~ group

  Res.Df     RSS Df Sum of Sq      F  Pr(>F)  
1     19 10.7075                              
2     18  8.7292  1    1.9782 4.0791 0.05856 .
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Use the linearHypothesis function from car package. For instance, you can check if the coefficient of groupTrt equals -1 using.

linearHypothesis(lm.D9, "groupTrt = -1")

Linear hypothesis test

Hypothesis:
groupTrt = - 1

Model 1: restricted model
Model 2: weight ~ group

  Res.Df     RSS Df Sum of Sq      F  Pr(>F)  
1     19 10.7075                              
2     18  8.7292  1    1.9782 4.0791 0.05856 .
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
彻夜缠绵 2024-12-22 07:48:19

smatr 包有一个 slope.test() 函数,您可以通过它使用 OLS。

The smatr package has a slope.test() function with which you can use OLS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文