Running a regression on a subset of observations using R -
let's have 2 variables a:{1,2,3,4,5,6,7,8,9,10} , b:{11,12,13,14,15,16,17,18,19,20} , want run regression in r, using observations have a>6, i.e. run regression using {7,8,9,10} , {17, 18,19,20}.
in stata easy it: reg b if a>6, in r cannot find easy way (i use lm command).
please notice new in r , can use vanilla r, not allowed install package. in advance.
it's best make sure variables stored in same object , best that object data frame. way can more extend multiple regression , if reason reorder data reorganization extend variables. when subset, extend variables.
so answer question:
df = data.frame(a = c(1:10), b = c(11:20)) lm(a ~ b, data = df[df$a>6,])
or using subset
function:
lm(a ~ b, data = subset(df, > 6))
Comments
Post a Comment