Elasso
Elasso

Reputation: 21

Creating a plot from random sample with mean and standard deviation

Repeat the following 10 times and calculate the mean each time: sample 30 observations from a normally-distributed population having mean 0 and standard deviation 2. Create a data.frame containing the output from the 10 simulations and generate a single plot demonstrating the mean and st.dev of each of 10 samples.

I am a complete beginner and don't know where to go from here:

tensample <- replicate(10, rnorm(30, mean = 0, sd = 2))
tensampleDF <- data.frame(tensample)

I know I can find the mean and sd of each of the samples like so:

means <- colMeans(tensampleDF)
sd <- apply(tensampleDF, 2, sd)

But how to plot them together?

Upvotes: 1

Views: 911

Answers (2)

Bernhard
Bernhard

Reputation: 4427

This is a ggplot2 answer

tensample <- replicate(10, rnorm(30, mean = 0, sd = 2))
tensampleDF <- data.frame(tensample)
m = colMeans(tensampleDF)

d <- data.frame(id = 1:10,
                m = m,
                upper = m + apply(tensampleDF, 2, sd),
                lower = m - apply(tensampleDF, 2, sd))
library(ggplot2)
ggplot(d) +
  geom_pointrange(aes(x=id, y=m, ymin=lower, ymax = upper))

enter image description here

You should correct the x-axis stops etc but now your are free to choose the graphics system.

Edit:

In order to achieve acceptable axes maybe do something more along the lines of

ggplot(d) +
  geom_pointrange(aes(x=id, y=m, ymin=lower, ymax = upper)) +
  scale_x_continuous(breaks = 1:10, minor_breaks = NULL) +
  xlab("x") +
  ylab("y") + 
  theme_bw()

Upvotes: 0

Bernhard
Bernhard

Reputation: 4427

This will off course depend on which graphics system ist meant to be used. This is a way for base graphics:

tensample <- replicate(10, rnorm(30, mean = 0, sd = 2))
tensampleDF <- data.frame(tensample)

m <- colMeans(tensampleDF)
upper <- m + apply(tensampleDF, 2, sd)
lower <- m - apply(tensampleDF, 2, sd)

plot(1:10, colMeans(tensampleDF), pch = 15, ylim = c(-5, 5),
     xlab = "x", ylab = "y")
arrows(x0 =1:10, x1 = 1:10, y0 = lower, y1 = upper, length = 0)

It will produce something like

enter image description here

Upvotes: 1

Related Questions