Problem calculating confidence intervals

rstudio
datascience

#1

Hello dear R Community,

I am having some issues calculating the 95% confidence intervals for the mean prices of diamonds according to their depth. The data used were taken from the “Diamond” database (http://ggplot2.tidyverse.org/reference/diamonds.html) .

The Problem:
I don’t know why, but I keep getting missing values (NA’s) for my confidence intervals. I tried to calculate the confidence intervals with the R function “ci.mean”
(https://www.rdocumentation.org/packages/Publish/versions/2017.10.24/topics/ci.mean), but I got even more missing values.

It would be great if anyone could tell me why this keeps happening.

Here is my code:

diamonds %>%
  group_by(depth) %>%
  summarize(
    mean = mean(price),
    sd = sd(price),
    se = sdev / sqrt(length(price)),
    t <- qt(1-.025, df=(length(price)-1)),
    ci.lower = m - (t * stderr),
    ci.upper = m + (t * stderr)) -> results 
results

Many many thanks in advance!

Best,
JohnDoe


#2

I think I figured it out. I get the missing values in cases where there was only one observation for the depth. Confidence intervals therefore couldn’t be calculated and R returned NA’s.

Thank you anyway!