估算三种处理的截距和斜率

时间:2018-10-19 03:10:38

标签: r regression anova dummy-variable

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

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)

2 个答案:

答案 0 :(得分: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的影响以及每个系数的影响。

每个beta都是针对x系数为共享斜率的处理的截距调整。

根据罗兰的建议进行编辑,这将为您提供个性化的坡度。

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

答案 1 :(得分: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

so 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)
相关问题