【问题标题】:Estimating the intercepts and slopes for three treatments估计三种处理的截距和斜率
【发布时间】:2018-10-19 03:10:37
【问题描述】:

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

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


    【解决方案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 系数是一个共享斜率。

    根据 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
    

    【讨论】:

    • 不要抑制截距并包含交互以允许不同的斜率:y ~ x * treatment
    • 这意味着通过包含截距,您会得到上面列出的截距,然后是 X 的斜率和类别的每个 LEVEL 的系数。因为它们是互斥的类别,所以其他两个的 Beta 值在乘以二进制变量时变为零 您必须将其视为每个类别级别都是一个虚拟列,正确类别为 1,正确类别为 0另外两个。你最终会得到一个 X 系数和一个对 y 截距的类别常数调整。
    • 你可以像我上面做的那样堆叠X变量,它会正常工作。您不能将传统矩阵与类别一起使用,您需要将它们转换为 dummy ,两列分别为 0 和 1 。最好的方法是使用 `lm() 并让 R 管理矩阵数学。
    【解决方案2】:

    创建一个模型框架d(根据最后Note中的数据):

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-28
      • 1970-01-01
      • 2017-12-17
      相关资源
      最近更新 更多