2012-10-01 14 views
1

數據的迴歸:LM選項,請在每個類別

Y X levels 
y1 x1 2 

...

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

是否I(levels==1)levels==1下是什麼意思?如果不是,只有當levels等於1時,我該如何做YX的迴歸?

回答

1

你有參數的lmsubset,這裏就是一個例子。

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)subsetlm

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) 
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