How to change or reset parameters in the plot(ACF)-device within R-studio - r

I have estimated a two-intercept mixed multilevel-model using the function lme of the r-package nlme.
After that I checked for autocorrelation by visual inspection using the plot(ACF)-function.
Plotting for the first time I specified maxlag=16.
Now I have two problems: First, the maxlag parameter seems to be stuck somehow, i.e. further plots are all plotted with maxlag=16 even when maxlag is set to other values. 2. The plot is cropped at y=0.8 even if the value of lag 0 obviously is 1.
In the following I share the respective replex in hope of getting answers or inputs on how to solve these two issues.
Link to the dataset and if prefered to copy-paste to the following code-script as well:
#read.dataset:
datafclr <-read.csv("datafclr.csv", header = TRUE, sep = ",", dec = ".", fill = TRUE)
#required packages:
library("Matrix")
library("nlme")
#model-estimation:
tim2 <- lme(fixed=EERTmn ~ male + female +
(male:time7c) + (female:time7c) +
(male:IERT_Cp) + (female:IERT_Cp) +
(male:IERT_Cp_Partner) + (female:IERT_Cp_Partner)-1,
control=list(maxIter=100000), data=datafclr,
random=~male + female -1|dyade/female, correlation=corAR1(), na.action=na.omit)
summary(tim2)
#checking for autocorrelation:
plot(ACF(tim2, maxlag = 16), alpha = 0.01)
Results in the following plot:
This results in thin plot
When I change the maxlag:
plot(ACF(tim2, maxlag = 10), alpha = 0.01)
It results in the same plot
Many thanks in advance!
Best,
Patrick

Joes Schwartz helped me solve these issues in the R-Studio community. For the case someone will have the same difficulties I had I'm sharing his answers here:
First issue: maxlag needs to be typed maxLag and the function works fine.
Second issue: detailed help under the following link:
https://community.rstudio.com/t/resetting-plotting-settings-plot-acf-data/19441

Related

Problem with R code: Computation failed in `stat_signif()`: not enough 'y' observations

I am trying to compare two groups with 47 observations in each and tried to include the significance level using ggsignif but it gives me this error message :
Computation failed in stat_signif():
not enough 'y' observations
This is the data :enter image description here
The first group being "Un-C_SL" / "Un-C_RL" / "Un-C" and the second one that doesn't appear in the image is "Vp_SL" / "Vp_RL" / "Vp"
This is the code I used:
ggplot(UnC_Vp_2, aes(x = Condition, y = Biomass)) + geom_boxplot() + geom_signif(comparisons = list("Un-C", "Vp"), map_signif_level = TRUE)
I was able to get the box plot but not the significance level. What should I do?
The comparisons = argument needs a list() of pairwise comparisons that are each contained in a c(), so it should work if you change it to comparisons = list(c("Un-C", "Vp")).
See this github issue also. I found this question when I had the same problem; I had forgotten it needed to be a list and not just c() and I agree that a more informative error message would help.

How do I use the group argument for the plot_summs() function from the jtools package?

I am plotting my coefficient estimates using the function plot_summs() and would like to divide my coefficients into two separate groups.
The function plot_summs() has an argument groups, however, when I try to use it as explained in the documentation, I do not get any results nor error. Can someone give me an example of how I can use this argument please?
This is the code I currently have:
plot_summs(model.c, scale = TRUE, groups = list(pane_1 = c("AQI_average", "temp_yearly"), pane_2 = c("rain_1h_yearly", "snow_1h_yearly")), coefs = c("AQI Average"= "AQI_average", "Temperature (in Farenheit)" = "temp_yearly","Rain volume in mm" = "rain_1h_yearly", "Snow volume in mm" = "snow_1h_yearly"))
And the image below is what I get as a result. What I would like to get is to have two panes separate panes. One which would include "AQI_average" and "temp_yearly" and the other one that would have "rain_1h_yearly" and "snow_1h_yearly". Event though I use the groups argument, I do not get this.
Output of my code
By minimal reproducible example, markus is refering to a piece of code that enables others to exactly reproduce the issue you are refering to on our respective computers, as described in the link that they provided.
To me, it seems the problem is that the groups function does not seem to work in plot_summs - it seems someone here also pointed it out.
If plot_summs is replaced by plot_coef, the groups function work for me. However, the scale function does not seem to be available. A workaround might be:
r <- lm(Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width, data = iris)
y <- plot_summs(r, scale = TRUE) #Plot for scaled version
t <- plot_coefs(r, #Plot for unscaled versions but with facetting
groups =
list(
pane_1 = c("Sepal.Width", "Petal.Length"),
pane_2 = c("Petal.Width"))) + theme_linedraw()
y$data$group <- t$data$group #Add faceting column to data for the plot
t$data <- y$data #Replace the data with the scaled version
t
I hope this is what you meant!

Groups with fewer than two data points have been dropped. ggplot r

A couple of weeks ago, I drew a ggplot density plot in r. It worked fine. And then yesterday, I revisited and ran the same code. There was absolutely no change in the body of the code (only change in the Rmarkdown formatting like changing font size of texts, echo = FALSE, etc). I simply re-ran the same code and for some reason it does not work.
Here's the code for the dataset:
m_hospitals = m_hospitals %>% group_by(Provider.Name) %>% summarise(accSum =
sum(Average.Covered.Charges), atpSum = sum(Average.Total.Payments), ampSum =
sum(Average.Medicare.Payments), accMean = mean(Average.Covered.Charges),
atpMean = mean(Average.Total.Payments), ampMean =
mean(Average.Medicare.Payments))
#Take a sample
set.seed(1219)
sample_m_hospitals = m_hospitals[sample(nrow(m_hospitals), 30), ]
And here's the code for the plot that worked before but not anymore:
ggplot(data = sample_m_hospitals) +
aes(x = Provider.Name, y = accMean) +
geom_density(alpha = .75) + theme(axis.text.x = element_text(angle = 45,
hjust = 1))
It's giving me this message, "Groups with fewer than two data points have been dropped" * 30.
It is true that each of 30 rows only has 1 observation because they are summarised. Funny thing is, the message did not pop up before, did not drop any data points and worked fine. I even have the screenshot I took for the plot drawn. Here's the link: 1
One change I feel suspicious about is updating R (updated before re-plotting, 3.5.0 > 3.5.1) which erased all my libraries that I had to re-install all of them. But I'm not sure if the update has to do anything with this issue. It worked fine, but why is it suddenly not working? I don't understand.. Please help! I just wanna plot this in similar form however possible.
Contents updated as per you guys' comments:
Screenshots for devtools::session_info()
2 , 3
Screenshots for sample_m_hospitals
4 , 5
I had a similar issue which was resolved by converting columns. Numeric columns were being imported as strings.

How to control plot layout for lmerTest output results?

I am using lme4 and lmerTest to run a mixed model and then use backward variable elimination (step) for my model. This seems to work well. After running the 'step' function in lmerTest, I plot the final model. The 'plot' results appear similar to ggplot2 output.
I would like to change the layout of the plot. The obvious answer is to do it manually myself creating an original plot(s) with ggplot2. If possible, I would like to simply change the layout of of the output, so that each plot (i.e. plotted dependent variable in the final model) are in their own rows.
See below code and plot to see my results. Note plot has three columns and I would like three rows. Further, I have not provided sample data (let me know if I need too!).
library(lme4)
library(lmerTest)
# Full model
Female.Survival.model.1 <- lmer(Survival.Female ~ Location + Substrate + Location:Substrate + (1|Replicate), data = Transplant.Survival, REML = TRUE)
# lmerTest - backward stepwise elimination of dependent variables
Female.Survival.model.ST <- step(Female.Survival.model.1, reduce.fixed = TRUE, reduce.random = FALSE, ddf = "Kenward-Roger" )
Female.Survival.model.ST
plot(Female.Survival.model.ST)
The function that creates these plots is called plotLSMEANS. You can look at the code for the function via lmerTest:::plotLSMEANS. The reason to look at the code is 1) to verify that, indeed, the plots are based on ggplot2 code and 2) to see if you can figure out what needs to be changed to get what you want.
In this case, it sounds like you'd want facet_wrap to have one column instead of three. I tested with the example from the **lmerTest* function step help page, and it looks like you can simply add a new facet_wrap layer to the plot.
library(ggplot2)
plot(Female.Survival.model.ST) +
facet_wrap(~namesforplots, scales = "free", ncol = 1)
Try this: plot(difflsmeans(Female.Survival.model.ST$model, test.effs = "Location "))

Using panel.mathdensity and panel.densityplot in lattice graphics to plot Bayesian prior and posterior

I am trying to plot a Bayesian prior and posterior distribution using lattice graphics. I would like to have both distributions in one panel, for direct comparison.
I've tried different solutions all day, including qqmath but I didn't get them to work. Here's the attempt that has been most successful so far:
# my data
d <- dgamma(seq(from=0.00001,to=0.01,by=0.00001),shape = .1, scale = .01)
# my plot
densityplot(~d,
plot.points=FALSE,
panel = function(x,...) {
panel.densityplot(x,...)
panel.mathdensity(
dmath = dgamma,
args = list(shape = .1, scale=.01)
)
}
)
Even though the code runs through nicely, it doesn't do what I want it to. It plots the posterior (d) but not the prior.
I added stop("foo") to densityplot(...) to stop execution if an error occurs and I searched online for the error message:
Error in eval(substitute(groups), data, environment(formula)) : foo
But there are only a few results and they seem unrelated to me.
So, here's my question: Can anyone help me with this approach to achieve what I want?
I asked a similar question which leads to the same result. I got an answer and it was useful. You can find everything here

Resources