Dear Jarrett,
Your observed-variable covariance matrix contains a variable,
Prev.Year.Herbivore.Abundance, that doesn't appear in the model. I agree
that the error message that this produces is cryptic and will think about
how to improve that, or to allow the observed-variable covariance matrix to
contain variables not used in the model.
Beyond that, the structure of the model seems odd to me: (1) The model
implies that the exogenous variables (Prev.Year.Predatory.Invert.Abundance,
Prev.Year.Predatory.Fish.Abundance, and Mean.Temp.C, if I read the model
right) are uncorrelated with each other. You could specify covariances among
these variables, or, more simply, use the fixed.x argument to sem().(2)
Errors for similar endogenous variables in different years are uncorrelated
(e.g., for Prev.Year.Kelp.Abundance, Kelp.Abundance), and two variables
taken to be exogenous are uncorrelated with the errors of the same variables
in the subsequent year (Prev.Year.Predatory.Invert.Abundance,
Prev.Year.Predatory.Fish.Abundance). (3) Again if I read the structure of
the model right, it seems to be recursive, in which case least-squares
regression would give you the same estimates as the FIML estimates provided
by sem().
I hope this helps,
John
--------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario
Canada L8S 4M4
905-525-9140x23604
http://socserv.mcmaster.ca/jfox
--------------------------------
> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Jarrett Byrnes
> Sent: Monday, November 20, 2006 12:32 AM
> To: R-help at stat.math.ethz.ch
> Subject: [R] sem package subscript out of bounds error
>
> I'm having the most curious error while using the sem
> package. For the model I'm working with, I keep getting the
> following error:
>
> Error in J[cbind(1:n, observed)] <- 1 : subscript out of bounds
>
>
> I''ve used debug=TRUE with sem, and there don't appear to be
any
> problems with model - there are no latent variables in this model.
> The variables in the covariance matrix are all in the model.
> I've tried this with simpler version of the below model, and
> keep getting the same error.
>
> What does this error mean?
>
> Below is the full code, but I've tried this with all sorts of
> subsets of the model, making sure to keep all of the proper
> error terms.
>
> ---
>
> fish.invert.data<-data.frame(Prev.Year.Predatory.Invert.Abundance,
>
> Prev.Year.Predatory.Fish.Abundance,
>
> Predatory.Invert.Abundance,
>
> Predatory.Fish.Abundance,
>
> Prev.Year.Herbivorous.Invert.Abundance,
>
> Prev.Year.Herbivorous.Fish.Abundance,
>
> Herbivorous.Invert.Abundance,
>
> Herbivorous.Fish.Abundance,
> Kelp.Abundance,
>
> Prev.Year.Kelp.Abundance,
> Mean.Temp.C,
>
> Prev.Year.Herbivore.Abundance)
>
>
> #na.rm=TRUE to deal with a few rows of missing data
> fish.invert.covar<-var(fish.invert.data, na.rm=TRUE)
>
> fish.invert.model<-specify.model()
> Prev.Year.Predatory.Invert.Abundance ->
> Prev.Year.Herbivorous.Invert.Abundance, pred.invert.top.down,
> NA Predatory.Invert.Abundance ->
> Herbivorous.Invert.Abundance, pred.invert.top.down, NA
> Prev.Year.Predatory.Fish.Abundance ->
> Prev.Year.Herbivorous.Fish.Abundance, pred.fish.top.down, NA
> Predatory.Fish.Abundance -> Herbivorous.Fish.Abundance,
> pred.fish.top.down, NA Prev.Year.Predatory.Fish.Abundance ->
> Prev.Year.Herbivorous.Invert.Abundance,
> pred.fish.invert.top.down, NA Predatory.Fish.Abundance ->
> Herbivorous.Invert.Abundance, pred.fish.invert.top.down, NA
> Prev.Year.Herbivorous.Fish.Abundance ->
> Prev.Year.Kelp.Abundance, top.down.herb.fish, NA
> Prev.Year.Herbivorous.Invert.Abundance ->
> Prev.Year.Kelp.Abundance, top.down.herb.invert, NA
> Herbivorous.Fish.Abundance -> Kelp.Abundance,
> top.down.herb.fish, NA Herbivorous.Invert.Abundance ->
> Kelp.Abundance, top.down.herb.invert, NA
> Prev.Year.Kelp.Abundance -> Herbivorous.Fish.Abundance,
> bottom.up.kelp.fish, NA Prev.Year.Kelp.Abundance ->
> Herbivorous.Invert.Abundance, bottom.up.kelp.inverts, NA
> Prev.Year.Herbivorous.Fish.Abundance ->
> Predatory.Fish.Abundance, bottom.up.herb.fish, NA
> Prev.Year.Herbivorous.Invert.Abundance ->
> Predatory.Invert.Abundance, bottom.up.herb.invert, NA
> Prev.Year.Herbivorous.Invert.Abundance ->
> Predatory.Fish.Abundance, bottom.up.herb.invert.fish, NA
> Mean.Temp.C -> Kelp.Abundance, mean.nutrients.from.temp, NA
> Prev.Year.Predatory.Invert.Abundance ->
> Predatory.Invert.Abundance, prev.year.pred.invert, NA
> Prev.Year.Predatory.Fish.Abundance ->
> Predatory.Fish.Abundance, prev.year.pred.fish, NA
> Prev.Year.Herbivorous.Fish.Abundance ->
> Herbivorous.Fish.Abundance, prev.year.herb.fish, NA
> Prev.Year.Herbivorous.Invert.Abundance ->
> Herbivorous.Invert.Abundance, prev.year.herb.invert, NA
> Prev.Year.Kelp.Abundance -> Kelp.Abundance, prev.year.kelp,
> NA Kelp.Abundance <-> Kelp.Abundance, kelp.abundance.error,
> NA Prev.Year.Kelp.Abundance<-> Prev.Year.Kelp.Abundance,
> prev.year.kelp.error, NA Mean.Temp.C <-> Mean.Temp.C,
> mean.temp.error, NA Prev.Year.Herbivorous.Fish.Abundance <->
> Prev.Year.Herbivorous.Fish.Abundance, prev.herb.fish.error,
> NA Prev.Year.Herbivorous.Invert.Abundance <->
> Prev.Year.Herbivorous.Invert.Abundance,
> prev.herb.invert.error, NA Prev.Year.Predatory.Fish.Abundance
> <-> Prev.Year.Predatory.Fish.Abundance, prev.pred.fish.error,
> NA Prev.Year.Predatory.Invert.Abundance <->
> Prev.Year.Predatory.Invert.Abundance, prev.pred.invert.error,
> NA Herbivorous.Fish.Abundance <->
> Herbivorous.Fish.Abundance,herb.fish.error, NA
> Herbivorous.Invert.Abundance <->
> Herbivorous.Invert.Abundance, herb.invert.error, NA
> Predatory.Fish.Abundance <-> Predatory.Fish.Abundance,
> pred.fish.error, NA Predatory.Invert.Abundance <->
> Predatory.Invert.Abundance, pred.invert.error, NA
>
> fish.invert.model
>
> fish.invert.sem<-sem(fish.invert.model, fish.invert.covar, N=166,
> debug=TRUE)
>
>
> Yields
>
> observed variables:
> [1] "1:Prev.Year.Predatory.Invert.Abundance"
> "2:Prev.Year.Predatory.Fish.Abundance"
> "3:Predatory.Invert.Abundance"
> [4] "4:Predatory.Fish.Abundance"
> "5:Prev.Year.Herbivorous.Invert.Abundance"
> "6:Prev.Year.Herbivorous.Fish.Abundance"
> [7] "7:Herbivorous.Invert.Abundance"
> "8:Herbivorous.Fish.Abundance"
"9:Kelp.Abundance"
> [10] "10:Prev.Year.Kelp.Abundance"
> "11:Mean.Temp.C"
> "12:Prev.Year.Herbivore.Abundance"
>
>
> parameters:
> [1] "1:pred.invert.top.down"
"2:pred.fish.top.down"
> "3:pred.fish.invert.top.down" "4:top.down.herb.fish"
> [5] "5:top.down.herb.invert"
"6:bottom.up.kelp.fish"
> "7:bottom.up.kelp.inverts" "8:bottom.up.herb.fish"
> [9] "9:bottom.up.herb.invert"
"10:bottom.up.herb.invert.fish"
> "11:mean.nutrients.from.temp"
"12:prev.year.pred.invert"
> [13] "13:prev.year.pred.fish"
"14:prev.year.herb.fish"
> "15:prev.year.herb.invert" "16:prev.year.kelp"
> [17] "17:kelp.abundance.error"
"18:prev.year.kelp.error"
> "19:mean.temp.error"
"20:prev.herb.fish.error"
> [21] "21:prev.herb.invert.error"
"22:prev.pred.fish.error"
> "23:prev.pred.invert.error" "24:herb.fish.error"
> [25] "25:herb.invert.error"
"26:pred.fish.error"
> "27:pred.invert.error"
>
>
> RAM:
> heads to from parameter start
> [1,] 1 5 1 1 NA
> [2,] 1 7 3 1 NA
> [3,] 1 6 2 2 NA
> [4,] 1 8 4 2 NA
> [5,] 1 5 2 3 NA
> [6,] 1 7 4 3 NA
> [7,] 1 10 6 4 NA
> [8,] 1 10 5 5 NA
> [9,] 1 9 8 4 NA
> [10,] 1 9 7 5 NA
> [11,] 1 8 10 6 NA
> [12,] 1 7 10 7 NA
> [13,] 1 4 6 8 NA
> [14,] 1 3 5 9 NA
> [15,] 1 4 5 10 NA
> [16,] 1 9 11 11 NA
> [17,] 1 3 1 12 NA
> [18,] 1 4 2 13 NA
> [19,] 1 8 6 14 NA
> [20,] 1 7 5 15 NA
> [21,] 1 9 10 16 NA
> [22,] 2 9 9 17 NA
> [23,] 2 10 10 18 NA
> [24,] 2 11 11 19 NA
> [25,] 2 6 6 20 NA
> [26,] 2 5 5 21 NA
> [27,] 2 2 2 22 NA
> [28,] 2 1 1 23 NA
> [29,] 2 8 8 24 NA
> [30,] 2 7 7 25 NA
> [31,] 2 4 4 26 NA
> [32,] 2 3 3 27 NA
> Error in J[cbind(1:n, observed)] <- 1 : subscript out of bounds
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.