I have vector $x=[3, 5, 7, 8, 12, 13, 14, 18, 21]$ and  wanted to see summary   statistics.  if I use summary(x) function in R, My anwser     Min. 1st Qu.  Median    Mean 3rd Qu.    Max.     3.00    7.00   12.00   11.22   14.00   21.00   But actual anwser is Q1 = 6 and Q2=16   What is the difference ?
121279
I'm working through a statistics textbook while learning R and I've run into a stumbling block on the following example:    After looking at ?quantile I attempted to recreate this in R with the following:  &gt; nuclear &lt;- c(7, 20, 16, 6, 58, 9, 20, 50, 23, 33, 8, 10, 15, 16, 104) &gt; quantile(nuclear)    0%   25%   50%   75%  100%    6.0   9.5  16.0  28.0 104.0    Given that the text and R have different results, I'm gathering that R is utilizing the median in the calculation of the first and third quartiles.  Question:  Should I include the median in calculating the first and third quartiles?  More specifically, does the textbook or R have this correct? If the textbook has this correct, is there a way to properly achieve this in R?  Thanks in advance.
112202
I am new to statistics and I currently deal with ANOVA. I carry out an ANOVA test in R using  aov(dependendVar ~ IndependendVar)   I get – among others – an F-value and a p-value.   My null hypothesis ($H_0$) is that all group means are equal.   There is a lot of information available on , but I don't know how to read an F-statistic and how F and p are connected.   So, my questions are:   How do I determine the critical F-value for rejecting $H_0$? Does each F have a corresponding p-value, so they both mean basically the same? (e.g., if $p&lt;0.05$, then $H_0$ is rejected)
13317
If I draw 5 samples from a distribution that is cont., what is the probability that the median of the population is between the xmin and xmax of the draw samples?  What is the formula to apply here?
47209
I'm trying to model a Roulette game where if you bet on red (out of red(18/37), black(18/37), green(1/37)) and if you the ball lands on red you win 1 dollar and if it doesn't you lose $1. So in this case, is it a binomial distribution? Since there are only 2 outcomes from the gamblers perspective, red or not red. If thats the case are these 2 R functions the same?  set.seed(3) sample(c(0,1),100,replace=T,prob=c(19/37,18/37))     rbinom(n = 100, size = 1, prob = 18/37)
114943
Let $X_1, X_2, \ldots, X_{16}$ be  independent with $N(3,4)$ distributions and $\bar{X}$ denote the sample mean. Find:   $P(-8 &lt; 2 \bar{X} &lt; -4)$. A number $K$ such that $P(-K &lt; 2 \bar{X} &lt; K) = 0.99$. $P(-10 &lt; 3 X_1+X_7 &lt; -8)$. A number $K$ such that $P(3 X_2+X_5 &lt; K) = 0.3$.   If someone could give me some help or hints on where to start with (a) or any of the problems that would be really helpful and I'd really appreciate it.
113648
If i have this sorted data:  $1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 5, 6, 6, 6, 6, 6, 7, 7, 8, 8, 8, 9, 9, 10, 10$  They are 25 numbers.  How should I calculate the quartiles and percentiles?  I try this:  $Q_2 = 2 * \frac{25}{4} = 12.5$  $Q_1 = 1 * \frac{25}{4} = 6.25$   $P_{50} = 50 * 25/100 = 12.5$  Well, but what should I do when it is decimal, for non-clustered data? In some part I have seen,     approach the nearest larger whole.   So, $12.5 \rightarrow 13$  So, $6.25 \rightarrow 7$  In other places I read:     the value will be the average between the data that is at the left of   the position with the data on the right.   So, What should I really do?
42935
My PDF:     M was estimated and found to be 5.   I need to work out the quartiles for the PDF above. In addition, I need to use different methods of estimation to estimate the parameters. So far I've successfully used the method of moments. I'm finding it difficult to form the likelihood function.  This is the approach I took to work out the quartiles, which I believe is not correct; quartile 1 below:    And this is what I have so far for the likelihood function:    Any help would be truly appreciated! Thank you.
16711
The help pages in R assume I know what those numbers mean, but I don't. I'm trying to really intuitively understand every number here. I will just post the output and comment on what I found out. There might (will) be mistakes, as I'll just write what I assume. Mainly I'd like to know what the t-value in the coefficients mean, and why they print the residual standard error.   Call: lm(formula = iris$Sepal.Width ~ iris$Petal.Width)  Residuals:      Min       1Q   Median       3Q      Max  -1.09907 -0.23626 -0.01064  0.23345  1.17532    This is a 5-point-summary of the residuals (their mean is always 0, right?). The numbers can be used (I'm guessing here) to quickly see if there are any big outliers. Also you can already see it here if the residuals are far from normally distributed (they should be normally distributed).  Coefficients:                  Estimate Std. Error t value Pr(&gt;|t|)     (Intercept)       3.30843    0.06210  53.278  &lt; 2e-16 *** iris$Petal.Width -0.20936    0.04374  -4.786 4.07e-06 *** --- Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1    Estimates $\hat{\beta_i}$, computed by least squares regression. Also, the standard error is $\sigma_{\beta_i}$. I'd like to know how this is calculated.  I have no idea where the t-value and the corresponding p-value come from. I know $\hat{\beta}$ should be normal distributed, but how is the t-value calculated?  Residual standard error: 0.407 on 148 degrees of freedom   $\sqrt{ \frac{1}{n-p} \epsilon^T\epsilon }$, I guess. But why do we calculate that, and what does it tell us?  Multiple R-squared: 0.134,  Adjusted R-squared: 0.1282    $ R^2 = \frac{s_\hat{y}^2}{s_y^2} $, which is $ \frac{\sum_{i=1}^n (\hat{y_i}-\bar{y})^2}{\sum_{i=1}^n (y_i-\bar{y})^2} $. The ratio is close to 1 if the points lie on a straight line, and 0 if they are random.  What is the adjusted R-squared?  F-statistic: 22.91 on 1 and 148 DF,  p-value: 4.073e-06    F and p for the whole model, not only for single $\beta_i$s as previous. The F value is $ \frac{s^2_{\hat{y}}}{\sum\epsilon_i} $. The bigger it grows, the more unlikely it is that the $\beta$'s do not have any effect at all.
8749
Inspired by Peter Donnelly's talk at , in which he discusses how long it would take for a certain pattern to appear in a series of coin tosses, I created the following script in R. Given two patterns 'hth' and 'htt', it calculates how long it takes (i.e. how many coin tosses) on average before you hit one of these patterns.  coin &lt;- c('h','t')  hit &lt;- function(seq) {     miss &lt;- TRUE     fail &lt;- 3     trp  &lt;- sample(coin,3,replace=T)     while (miss) {         if (all(seq == trp)) {             miss &lt;- FALSE         }         else {             trp &lt;- c(trp[2],trp[3],sample(coin,1,T))             fail &lt;- fail + 1         }     }     return(fail) }  n &lt;- 5000 trials &lt;- data.frame("hth"=rep(NA,n),"htt"=rep(NA,n))  hth &lt;- c('h','t','h') htt &lt;- c('h','t','t')  set.seed(4321) for (i in 1:n) {     trials[i,] &lt;- c(hit(hth),hit(htt))     } summary(trials)   The summary statistics are as follows,        hth             htt          Min.   : 3.00   Min.   : 3.000    1st Qu.: 4.00   1st Qu.: 5.000    Median : 8.00   Median : 7.000    Mean   :10.08   Mean   : 8.014    3rd Qu.:13.00   3rd Qu.:10.000    Max.   :70.00   Max.   :42.000    In the talk it is explained that the average number of coin tosses would be different for the two patterns; as can be seen from my simulation. Despite watching the talk a few times I'm still not quite getting why this would be the case. I understand that 'hth' overlaps itself and intuitively I would think that you would hit 'hth' sooner than 'htt', but this is not the case. I would really appreciate it if someone could explain this to me.
59606
I am creating a component that aims to calculate the average and variance of a metric associated with events happening during time but with a limited internal memory.  Imagine that the events are visitors entering in a shop and the metric is their age.  During time, my component receives events with the age of each visitor. I don't want my component to memorize the history of each ages. Ideally, I would like a light component storing only: the average A, the variance V and the number of events N.  After each event with age E, I want to update those three values :  N&lt;=N+1 A&lt;=(A*N+E)/(N+1) V&lt;=???   What for V? I am thinking of something like :  V&lt;=(V*N+(E-A)^2)/(N+1)   I know it is not exact as my previous V is using the old A which is no more the average.   Q1 - Is there an exact formula? Q2 - If not, is my proposal a good estimate? Is it biased? Will it converge correctly when N increases? Q3 - Is there a better formula?
42379
If so, what? If not, why not?  For a sample on the line, the median minimizes the total absolute deviation. It would seem natural to extend the definition to R2, etc., but I've never seen it. But then, I've been out in left field for a long time.
99479
We are typically introduced to method of moments estimators by "equating population moments to their sample counterpart" until we have estimated all of the population's parameters; so that, in the case of a normal distribution, we would only need the first and second moments because they fully describe this distribution.   $E(X) = \mu \implies \sum_{i=1}^n X_i/n = \bar{X}$  $E(X^2) = \mu^2 + \sigma^2 \implies \sum_{i=1}^n X_i^2/n$  And we could theoretically compute up to $n$ additional moments as:  $E(X^r) \implies \sum_{i=1}^nX_i^r /n$  How can I build intuition for what moments really are? I know they exist as a concept in physics and in mathematics, but I find neither directly applicable, especially because I don't know how to make the abstraction from the mass concept to a data point. The term seems to be used in a specific way in statistics, which differs from usage in other disciplines.  What characteristic of my data determines how many ($r$) moments there are overall?
117961
I know this is a fairly specific R question, but I may be thinking about proportion variance explained, $R^2$, incorrectly. Here goes.  I'm trying to use the R package randomForest. I have some training data and testing data. When I fit a random forest model, the randomForest function allows you to input new testing data to test. It then tells you the percentage of variance explained in this new data. When I look at this, I get one number.  When I use the predict() function to predict the outcome value of the testing data based on the model fit from the training data, and I take the squared correlation coefficient between these values and the actual outcome values for the testing data, I get a different number. These values don't match up.   Here's some R code to demonstrate the problem.  # use the built in iris data data(iris)  #load the randomForest library library(randomForest)  # split the data into training and testing sets index &lt;- 1:nrow(iris) trainindex &lt;- sample(index, trunc(length(index)/2)) trainset &lt;- iris[trainindex, ] testset &lt;- iris[-trainindex, ]  # fit a model to the training set (column 1, Sepal.Length, will be the outcome) set.seed(42) model &lt;- randomForest(x=trainset[ ,-1],y=trainset[ ,1])  # predict values for the testing set (the first column is the outcome, leave it out) predicted &lt;- predict(model, testset[ ,-1])  # what's the squared correlation coefficient between predicted and actual values? cor(predicted, testset[, 1])^2  # now, refit the model using built-in x.test and y.test set.seed(42) randomForest(x=trainset[ ,-1], y=trainset[ ,1], xtest=testset[ ,-1], ytest=testset[ ,1])
119622
Suppose I have a function such like:  f &lt;- function(x){   exp(x) / (1 + exp(x)) }   it's supposed to work for any real value of x, but actually it returns NaN when x is 710 or larger. I'm wondering what's a proper way to handle this issue. I realize it's easy to make it just return 1, but maybe it's not a good behavior from a statistician's point. Does somebody have some comments or suggestions?
9526
I need to generate random numbers following Normal distribution within the interval $(a,b)$. (I am working in R.)  I know the function rnorm(n,mean,sd) will generate random numbers following normal distribution,but how to set the interval limits within that? Is there any particular R functions available for that?
45552
Example code:  (pc.cr &lt;- princomp(USArrests))   summary(pc.cr) loadings(pc.cr)  ## note that blank entries are small but not zero   I am getting different outputs from each, and I am not sure I understand what the difference is.  Here is the output:  &gt; summary(pc.cr) Importance of components:                            Comp.1      Comp.2      Comp.3       Comp.4 Standard deviation     82.8908472 14.06956001 6.424204055 2.4578367034 Proportion of Variance  0.9655342  0.02781734 0.005799535 0.0008489079 Cumulative Proportion   0.9655342  0.99335156 0.999151092 1.0000000000   &gt; loadings(pc.cr)  ## note that blank entries are small but not zero  ...                 Comp.1 Comp.2 Comp.3 Comp.4 SS loadings      1.00   1.00   1.00   1.00 Proportion Var   0.25   0.25   0.25   0.25 Cumulative Var   0.25   0.50   0.75   1.00   P.S: how can I get access to the table created by summary(pc.cr)?? (I can't seem to find it in str.)
75191
In the book An introduction to statistical learning with Applications in R, when testing the relationship between the Response and the Predictors using null hypothesis, it gives a formula: $$F = \frac{(TSS - RSS)/p}{RSS/(n-p-1)}$$ where $$RSS = \sum_{i=1}^{n} (y_i-\hat{y_i})^2$$ $$\hat{y}=\hat{\beta_0} + \hat{\beta_1}x_1 + \dotsb + \hat{\beta_p}x_p$$ $$TSS = \sum_{i=1}^{n} (y_i-\bar{y})^2$$  I googled the F test and learned that the degree of freedom in F statistic means number of values in the final calculation of a statistic that are free to vary.(, )   But, here I cannot get why in this formula the degree of freedom are p and (n-p-1)?