jslefche / piecewiseSEM

Piecewise Structural Equation Modeling in R
155 stars 48 forks source link

Errors using a random effect #182

Closed jlamit closed 4 years ago

jlamit commented 5 years ago

Hi, I am receiving some errors that I don't quite understand. The data involves bark traits, and lichen data collected on trees in a common garden. There are 54 trees in the dataset, each tree belongs to a single tree genotype, there are 13 tree genotypes total, and each genotype is represented by 2-6 trees. On each tree the following variables were quantified: total lichen cover (TLCov) in quadrats on tree trunks, bark texture roughness on the tree trunks (BR), and condensed tannins of the bark of each tree trunk. All the three measured variables vary significantly among tree genotypes (done using a simple model with genotype as a random effect and a single trait as a response variable, in lmerTest). What I am interested in using SEM for is testing if condensed tannins or roughness are genetically based mechanisms that cause lichen cover to vary among tree genotypes. So, the simple SEM would have a direct path from tree genotype to condensed tannins and from tree genotype to bark roughness, and then genotype, condensed tannins and roughness would have paths leading to lichen cover. This seems like a relatively simple model to run but I get error messages when I fit it with lmerTest and with nlme. Below is the output from running the models using both programs. I am not certain that I understand the error messages entirely, or at least the reason for them. Any suggestions on where to go from here would be greatly appreciated. Thanks.

###################################

Below is the model set fit with lmerTest, which reveals some issues. First, it seems to think there are NAs in the dataset but there are no missing values in the input data. Second, output from looking at the Model_lmerTest object shows that no model actually has tree Genotype as an effect, and the data says 244 rows and a bunch of NAs, neither of which reflect the input dataset. Third, running summary and coefs both say there is an incorrect number of dimensions.

SEM 1 with lmerTest

Model_lmerTest<-psem(

  • lmer(BR~(1|Geno),data=RawData),
  • lmer(CT~(1|Geno),data=RawData),
  • lmer(TLCov~BR+CT+(1|Geno),data=RawData)) Warning message: NAs detected in the dataset. Models will run but this is not recommended

Model_lmerTest
Structural Equations: lmerModLmerTest: BR ~ 1 lmerModLmerTest: CT ~ 1 lmerModLmerTest: TLCov ~ BR + CT

Data: Geno BR CT TLCov 1 10 5.5 2.66 3.25 2 10 5.5 3.04 NA 3 10 5.5 3.07 NA 4 10 5.5 3.38 NA 5 10 10.0 2.66 NA 6 10 10.0 3.04 NA ...with 244 more rows

[1] "class(psem)"

summary(Model_lmerTest) |====================================================================================================================================================================================| 100% Error in newdata[, notrans[k]] : incorrect number of dimensions rsquared(Model_lmerTest, method = NULL) Response family link method Marginal Conditional 1 BR gaussian identity none 0.0000000 0.33483307 2 CT gaussian identity none 0.0000000 0.09397688 3 TLCov gaussian identity none 0.1261363 0.21848890 coefs(Model_lmerTest) Error in newdata[, notrans[k]] : incorrect number of dimensions

###################################

Below is the model set fit with nlme, which also reveals some issues. First, output from looking at the Model_nlme object shows that no model actually has tree Genotype as an effect, but unlike the lmerTest output (above) the row number is correct and there are no NAs indicated. Running summary and coefs both say there is less than 2 dimensions. The rsquared function has the same output as that for the lmerTest model set.

Model_nlme <- psem(

  • lme(BR ~1, random = ~1|Geno, data=RawData),
  • lme(CT~1, random = ~1|Geno, data=RawData),
  • lme(TLCov~ BR +CT, random = ~1|Geno, data=RawData))

Model_nlme
Structural Equations: lme: BR ~ 1 lme: CT ~ 1 lme: TLCov ~ BR + CT

Data: Tree.ID Geno BR TLCov CT 1 N1.04 1017 75.0 15.13 2.77 2 N1.07 T6 40.0 5.63 1.93 3 N1.08 1017 37.5 3.05 2.07 4 N1.10 996 17.5 2.25 2.43 5 N1.11 T6 85.0 25.03 2.46 6 N1.24 1017 55.0 32.25 1.75 ...with 54 more rows

[1] "class(psem)"

summary(Model_nlme) |====================================================================================================================================================================================| 100% Error in colnames<-(*tmp*, value = notrans) : attempt to set 'colnames' on an object with less than two dimensions rsquared(Model_nlme, method = NULL) Response family link method Marginal Conditional 1 BR gaussian identity none 0.0000000 0.33483306 2 CT gaussian identity none 0.0000000 0.09397688 3 TLCov gaussian identity none 0.1261362 0.21848891 coefs(Model_nlme) Error in colnames<-(*tmp*, value = notrans) : attempt to set 'colnames' on an object with less than two dimensions

jslefche commented 5 years ago

Hi, it’s a bit hard to tell what’s happening here. It could be pbkrtest trying to get p-values from lmer models or something in our package.

Try installing the development version of piecewise and see if that fixes the problem:

install.packages(“devtools”)
library(devtools)
devtools::install_github(“jslefche/piecewiseSEM@devel”)

Feel free to contact me by email if you continue to run into problems

J


Jonathan S. Lefcheck, Ph.D. Tennenbaum Coordinating Scientist MarineGEO: https://marinegeo.si.edu/ Smithsonian Institution Phone: +1 (443) 482-2443 www.jonlefcheck.nethttp://www.jonlefcheck.net

From: jlamitmailto:notifications@github.com Sent: Saturday, March 23, 2019 1:27 PM To: jslefche/piecewiseSEMmailto:piecewiseSEM@noreply.github.com Cc: Subscribedmailto:subscribed@noreply.github.com Subject: [jslefche/piecewiseSEM] Errors using a random effect (#182)

Hi, I am receiving some errors that I don't quite understand. The data involves bark traits, and lichen data collected on trees in a common garden. There are 54 trees in the dataset, each tree belongs to a single tree genotype, there are 13 tree genotypes total, and each genotype is represented by 2-6 trees. On each tree the following variables were quantified: total lichen cover (TLCov) in quadrats on tree trunks, bark texture roughness on the tree trunks (BR), and condensed tannins of the bark of each tree trunk. All the three measured variables vary significantly among tree genotypes (done using a simple model with genotype as a random effect and a single trait as a response variable, in lmerTest). What I am interested in using SEM for is testing if condensed tannins or roughness are genetically based mechanisms that cause lichen cover to vary among tree genotypes. So, the simple SEM would have a direct path from tree genotype to condensed tannins and from tree genotype to bark roughness, and then genotype, condensed tannins and roughness would have paths leading to lichen cover. This seems like a relatively simple model to run but I get error messages when I fit it with lmerTest and with nlme. Below is the output from running the models using both programs. I am not certain that I understand the error messages entirely, or at least the reason for them. Any suggestions on where to go from here would be greatly appreciated. Thanks.

###################################

Below is the model set fit with lmerTest, which reveals some issues. First, it seems to think there are NAs in the dataset but there are no missing values in the input data. Second, output from looking at the Model_lmerTest object shows that no model actually has tree Genotype as an effect, and the data says 244 rows and a bunch of NAs, neither of which reflect the input dataset. Third, running summary and coefs both say there is an incorrect number of dimensions.

SEM 1 with lmerTest

Model_lmerTest<-psem(

Model_lmerTest Structural Equations: lmerModLmerTest: BR ~ 1 lmerModLmerTest: CT ~ 1 lmerModLmerTest: TLCov ~ BR + CT

Data: Geno BR CT TLCov 1 10 5.5 2.66 3.25 2 10 5.5 3.04 NA 3 10 5.5 3.07 NA 4 10 5.5 3.38 NA 5 10 10.0 2.66 NA 6 10 10.0 3.04 NA ...with 244 more rows

[1] "class(psem)"

summary(Model_lmerTest) |====================================================================================================================================================================================| 100% Error in newdata[, notrans[k]] : incorrect number of dimensions rsquared(Model_lmerTest, method = NULL) Response family link method Marginal Conditional 1 BR gaussian identity none 0.0000000 0.33483307 2 CT gaussian identity none 0.0000000 0.09397688 3 TLCov gaussian identity none 0.1261363 0.21848890 coefs(Model_lmerTest) Error in newdata[, notrans[k]] : incorrect number of dimensions

###################################

Below is the model set fit with nlme, which also reveals some issues. First, output from looking at the Model_nlme object shows that no model actually has tree Genotype as an effect, but unlike the lmerTest output (above) the row number is correct and there are no NAs indicated. Running summary and coefs both say there is less than 2 dimensions. The rsquared function has the same output as that for the lmerTest model set.

Model_nlme <- psem(

Model_nlme Structural Equations: lme: BR ~ 1 lme: CT ~ 1 lme: TLCov ~ BR + CT

Data: Tree.ID Geno BR TLCov CT 1 N1.04 1017 75.0 15.13 2.77 2 N1.07 T6 40.0 5.63 1.93 3 N1.08 1017 37.5 3.05 2.07 4 N1.10 996 17.5 2.25 2.43 5 N1.11 T6 85.0 25.03 2.46 6 N1.24 1017 55.0 32.25 1.75 ...with 54 more rows

[1] "class(psem)"

summary(Model_nlme) |====================================================================================================================================================================================| 100% Error in colnames<-(tmp, value = notrans) : attempt to set 'colnames' on an object with less than two dimensions rsquared(Model_nlme, method = NULL) Response family link method Marginal Conditional 1 BR gaussian identity none 0.0000000 0.33483306 2 CT gaussian identity none 0.0000000 0.09397688 3 TLCov gaussian identity none 0.1261362 0.21848891 coefs(Model_nlme) Error in colnames<-(tmp, value = notrans) : attempt to set 'colnames' on an object with less than two dimensions

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://nam02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fjslefche%2FpiecewiseSEM%2Fissues%2F182&data=02%7C01%7Clefcheckj%40si.edu%7C8ce66f38909a4c466ed308d6afb4c7b3%7C989b5e2a14e44efe93b78cdd5fc5d11c%7C1%7C0%7C636889588315332200&sdata=j9z5qh1AN63PfY34xe%2BzIMjwEGO1N4koLW1GCzWoM50%3D&reserved=0, or mute the threadhttps://nam02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FACPAVwaxxsZX7RO39ONRbu87xTCDteTeks5vZmPsgaJpZM4cFCky&data=02%7C01%7Clefcheckj%40si.edu%7C8ce66f38909a4c466ed308d6afb4c7b3%7C989b5e2a14e44efe93b78cdd5fc5d11c%7C1%7C0%7C636889588315342200&sdata=%2F8p6w%2Bx6fn2lrmiRawwCuM48FJ6q17eFZGI2IgAYXzI%3D&reserved=0.

MKLau commented 5 years ago

Hi Jon, I just updated Jamie's script to use the dev version of pairwiseSEM and it looks like we're still getting the same errors. I've just invited you to access the repo for the project, currently private as the data are not yet published.

If you have a sec, could you run the script and check the errors? (https://github.com/HarvardForest/comgen_sem) I'm getting the same errors as Jamie, so they should be reproducible. The paths to data relativized and the library install and loading are all coded, so it should run out of the box with minimal effort on your part.

jslefche commented 5 years ago

Hmm link is 404


Jonathan S. Lefcheck, Ph.D. Tennenbaum Coordinating Scientist MarineGEO: https://marinegeo.si.edu/ Smithsonian Institution Phone: +1 (443) 482-2443 www.jonlefcheck.nethttp://www.jonlefcheck.net

From: MKLaumailto:notifications@github.com Sent: Monday, March 25, 2019 1:01 PM To: jslefche/piecewiseSEMmailto:piecewiseSEM@noreply.github.com Cc: Lefcheck, Jonathanmailto:LefcheckJ@si.edu; Commentmailto:comment@noreply.github.com Subject: Re: [jslefche/piecewiseSEM] Errors using a random effect (#182)

Hi Jon, I just updated Jamie's script to use the dev version of pairwiseSEM and it looks like we're still getting the same errors. I've just invited you to access the repo for the project, currently private as the data are not yet published.

If you have a sec, could you run the script and check the errors? (https://github.com/HarvardForest/comgen_semhttps://nam02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2FHarvardForest%2Fcomgen_sem&data=02%7C01%7Clefcheckj%40si.edu%7C010b2e5e35344baf4e0708d6b1437c8e%7C989b5e2a14e44efe93b78cdd5fc5d11c%7C1%7C0%7C636891300745736164&sdata=KDT0tZwDtC95qHnZ0kE1WJ5dC9YWWvWvspmXekUWnP8%3D&reserved=0) I'm getting the same errors as Jamie, so they should be reproducible. The paths to data relativized and the library install and loading are all coded, so it should run out of the box with minimal effort on your part.

— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://nam02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fjslefche%2FpiecewiseSEM%2Fissues%2F182%23issuecomment-476289465&data=02%7C01%7Clefcheckj%40si.edu%7C010b2e5e35344baf4e0708d6b1437c8e%7C989b5e2a14e44efe93b78cdd5fc5d11c%7C1%7C0%7C636891300745746168&sdata=fPM1oE%2BS2ICnZ4j%2F%2FJK2apIpLZX3kIVM%2BR57r93RK18%3D&reserved=0, or mute the threadhttps://nam02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FACPAV3u0c83jgYXJfbP2jwWzi4C-v0bdks5vaQDYgaJpZM4cFCky&data=02%7C01%7Clefcheckj%40si.edu%7C010b2e5e35344baf4e0708d6b1437c8e%7C989b5e2a14e44efe93b78cdd5fc5d11c%7C1%7C0%7C636891300745746168&sdata=xvvyVUMAICJ%2BvpzsHdlvHzZfCbX0N3%2Bj5OTcr%2FjTveI%3D&reserved=0.

MKLau commented 5 years ago

Did you accept the org invite?

If not, you’ll get a 404 response because the repo’s private.

rachelthoms commented 5 years ago

has this been solved? having a similar problem

jslefche commented 5 years ago

Can you try now with the latest version of the development branch?

https://github.com/jslefche/piecewiseSEM/tree/devel

Let me know if the issue persists


Jonathan S. Lefcheck, Ph.D. Tennenbaum Coordinating Scientist MarineGEO: https://marinegeo.si.edu/ Smithsonian Institution Phone: +1 (443) 482-2443 www.jonlefcheck.nethttp://www.jonlefcheck.net

From: rachelthoms123mailto:notifications@github.com Sent: Thursday, March 28, 2019 6:18 PM To: jslefche/piecewiseSEMmailto:piecewiseSEM@noreply.github.com Cc: Lefcheck, Jonathanmailto:LefcheckJ@si.edu; Commentmailto:comment@noreply.github.com Subject: Re: [jslefche/piecewiseSEM] Errors using a random effect (#182)

has this been solved? having a similar problem

— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://nam02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fjslefche%2FpiecewiseSEM%2Fissues%2F182%23issuecomment-477791862&data=02%7C01%7Clefcheckj%40si.edu%7Cbaf7ddc96c6843c702fa08d6b3cb417c%7C989b5e2a14e44efe93b78cdd5fc5d11c%7C1%7C1%7C636894082898720026&sdata=drIYui%2BagT%2BinQ1olT%2F8F1UqyhIh0DXcOWSvh%2BRLAN0%3D&reserved=0, or mute the threadhttps://nam02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FACPAVycAaXpYbxOr1AK_yZ6m_tgIUYX8ks5vbT-egaJpZM4cFCky&data=02%7C01%7Clefcheckj%40si.edu%7Cbaf7ddc96c6843c702fa08d6b3cb417c%7C989b5e2a14e44efe93b78cdd5fc5d11c%7C1%7C1%7C636894082898720026&sdata=OWVLlcoud73dacmbFNfGXa%2Fps3cjc8fu9C6fUzeSFKI%3D&reserved=0.

MKLau commented 5 years ago

I still get:

Error in colnames<-(*tmp*, value = notrans) : attempt to set 'colnames' on an object with less than two dimensions

This is from:

NetX1_nlme <- psem(
    lme(BR ~ 1, random = ~1|geno, data = RawData),
    lme(CT ~ 1, random = ~1|geno, data = RawData),
    lme(netX1 ~ BR + CT, random = ~1|geno, data = RawData)
)
summary(NetX1_nlme)
jslefche commented 5 years ago

Ah we don’t support intercept-only models in that way (although maybe we should)…

Its not clear what this “SEM” is testing. it looks like a single regression where you are also trying to model variance in each exogenous predictor through the random effect. It might easiest if we continue this convo privately by email so you can elaborate a bit more on what you’re testing…

J


Jonathan S. Lefcheck, Ph.D. Tennenbaum Coordinating Scientist MarineGEO: https://marinegeo.si.edu/ Smithsonian Institution Phone: +1 (443) 482-2443 www.jonlefcheck.nethttp://www.jonlefcheck.net

From: MKLaumailto:notifications@github.com Sent: Saturday, March 30, 2019 3:12 PM To: jslefche/piecewiseSEMmailto:piecewiseSEM@noreply.github.com Cc: Lefcheck, Jonathanmailto:LefcheckJ@si.edu; Commentmailto:comment@noreply.github.com Subject: Re: [jslefche/piecewiseSEM] Errors using a random effect (#182)

I still get:

Error in colnames<-(tmp, value = notrans) : attempt to set 'colnames' on an object with less than two dimensions

This is from:

NetX1_nlme <- psem(

lme(BR ~ 1, random = ~1|geno, data = RawData),

lme(CT ~ 1, random = ~1|geno, data = RawData),

lme(netX1 ~ BR + CT, random = ~1|geno, data = RawData)

����)

summary(NetX1_nlme)

— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://nam02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fjslefche%2FpiecewiseSEM%2Fissues%2F182%23issuecomment-478279611&data=02%7C01%7Clefcheckj%40si.edu%7Ce9efc919ad2f4161190908d6b543a7a0%7C989b5e2a14e44efe93b78cdd5fc5d11c%7C1%7C0%7C636895699531169810&sdata=zOXNvrIEUrdPqbAAF0a5RAepKf8AFELutuBTD6eDo%2Bs%3D&reserved=0, or mute the threadhttps://nam02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FACPAVyTCiA9u_b6yerxBSU5fPvJ2rzHKks5vb7cdgaJpZM4cFCky&data=02%7C01%7Clefcheckj%40si.edu%7Ce9efc919ad2f4161190908d6b543a7a0%7C989b5e2a14e44efe93b78cdd5fc5d11c%7C1%7C0%7C636895699531179815&sdata=VsdrH1HBYpFOkeKGR7wkL9UPQ6%2FvmJyzon0cvcMA3LY%3D&reserved=0.

MKLau commented 5 years ago

Ok, so sounds like it might be a model specification issue. Yeah, let's move this discussion out of here for now. I'll start an issue in the comgen_sem repo and tag you.

jebyrnes commented 5 years ago

BTW: Intercept only models are actually something I think we want, as it let's us set something as an endogenous variable that is not connected to anything else - so, models where we sever links. Just my $0.02.

jslefche commented 5 years ago

I agree

Also, is this not essentially a single indicator latent??


Jonathan S. Lefcheck, Ph.D. Tennenbaum Coordinating Scientist MarineGEO: https://marinegeo.si.edu/ Smithsonian Institution Phone: +1 (443) 482-2443 www.jonlefcheck.nethttp://www.jonlefcheck.net

From: Jarrett Byrnesmailto:notifications@github.com Sent: Wednesday, April 3, 2019 9:31 AM To: jslefche/piecewiseSEMmailto:piecewiseSEM@noreply.github.com Cc: Lefcheck, Jonathanmailto:LefcheckJ@si.edu; Commentmailto:comment@noreply.github.com Subject: Re: [jslefche/piecewiseSEM] Errors using a random effect (#182)

BTW: Intercept only models are actually something I think we want, as it let's us set something as an endogenous variable that is not connected to anything else - so, models where we sever links. Just my $0.02.

— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://nam02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fjslefche%2FpiecewiseSEM%2Fissues%2F182%23issuecomment-479514961&data=02%7C01%7Clefcheckj%40si.edu%7C0378988a28b041c4f65508d6b841043a%7C989b5e2a14e44efe93b78cdd5fc5d11c%7C1%7C0%7C636898986722120451&sdata=%2FAoq8HNdCAwCn7B5dfwAvxQ9FfUkcZJSZ5%2Fwub4VYFs%3D&reserved=0, or mute the threadhttps://nam02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FACPAV5TZfKfy_QOXR9_11NCdelatGutJks5vdLstgaJpZM4cFCky&data=02%7C01%7Clefcheckj%40si.edu%7C0378988a28b041c4f65508d6b841043a%7C989b5e2a14e44efe93b78cdd5fc5d11c%7C1%7C0%7C636898986722120451&sdata=OSHWM%2BOueb%2BPrfIvWat1A%2FOgDvMr3h4b9e%2FhKA6a58Q%3D&reserved=0.

MKLau commented 5 years ago

@jslefche perhaps the way we've coded the model suggests a single indicator latent variable, but I don't think that that's our intent here. Geno is genotype identity and CT and BR are both plant traits. We'd like to know the effect of genotype on CT and BR, and as such I would interpret it as a real variable not an indicator. I would think that if anything, geno would be a latent (or more likely a composite variable) comprised of binary indicators for the genotypic identity of each tree, which is how multi-level factors used to be treated in AMOS.

But, I'd love to have your input on how best to model this. I've added a new issue to the private repo comgen_sem and tagged you. Go ahead and send comments there so we don't clutter this issue.