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

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.

Related

Error in as.double(y) : cannot coerce type 'environment' to vector of type 'double' in ggplot command in R

I am trying to make a plot between two different variables (RodC independent varaible and TickPrev dependent varaible) using the ggplot command. I have the following packages installed: pacman, party, rio, tidyverse,dplyr,MASS.
gg1314 <- ggplot(df1314, aes(RodC, TickPrev, color=YearS)) +
geom_jitter() +
labs(title = "Tick Prevalence vs Captured Rodents 2013-2014",
x="Tick Prevalence (%)",
y="Nº of Captured Rodents") +
scale_color_discrete(name="YearSeason")
The code above worked perfectly fine, but when I deleted a few rows to conduct a similar analysis with the same variables, the code start to give the following error:
ggNBS <- ggplot(dfNBS, aes(RodC, TickPrev, colors=Year)) +
geom_jitter() +
labs(title = "Tick Prevalence vs Captured Rodents NBS",
x="Tick prevalence (%)",
y="Nº of captured rodents") +
scale_color_discrete(name="Year")
Error in as.double(y) : cannot coerce type 'environment' to vector
of type 'double'
The only difference between the dataframes is that I eliminated some rows. But the variables remained the same.
enter image description here
How can I solve this.
Thank you very much
Image from df1314. dfNBS is the same, but in the MiceS and YearS it only contains information that had NBS or NBS2.
I found the answer. I was also using %>% plot() so that the plot of the graphic was done right way. After removing that line it solved the problem. :) Thank you for the help.
However now, the plot doens't present colors, all the points are black and shouldn't be.

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!

Using the QQ Plot functionality in ggplot

I'm brand new to R, and have a data frame with 8 columns that has daily changes in interest rates. I can plot QQ plots for data each of the 8 columns using the following code:
par(mfrow = c(2,4))
for(i in 1:length(column_names)){
qqnorm(deltaIR.df[,i],main = column_names[i], pch = 16, cex = .5)
qqline(deltaIR.df[,i],cex = .5)
}
I'd like now to use the stat_qq function in the ggplot2 package to do this more elegantly, but just can't get my arms around the syntax - I keep getting it wrong. Would someone kindly help me translate the above code to use ggplot and allow me to view my 8 QQ plots on one page with an appropriate header? Trying the obvious
ggplot(deltaIR.df) + stat_qq(sample = columns[i])
gets me only an error message
Warning: Ignoring unknown parameters: sample
Error: stat_qq requires the following missing aesthetics: sample
and adding in the aesthetics
ggplot(deltaIR.df, aes(column_names)) + stat_qq()
is no better. The error message just changes to
Error: Aesthetics must be either length 1 or the same as the data (5271)
In short, nothing I have done so far (even with Google's assistance) has got me closer to a solution. May I ask for guidance?

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

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

r - Add text to each lattice histogram with panel.text but has error "object x is missing"

In the following R code, I try to create 30 histograms for the variable allowed.clean by the factor zip_cpt(which has 30 levels).
For each of these histograms, I also want to add mean and sample size--they need to be calculated for each level of the factor zip_cpt. So I used panel.text to do this.
After I run this code, I had error message inside each histogram which reads "Error using packet 21..."x" is missing, with..." (I am not able to read the whole error message because they don't show up in whole). I guess there's something wrong with the object x. Is it because mean(x) and length(x) don't actually apply to the data at each level of the factor zip_cpt?
I appreciate any help!
histogram(~allowed.clean|zip_cpt,data=cpt.IC_CAB1,
type='density',
nint=100,
breaks=NULL,
layout=c(10,3),
scales= list(y=list(relation="free"),
x=list(relation="free")),
panel=function(x,...) {
mean.values <-mean(x)
sample.n <- length(x)
panel.text(lab=paste("Sample size = ",sample.n))
panel.text(lab=paste("Mean = ",mean.values))
panel.histogram(x,col="pink", ...)
panel.mathdensity(dmath=dnorm, col="black",args=list(mean=mean(x, na.rm = TRUE),sd=sd(x, na.rm = TRUE)), ...)})
A discussion I found online is helpful for adding customized text (e.g., basic statistics) on each of the histograms:
https://stat.ethz.ch/pipermail/r-help/2007-March/126842.html

Resources