如何将lm函数应用于数据子集

时间:2014-04-10 09:08:46

标签: r

我使用下面数据框的子集提取数据。

ns <- 20
ans <- matrix(rep(0,200),nrow=100)
for(k in 1:100)
{ 
x1=rnorm(ns,0,1)
x2=rnorm(ns,5,5)
x3=rnorm(ns,10,5)
U=c(x1,x2,x3)
simdata=data.frame(CD=U,
                   Time=factor(rep(c(1,2,3),each=ns)),
                   treatment=sample(rep(c('Trt','placebo'),ns/2)))
ans[k,]=table(simdata$treatment)
}
#simdata

Y=subset(simdata,Time==1,select=c(CD))
X=c(rep(0,10),rep(1,10))
lm(Y~X) #but its not working.

任何想法如何做到这一点将不胜感激。

1 个答案:

答案 0 :(得分:0)

Y不是这里的向量:

ns <- 20
ans <- matrix(rep(0,200),nrow=100)
for(k in 1:100)
{ 
x1=rnorm(ns,0,1)
x2=rnorm(ns,5,5)
x3=rnorm(ns,10,5)
U=c(x1,x2,x3)
simdata=data.frame(CD=U,
                   Time=factor(rep(c(1,2,3),each=ns)),
                   treatment=sample(rep(c('Trt','placebo'),ns/2)))
ans[k,]=table(simdata$treatment)
}
#simdata

Y=subset(simdata,Time==1,select=c(CD))
X=c(rep(0,10),rep(1,10))
Y1=t(Y)
Y1=Y1[,1]
lm(Y1~X)