Trouble with pairs() function in nlme - r

I am having trouble getting the pairs() function to work in nlme. Take this example from Pinhiero and Bates Mixed-Effects Models in S and S-Plus.
The model itself runs just fine
fm1Theo.lis <- nlsList(conc ~ SSfol(Dose, Time, lKe, lKa, lCl), data = Theoph)
But the pairs plot...
pairs(fm1Theo.lis, id = 0.1)
...returns this error
Error in as.data.frame.default(x) :
cannot coerce class "c("nlsList", "lmList")" to a data.frame
I also tried
pairs(fm1Theo.lis, ~ ranef(., level = 2), id = 0.1)
But get the same error. Any ideas?

Here's how one may think in this case. The error
Error in as.data.frame.default(x) :
cannot coerce class ‘c("nlsList", "lmList")’ to a data.frame
says that some object of class c("nlsList", "lmList") is being coerced to a data frame. Now since fm1Theo.lis is the result of using nlsList, it seems that the object in the error is indeed nlsList. That means that pairs does not know what to do with objects of such class. To confirm this, we can run
pairs.default(fm1Theo.lis, id = 0.1)
which is what is going to happen when no specific method for fm1Theo.lis is found. Indeed the error is the same. In one way or another confirming that nlsList and comes from nlme, it becomes clear that the issue is with loading the nlme package. Loading it or restarting the session then is almost surely going to help.

Related

When using the function boxCox in R I keep getting this error "Error in as.data.frame.default(

I have the correct package alr4 which contains cars so it shouldn't be a problem with the function itself
box <- lm(Distance~Speed, data = stop)
boxCox(box)
This is the error
Error in as.data.frame.default(data, optional = TRUE) : cannot coerce class ‘"function"’ to a data.frame
The lm function runs just fine on its own, and I have used it in other analysis just fine, so it isn't an issue there. I am mostly just confused about the error and how to fix it. If you have suggestions or know why it happens that would be appreciated! Thanks

How to fix lmer error: "Error in as(value, fieldClass, strict=FALSE) :"?

I'm getting a strange error when I run an lmer function in r.
I've tried changing the variable types (all of them are numeric or factor) and removing the NA before analysis, but nothing seems to work.
model_1 <- lmer(Q14 ~ gender * time + (1|OMID), data=data)
summary(model_1)
Specifically, my error message reads:
Error in as(value, fieldClass, strict = FALSE) :
internal problem in as(): “labelled” is(object, "numeric") is TRUE, but the metadata asserts that the 'is' relation is FALSE
Not sure why this is happening, but I can't seem to find any answers for it. Any help would be appreciated.
Thanks!
I think lmer has a problem with 'labelled' data. If you un-label the predictors it should work fine.
I had the same error: the code for the lme-formula worked perfectly fine and one day I encountered that error. The solution in my case was simply to restart the R session, reload the data - in my case from SPSS via library("haven")::read.sps and after that load library("lme4") and execute the lme-formula.
So, if the formula worked before without any error, maybe just clean the project environment and re-run the most crucial code without any additional packages loaded. Maybe it's just some "cross-contamination" between packages or an unwanted effect of any package on the dataframe.

Functions can't be found, but the packages are certainly installed and loaded

I'm trying to predict values with a linear mixed model and new data, but I keep getting errors that both functions I'm trying (predict.lme from nlme, and predict.merMod from lme4) do not exist. The packages are installed and loaded.
I first tried using predict.lme (nlme). I note that I've installed and loaded the package, since obviously this is a reason the function may not be found.
But I get this error:
Error in predict.lme(object = PlotModel.best, newdata = PlotInvData_predict,:
could not find function "predict.lme"
I've had random problems with nlme before, so I decide to try predict.merMod (lme4), but I get the same error that that function can't be found. I thought maybe those functions can't handle the interval argument that the regular predict function can. I get rid of that, and it still doesn't work. I tested other functions in those packages, and they work just fine. So something is wrong with my workflow, but I can't figure out what. It's the same workflow as with the regular predict function, which I've used just fine.
What am I doing wrong?
Here's the new data:
PlotInvData_predict <- read.csv(file="D:/ThesisPart2/Data/PlotInvData_predict.csv", header=TRUE, sep=",")
Heres the model:
PlotModel.best <- lmer(d_InvCov ~ TimeSinceDist + UnitArea_ha + (1|MgmtSame)+ (1|LandMgmt.1), PlotInvData)
Then I first tried using predict.lme (nlme). I note that I've installed and loaded the package, since obviously this is a reason the function may not be found.
install.packages("nlme")
library(nlme)
p_bd <- predict.lme(object=PlotModel.best, newdata=PlotInvData_predict, interval="confidence")
Here's the error, again:
Error in predict.lme(object = PlotModel.best, newdata = PlotInvData_predict, :
could not find function "predict.lme"
When using the regular predict function with a different model (linear model), I would get something like this as a result:
fit lwr upr
1 1.098959 0.5803632 1.617556
2 1.156005 0.6627035 1.649306
3 1.213050 0.7408797 1.685220
4 1.270095 0.8143122 1.725879
5 1.327141 0.8824762 1.771805
6 1.384186 0.9449715 1.823401
7 1.441231 1.0015871 1.880875

pool() function error in R

Need some assistance in R. I ran the lm function and assigned it to variable. Then I used that variable to retrieve summary using pool() function.
Here's the code but getting argument data is missing. I saw same example but didn't get that error.
> modelFit1 <- with(imputed,lm(MPG ~ CYLINDERS+SIZE+HP+WEIGHT+ACCEL+ENG_TYPE))
> summary(pool(modelFit1))
Error in pool(modelFit1) : argument "data" is missing, with no default
Appreciate any help. Thanks.
I also had this error. In my case it was due to conflicting packages. I had loaded packages: "mice" and "mi", and "mitools", among others. There is no 'data' argument necessary for 'pool' in the "mice" package, but mi::pool does require a data argument. If you are using mice and following examples in mice's documentation, Try
pooled <- mice::pool(modelFit1))
summary(pooled)

R placing rugarch output into a dataframe

I have updated to the last version of R and updated the rugarch package as well.
Unfortunately some code that worked previously no longer works. I now get an error.
I would be greatful for some help to get the output into a dataframe.
library(rugarch)
data(sp500ret)
spec = ugarchspec( )
fit1 = ugarchfit(spec = spec, data = sp500ret)
df.fit1 <- as.data.frame(fit1,which="VaR")
Error in as.data.frame.default(fit1, which = "VaR"):
cannot coerce class "structure("uGARCHfit", package = "rugarch")" to a
data.frame
attributes(fit1)
shows:$fit$sigma
but when I try:
df1 <- data.frame(fit1$fit$sigma)
I get an error message;
Error in fit1$fit : $ operator not defined for this S4 class
as.data.frame(fit1, which="VaR") NEVER worked with an object of class uGARCHfit (you are confusing this with a uGARCHroll object). If you want the conditional VaR you can NOW (in the new version) use the quantile method e.g. quantile(fit1, c(0.01,0.05)).
If you want the conditional standard deviation then you should use sigma(fit1) which will return an xts matrix, or fit1#fit$sigma (# goes after an object in S4 classes). This and most other questions can be answered by carefully reading the documentation, vignette and the author's website which contains details of the changes.

Resources