Use substet parameter in lm
function
lm(Y ~ P, data=df, subset=df$P %in% 1:3)
Question
My data looks something like this:
example <- structure(list(ID = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L,
3L), .Label = c("A1", "A2", "A3"), class = "factor"), y = c(44.1160205053166,
33.0574407376116, 50.5295183433918, 44.1160205053166, 33.0574407376116,
50.5295183433918, 44.1160205053166, 33.0574407376116, 50.5295183433918
), day = structure(c(1392647220, 1392733620, 1392820020, 1392647220,
1392733620, 1392820020, 1392647220, 1392733620, 1392820020), class = c("POSIXct",
"POSIXt"), tzone = ""), P = c(16.345885329647, 6.21615618292708,
9.89848991157487, 14.4955473870505, 8.47820783441421, 2.36668747442309,
10.4325918923132, 9.26802998466883, 14.8380589560838), o = c(25.6364896567538,
10.5067015672103, 12.0306829502806, 25.6364896567538, 10.5067015672103,
12.0306829502806, 25.6364896567538, 10.5067015672103, 12.0306829502806
)), .Names = c("ID", "y", "day", "P", "x"), row.names = c(NA,
-9L), class = "data.frame")
I want to ran a regression of Y on P on day 1, 2, and 3. That is
y ~ p[1] + p[2] + p[3] + x
What is the best way of doing this? Do I need to create a new data frame with these variables before running lm
? Or there is a better way?
Thanks!
Solution
Use substet parameter in lm
function
lm(Y ~ P, data=df, subset=df$P %in% 1:3)