lm选项,对每个类别进行回归

时间:2012-10-01 05:08:41

标签: r lm

数据:

Y   X   levels
y1  x1  2

...

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

I(levels==1)是否在levels==1下?如果没有,只有在Y等于X时才能对levels1进行回归?

3 个答案:

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

答案 1 :(得分:1)

您拥有subset的参数lm,这是一个示例。

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)隐式传递给subset内的lm

答案 2 :(得分:0)

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

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)