【问题标题】:lm options, do regression of each category [duplicate]lm选项,对每个类别进行回归[重复]
【发布时间】:2012-09-21 23:28:16
【问题描述】:

数据:

Y   X   levels
y1  x1  2

...

lm(Y~X,I(levels==1))

I(levels==1) 是指levels==1 下的意思吗?如果没有,只有当levels 等于1 时,我如何才能对YX 进行回归?

【问题讨论】:

    标签: r lm


    【解决方案1】:

    你有lm的参数subset,这里是一个例子。

    x <- rnorm(100)
    y <- rnorm(100, sd=0.1) 
    y[1:50] <- y[1:50] + 3*x[1:50] + 10  # line y = 3x+10
    y[51:100] <- y[51:100] + 8*x[51:100] - 5 # line y = 8x-5
    levels <- rep(1:2, each=50, len=100)
    
    data = data.frame(x=x, y=y, levels=levels)
    
    lm(y ~ x, data=data, subset=levels==1) # regression for the first part
    
    Coefficients:   (Intercept)            x  
                        10.015          2.996
    
    lm(y ~ x, data=data, subset=levels==2) # second part
    
    Coefficients:   (Intercept)            x  
                        -4.986          8.000
    

    您正在将I(levels==1) 隐式传递给lm 内的subset

    【讨论】:

      【解决方案2】:

      看看 nlme 包中的 lmList

      set.seed(12345)
      dataset <- data.frame(x = rnorm(100), y = rnorm(100), levels = gl(2, 50))
      dataset$y <- with(dataset, 
          y + (0.1 + as.numeric(levels)) * x + 5 * as.numeric(levels)
      )
      library(nlme)
      models <- lmList(y ~ x|levels, data = dataset)
      

      输出是 lm 模型的列表,每层一个

      models
      
      Call:
        Model: y ~ x | levels 
         Data: dataset 
      
      Coefficients:
        (Intercept)        x
      1    4.964104 1.227478
      2   10.085231 2.158683
      
      Degrees of freedom: 100 total; 96 residual
      Residual standard error: 1.019202
      

      这里是第一个模型的总结

      summary(models[[1]])
      
      Call:
      lm(formula = form, data = dat, na.action = na.action)
      
      Residuals:
           Min       1Q   Median       3Q      Max 
      -2.16569 -1.04457 -0.00318  0.78667  2.65927 
      
      Coefficients:
                  Estimate Std. Error t value Pr(>|t|)    
      (Intercept)   4.9641     0.1617  30.703  < 2e-16 ***
      x             1.2275     0.1469   8.354 6.47e-11 ***
      ---
      Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 
      
      Residual standard error: 1.128 on 48 degrees of freedom
      Multiple R-squared: 0.5925,     Adjusted R-squared: 0.584 
      F-statistic: 69.78 on 1 and 48 DF,  p-value: 6.469e-11
      

      【讨论】:

        【解决方案3】:

        我不确定。但是这段代码似乎表明你是正确的。

        my.data <- "x y level 
                    1  2 1
                    2  4 2
                    3  4 1
                    4  3 2
                    5  5 1
                    6  5 2
                    7  7 1
                    8  6 2
                    9 10 1
                    10 5 2"
        
        my.data2 <- read.table(textConnection(my.data), header = T)
        my.data2
        
        lm(x ~ y,I(level==1), data=my.data2)
        
        
        
        my.data <- "x y level 
                    1  2 1
                    3  4 1
                    5  5 1
                    7  7 1
                    9 10 1"
        
        my.data2 <- read.table(textConnection(my.data), header = T)
        my.data2
        
        lm(x ~ y, data=my.data2)
        

        【讨论】:

          猜你喜欢
          • 2018-11-19
          • 1970-01-01
          • 2017-07-13
          • 1970-01-01
          • 1970-01-01
          • 2015-10-30
          • 2019-09-06
          • 2011-12-11
          • 2016-03-01
          相关资源
          最近更新 更多