简体   繁体   English

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

[英]How do I apply the lm function to subset of data

I have extracted data using the subset for the data frame below. 我已经使用下面的数据框的子集提取了数据。

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.

Any idea how to do this will be appreciated. 任何想法如何做到这一点将不胜感激。

Y is not a vector here: 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM