[R] Sample mean in R
(Ted Harding)
Ted.Harding at manchester.ac.uk
Sun Oct 5 22:37:24 CEST 2008
On 05-Oct-08 20:00:00, dilian wrote:
> I am having issues with the following:
>
> (muhat = 1/n^2(sum of all the xi's) )
>
> essentially if xbar = the sample mean, muhat = sample mean but square
> the n.
>
> Question:
> Use R to run a Monte Carlo simulation which compares the finite-sample
> performance of xbar and muhat. Specifically generate 1000 samples n=30
> from a standard normal distribution. For each sample calculate xbar and
> muhat. I have no problem calculating the mean of the xbar's - however
> I cannot figure out how to set up the muhat variable and find the
> means. My code is as follows:
>
># R code starts here
> rm(list=ls())
> set.seed(100)
>
> n<-30
> s<-1000
>
> xbar<-rep(0,s)
> muhat<-rep(0,s)
>
> for (i in 1:s) {
> x<-rnorm(0,n=10)
> xbar[i]<-mean(x)
> muhat[i]<-mean(x^(-1/2))
> }
The line muhat[i]<-mean(x^(-1/2)) is anomalous -- in more than
one way! [1] It does not match up with your stated definition
of muhat (there is no "x^(-1/2)" there); [2] x^(-1/2) is going
to give a bad result for negative values of x anyway (as will
be the case with your rnorm(0,n=10)).
To achieve what you defined as muhat, surely
muhat[i] <- mean(x)/n
(where n <- length(x) somewhere, or simply n <- 10).
But in any case I am wondering why you are interested in that
"muhat = 1/n^2(sum of all the xi's)" definition of muhat.
Part of your message seems to be going into one ear, and part into
my other; when they meet in the middle, they compare notes and
being to wonder if you are getting Mean mixed up with Standard
Error (SE^2 = var(x)/n).
Hmmm.
Hoping this helps,
Ted.
> cat("Estimated mean of xbar:",mean(xbar),"\n")
> cat("Estimated mean of muhat:",mean(muhat),"\n")
>
> Any help would be greatly appreciated.
> --
> View this message in context:
> http://www.nabble.com/Sample-mean-in-R-tp19828546p19828546.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 05-Oct-08 Time: 21:37:21
------------------------------ XFMail ------------------------------
More information about the R-help
mailing list