On Fri, Oct 3, 2008 at 8:04 AM, eugen pircalabelu
<eugen_pircalabelu at yahoo.com> wrote:> Hi List,
>
>
> I have the following problem: I am using the multilevel package and
make.univ function for available in the package and then xyplot from lattice and
I want to know how could I be able to use the "coefficient" for the
straight line that passes the data ?
>
> Example from help:
>
> library(multilevel)
> data(univbct) #a data set already in univariate or stacked form for job
satisfaction
> TEMP<-univbct[3*1:495,c(22,1:17)] #converting it back to multivariate
form
> TEMP2<-make.univ(x=TEMP,dvs=TEMP[,c(10,13,16)]) #transforming it to
univariate form again
>
>
xyplot(MULTDV~TIME|as.factor(SUBNUM),data=TEMP2,type=c("p","r","g"),col="blue",col.line="black",xlab="Time",ylab="SAT")
# taken from Bliese Paul ? Multilevel Modeling in R
>
> Now I want to be able to identify those SUBNUM that have a downwards trend
like (for eg SUBNUM 7) without picking them by inspecting the plot. Is there
some way how I could access these coefficients for this apparently regression
line?
You'll need to fit the models yourself, and then inspect the
coefficients of the results. Here's one way using the plyr package:
install.packages("plyr")
library(plyr)
df <- TEMP2
names(df) <- tolower(names(df))
# First make a linear model for each subset
mod <- function(df) lm(multdv ~ time, data = df)
models <- dlply(df, .(subnum), failwith(NULL, mod), progress. =
"text")
# List the three models that didn't fit, then remove them
names(Filter(is.null, models))
models <- Filter(function(x) !(is.null(x)), models)
# Pull out coefficients of the models
coefs <- ldply(models, coef)
# Select those models with negative coefficients
subset(coefs, time < 0)
You can learn more about how plyr works (including a ~20 page
introductory paper) at http://had.co.nz/plyr.
Hadley
--
http://had.co.nz/