[R-sig-ME] random effect latent variable model,
Ren-Huai Huang
huangrenhuai at gmail.com
Wed Jan 4 20:02:28 CET 2017
Thank you Jim.
I will look at PROC GLIMMIX in SAS. I choose PROC NLMIXED is because using
general(ll) in the model statement can specifiesa general log likelihood
function.
Attached below is the objective function I have tried in R . What I am not
sure is to join the log likelihood with the random effect as I did in the
last four lines. Let me know if I am wrong
see https://github.com/huangrh/relvm for more details.
Thanks again!
Regards,
Renhuai
# -------------------------------------------------------------------
# negative log likelihood: How to join Likelihood with random effect
nll <- function(par, data = data_tbl, w = weight_tbl) { # Reconstruction of
the parameters from the vector mu <- par[grep("mu",names(par))] # fl <-
par[grep("fl", names(par))] # factor loading lv <-
par[grep("lv",names(par))] # factor variable err <- par[grep("err",
names(par))] # # # Convert to matrix d <- dim(data); nr <- d[1]; nc <- d[2]
data<- as.matrix(data); w <- as.matrix(w) mu <- matrix(mu, nrow = nr, ncol
= nc, byrow = TRUE) fl <- matrix(fl, nrow = nr, ncol = nc, byrow = TRUE) lv
<- matrix(lv, nrow = nr, ncol = nc, byrow = FALSE) err <- matrix(err,nrow =
nr, ncol = nc, byrow = TRUE) # negtive log likelyhood means <- mu + fl * lv
out <- sum(( w * (dnorm(data, mean=means, sd = err, log=TRUE))),na.rm=TRUE)
out <- out + sum(dnorm(lv[,1], mean = 0, sd = 1, log=TRUE), na.rm=TRUE)
(-out) }
#------------------------------------------------------------------------------------
# optimize the nll function set.seed(100)
#
# the data set is available in the Github # <
https://github.com/huangrh/relvm/blob/master/inst/extdata/dat244.csv> file
<- system.file("/extdata/dat4557.csv", package="relvm") dat <-
read.csv(file, na.strings=".") # subset score_tbl <- subset(dat,
select=c(paste0("x",1:7))) weight_tbl<- subset(dat,
select=c(paste0("w",1:7)))
# Init parameters d <- dim(score_tbl); init <- unlist(list(mu = rep(0,
d[2]), fl = rep(1, d[2]), # factor loading lv = rep(1, d[1]), # latent
variable err = rep(1, d[2]))) # fit the function fit <- optim(par = init,
fn = relvm:::nll, gr = NULL, method = "BFGS", control= list(maxit=5200),
data = score_tbl, w = weight_tbl)
On Wed, Jan 4, 2017 at 11:06 AM, Baldwin, Jim -FS <jbaldwin at fs.fed.us>
wrote:
> I think you'd get a better response if you gave a complete (but maybe
> minimal) working example. Also, I suspect you'd be better off if your
> example used PROC GLIMMIX in SAS rather than PROC NLMIXED. And maybe
> showing what you've tried so far would also create more interest to help.
>
> Jim
>
>
> -----Original Message-----
> From: R-sig-mixed-models [mailto:r-sig-mixed-models-bounces at r-project.org]
> On Behalf Of Ren-Huai Huang
> Sent: Tuesday, January 03, 2017 8:57 PM
> To: r-sig-mixed-models at r-project.org
> Subject: Re: [R-sig-ME] random effect latent variable model,
>
> Happy new year,
>
> My question is how to replicate the following sas code in R, Julia or
> other open source languages? Thanks in advance!
>
> proc nlmixed data=input tech=dbldog qpoints=30 noad;
> ...
> model id ~ general(loglik);
> random latent_var ~ normal(0, 1) subject= id;
> ...
> run;
>
> Regards,
>
> Renhuai
>
>
> On Fri, Dec 30, 2016 at 4:24 PM, Ren-Huai Huang <huangrenhuai at gmail.com>
> wrote:
>
> > Re: Random Effect Latent Variable Model
> >
> >
> >
> > Dear mixed model members,
> >
> >
> > I am trying to do a weighted latent variable modeling with random
> > effects in R, which is similar to sas nlmixed general log likelihood
> > over a random effect (sas code attached here
> > <https://huangrh.github.io/relvm/vignettes/sascode.html>). The purpose
> > is to calculate a latent variable from 7 observed variables x1, x2, …
> > , x7 with weightings w1, w2, … , w7 to the corresponding likelihood of
> > the variables (the dataset is attached here
> > <https://github.com/huangrh/relvm/blob/master/inst/extdata/dat244.csv>).
> >
> >
> > Both equations of the weighted log likelihood and the random effect
> > log likelihood are linked here
> > <https://huangrh.github.io/relvm/vignettes/formula.html> (equations 2
> > and 3). I was wondering if the subjective function should be the joint
> > probability of both equations 2 and 3, as shown in equation 4. How to
> > join the equation 2 with the random effect?
> >
> >
> > I tried to optimize the function (link to the R code, lines 36-37
> > <https://github.com/huangrh/relvm/blob/master/R/nll.R>) using optim in
> > R (link to vignette
> > <https://huangrh.github.io/relvm/vignettes/Random_Effect_
> Latent_Variable_Model.html>).
> > But the result is very different from the sas nlmixed mentioned above
> > <https://huangrh.github.io/relvm/vignettes/sascode.html>.
> >
> >
> > Any suggestions are very welcome to help me to do this right in R.
> > Thank you very much in advance.
> >
> >
> >
> > Sincerely,
> >
> > Ren-Huai Huang
> >
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-mixed-models at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models
>
>
>
>
> This electronic message contains information generated by the USDA solely
> for the intended recipients. Any unauthorized interception of this message
> or the use or disclosure of the information it contains may violate the law
> and subject the violator to civil or criminal penalties. If you believe you
> have received this message in error, please notify the sender and delete
> the email immediately.
>
[[alternative HTML version deleted]]
More information about the R-sig-mixed-models
mailing list