估计三次治疗的截距和斜率

问题描述 投票:0回答:2

让我们假设我的数据看起来像这样(这只是用来说明我的问题的数据):

set.seed(1234)
x=runif(12, 22,28);x
y=runif(12,88,120);y

y1=y[1:4];y1
y2=y[5:8];y2
y3=y[9:12];y3

x1=x[1:4];x1
x2=x[5:8];x2
x3=x[9:12];x3

dat= cbind(y1,x1,y2,x2,y3,x3);dat

            y1       x1        y2       x2        y3       x3
    [1,] 118.72718 27.79948 100.56455 23.59740  95.37221 25.51741
    [2,]  90.46189 26.12509  94.38618 27.96536 103.42347 24.44862
    [3,]  92.48808 25.67335 108.15713 24.08918 108.85686 26.52390
    [4,] 103.06760 25.84996 108.88237 26.37924 103.26130 22.05003

假设(y1,x1)代表治疗1,(y2,x2)代表治疗2,(y3,x3)代表治疗3.我的目标是估计截距和斜率(使用增量设计)。然后,确定假设矩阵(所有斜率相同的零假设)。

我以为我可以使用指标。如果治疗则为x1 = 1,否则为0。如果处理2则x2 = 1,否则为0。否则,这是指第三种治疗。

这是我想出的:

    X< as.matrix(cbind(rep(1,12),c(rep(1,4),rep(0,8)),c(rep(0,4),rep(1,4),rep(0,4))))

无论如何,我不知道从哪里开始。我想知道是否有人会给我一些开始。请注意,所有符号都应采用矩阵形式。谢谢!

编辑:我试图用矩阵表示法,这是我到目前为止所得到的,虽然我有一些系数作为NA:

set.seed(1234)
x=runif(12, 22,28);x
y=runif(12,88,120);y

y1=y[1:4];y1
y2=y[5:8];y2
y3=y[9:12];y3

x1=x[1:4];x1
x2=x[5:8];x2
x3=x[9:12];x3

dat= cbind(y1,x1,y2,x2,y3,x3);dat



 X1<- cbind( dat[,2],rep(0,4),rep(0,4), dat[,2],rep(1,4),rep(1,4));X1
 X2 <- cbind(dat[,4],rep(1,4),dat[,4], rep(0,4),rep(1,4),rep(1,4));X2
 X3 <- cbind(dat[,6],rep(1,4),dat[,6], rep(0,4),rep(0,4),rep(0,4));X3

 X <- rbind(X1,X2,X3)

 model <- lm(formula = y ~ X);summary(model)
r regression anova dummy-variable
2个回答
1
投票

如果你改变第二部分,你可以通过将y和x值与第三个“分类”变量绑定来完成你想要做的事情,该变量告诉试验它属于哪个。

然后将它们堆叠在一个三列数据框中,标记列以保持变量的直线并将行绑定在一起。

确保你的号码没有被分类为绑定(就像我的那样)。

然后你可以做一个线性回归

one<- cbind(y1,x1,'one')
two<- cbind(y2, x2, 'two')
three<- cbind(y3, x3,'three')

dat<- data.frame(rbind(one, two, three))
colnames(dat)<- c('y', 'x', 'treatment')
dat$y<-as.numeric(dat$y) #binding tweaked numerics to categories
dat$x<-as.numeric(dat$x) #I converted them back
model <- lm(formula = y ~ 0 + x + treatment, data = dat)

然后你的输出如下:

Call:
lm(formula = y ~ 0 + x + treatment, data = dat)

Residuals:
    Min      1Q  Median      3Q     Max 
-4.4579 -2.2156 -0.4115  1.5442  6.6039 

Coefficients:
               Estimate Std. Error t value Pr(>|t|)  
x               -0.1461     0.4283  -0.341   0.7418  
treatmentone     7.9185     3.9775   1.991   0.0817 .
treatmentthree   8.0112     2.5156   3.185   0.0129 *
treatmenttwo     6.4185     3.9775   1.614   0.1453  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 4.04 on 8 degrees of freedom
Multiple R-squared:  0.7991,    Adjusted R-squared:  0.6986 
F-statistic: 7.954 on 4 and 8 DF,  p-value: 0.006839

通过将公式拼写为零,您可以获得x加上每个系数的效果。

每个β是治疗的截距调整,x系数是共享斜率。

编辑符合Roland的建议,这将为您提供单独的斜坡。

Call:
lm(formula = y ~ x + treatment, data = dat)

Residuals:
    Min      1Q  Median      3Q     Max 
-4.4579 -2.2156 -0.4115  1.5442  6.6039 

Coefficients:
               Estimate Std. Error t value Pr(>|t|)  
(Intercept)      7.9185     3.9775   1.991   0.0817 .
x               -0.1461     0.4283  -0.341   0.7418  
treatmentthree   0.0927     3.4463   0.027   0.9792  
treatmenttwo    -1.5000     2.8570  -0.525   0.6138  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 4.04 on 8 degrees of freedom
Multiple R-squared:  0.08671,   Adjusted R-squared:  -0.2558 
F-statistic: 0.2532 on 3 and 8 DF,  p-value: 0.857

0
投票

创建一个模型框架d(从最后的注释中的数据):

d <- with(DF, data.frame(y = c(y1, y2, y3), x = c(x1, x2, x3), g = gl(3, 4)))

然后针对具有相同斜率的单独截距和斜率与单独的截距运行回归。最后使用anova进行测试。

fm1 <- lm(y ~ g + x:g + 0, d) # separate intercepts & slopes
coef(fm1)
##           g1           g2           g3         g1:x         g2:x         g3:x 
## -201.0983992  141.2889141   94.7617449   11.4666919   -1.5011629    0.3233902 

fm2 <- lm(y ~ g + x + 0, d) # separate intercepts, same slope
coef(fm2)
##         g1         g2         g3          x 
## 83.5468017 85.9297193 86.2446353  0.6691224 

anova(fm1, fm2)

赠送:

Analysis of Variance Table

Model 1: y ~ g + x/g + 0
Model 2: y ~ g + x + 0
  Res.Df    RSS Df Sum of Sq      F Pr(>F)  
1      6 330.52                             
2      8 723.85 -2   -393.33 3.5701 0.0952 .
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

所以p = 0.0952并且两个模型没有显着差异,即斜率在5%水平上彼此没有显着差异。

注意

假设的输入是:

Lines <- "
       y1       x1        y2       x2        y3       x3
118.72718 27.79948 100.56455 23.59740  95.37221 25.51741
 90.46189 26.12509  94.38618 27.96536 103.42347 24.44862
 92.48808 25.67335 108.15713 24.08918 108.85686 26.52390
103.06760 25.84996 108.88237 26.37924 103.26130 22.05003"
DF <- read.table(text = Lines, header = TRUE)
© www.soinside.com 2019 - 2024. All rights reserved.