library(IS606)
## 
## Welcome to CUNY IS606 Statistics and Probability for Data Analytics 
## This package is designed to support this course. The text book used 
## is OpenIntro Statistics, 3rd Edition. You can read this by typing 
## vignette('os3') or visit www.OpenIntro.org. 
##  
## The getLabs() function will return a list of the labs available. 
##  
## The demo(package='IS606') will list the demos that are available.
## 
## Attaching package: 'IS606'
## The following object is masked from 'package:utils':
## 
##     demo
library(StMoSim)
## Warning: package 'StMoSim' was built under R version 3.2.3
## Loading required package: RcppParallel
## Warning: package 'RcppParallel' was built under R version 3.2.3
## Loading required package: Rcpp
## Warning: package 'Rcpp' was built under R version 3.2.3

4.4 Heights of adults. Researchers studying anthropometry collected body girth measurements and skeletal diameter measurements, as well as age, weight, height and gender, for 507 physically active individuals. The histogram below shows the sample distribution of heights in centimeters.

Min 147.2 Q1 163.8 Median 170.3 Mean 171.1 SD 9.4 Q3 177.8 Max 198.1

(a) What is the point estimate for the average height of active individuals? What about the median? (See the next page for parts (b)-(e).)

i. Point estimate (mean) - 170.3 cms.
ii. Median - 171.1 cms.

(b) What is the point estimate for the standard deviation of the heights of active individuals? What about the IQR?

i.  SD = 9.4 cms.
ii. IQR = Q3 - Q1 = 177.8 - 163.8 = 14      

(c) Is a person who is 1m 80cm (180 cm) tall considered unusually tall? And is a person who is 1m 55cm (155cm) considered unusually short? Explain your reasoning.

It depends on what is considered unusual. 180 cms. is above Q3 (75th percentile) of of the point sample and 155 cms. is below Q1 (25th percentile). If 'unusual' means above the Q3 percentile or below the Q1 percentile, then both data fit the definition. If 'unusual' means 2 standard deviations from the mean, then both data are NOT unusual since both are within 1 standard deviation of the mean. But since this is only a point sample of the entire population, no conclusions should be drawn about the population data. 

(d) The researchers take another random sample of physically active individuals. Would you expect the mean and the standard deviation of this new sample to be the ones given above? Explain your reasoning.

No, the mean and standard deviation is likely to be different. Since the random sample is another set of data from a variable population of active individuals, the mean and the standard deviation should be different and the difference is a function of the variability of the population data.  

(e) The sample means obtained are point estimates for the mean height of all active individuals, if the sample of individuals is equivalent to a simple random sample. What measure do we use to quantify the variability of such an estimate (Hint: recall that SD¯ x = pn)? Compute this quantity using the data from the original sample under the condition that the data are a simple random sample.

   SD(mean of sample X) = SD of sample X/sqrt of sample n 
                        = 9.4 / SQRT(507)
                        = 0.42
                        
                        

4.14 Thanksgiving spending, Part I. The 2009 holiday retail season, which kicked off on November 27, 2009 (the day after Thanksgiving), had been marked by somewhat lower self-reported consumer spending than was seen during the comparable period in 2008. To get an estimate of consumer spending, 436 randomly sampled American adults were surveyed. Daily consumer spending for the six-day period after Thanksgiving, spanning the Black Friday weekend and Cyber Monday, averaged $84.71. A 95% confidence interval based on this sample is ($80.31, $89.11). Determine whether the following statements are true or false, and explain your reasoning.

(a) We are 95% confident that the average spending of these 436 American adults is between $80.31 and $89.11.

FALSE; Confidence interval is about the population and not just the sample.

(b) This confidence interval is not valid since the distribution of spending in the sample is right skewed.

FALSE; A large sample size of 436 randomly sampled American adults make up for the sample distribution to be rightly skewed.

(c) 95% of random samples have a sample mean between $80.31 and $89.11.

FALSE; a different sample may have a different range.

(d) We are 95% confident that the average spending of all American adults is between $80.31 and $89.11.

TRUE;

(e) A 90% confidence interval would be narrower than the 95% confidence interval since we don’t need to be as sure about our estimate.

TRUE; The smaller the confidence interval the narrower the interval.

(f) In order to decrease the margin of error of a 95% confidence interval to a third of what it is now, we would need to use a sample 3 times larger.

FALSE; Margin of error = z * SE = z * (s/sqrt(n)) We need the sample size to be 9 times larger to decrease margin of error by 3

(g) The margin of error is 4.4.

TRUE

4.24 Gifted children, Part I. Researchers investigating characteristics of gifted children collected data from schools in a large city on a random sample of thirty-six children who were identified as gifted children soon after they reached the age of four. The following histogram shows the distribution of the ages (in months) at which these children first counted to 10 successfully. Also provided are some sample statistics.

(a) Are conditions for inference satisfied?

  1. Independence; no; since sample was collected from a limited geographical location, gifted children may be all be coming from a higher socio-economic background and not enough samples may have been gathered from poorer communinities.
  2. sample size are >= 30; yes; 36
  3. distribution not overly skewed; yes distribution is bi-modal and not skewed
  4. assumption is that 36 is <10% of the population of gifted children in a big city

(b) Suppose you read online that children first count to 10 successfully when they are 32 months old, on average. Perform a hypothesis test to evaluate if these data provide convincing evidence that the average age at which gifted children first count to 10 successfully is less than the general average of 32 months. Use a significance level of 0.10.

 H(null): u = 32 months
 H(alternative): u < 32 months
 significance level = 0.10

(c) Interpret the p-value in context of the hypothesis test and the data.

   z = (30.69 - 32) / 4.31 = -0.30
   pnorm(-0.30) = 0.38
   p-value = 0.38
   
   if the null hypothesis is true, the probability of observing a sample mean of 30.69 is 0.38. This is greater than significance 0.10 which means we cannot reject the null hypothesis.
   

(d) Calculate a 90% confidence interval for the average age at which gifted children first count to 10 successfully.

     SE = 4.31/SQRT(36) = 0.72
     30.87 +/- (z-score for 90% confidence interval * SE)
     30.69 +/- (1.645 * 0.72) = (29.51, 31.87)
   

(e) Do your results from the hypothesis test and the confidence interval agree? Explain.

No they don’t. The hypothesis shows that there is a high probability of observing the sample mean (0.38). But the sample mean is not within the confidence interval (since we chose a narrow 90% confidence interval). For a 90% confidence, 10% of sample means are not captured in the interval.

4.26 Gifted children, Part II. Exercise 4.24 describes a study on gifted children. In this study, along with variables on the children, the researchers also collected data on the mother’s and father’s IQ of the 36 randomly sampled gifted children. The histogram below shows the distribution of mother’s IQ. Also provided are some sample statistics.

n 36 min 101 mean 118.2 sd 6.5 max 131

(a) Perform a hypothesis test to evaluate if these data provide convincing evidence that the average IQ of mothers of gifted children is different than the average IQ for the population at large, which is 100. Use a significance level of 0.10.

 H(null): u = 100
 H(alternative): u > 100
 significance level = 0.10

   z = (118.2 - 100) / 6.5 = 2.8
   pnorm(2.8) = 0.997
   p-value = 0.003
   
   If the null hypothesis is true, the probability of observing a sample mean of 118.2 is only 0.003. This is a lot less than the significance value of 0.10. Thus we can reject the null hypothesis and say that for this population (mother's of gifted children), their IQ is greater than the general population.
   

(b) Calculate a 90% confidence interval for the average IQ of mothers of gifted children.

     SE = 6.5/SQRT(36) = 1.08
     118.2 +/- (z-score for 90% confidence interval * SE)
     118.2 +/- (1.645 * 1.08) = (116.42, 119.98)
     The general population mean of 100 is outside the 90% confidence interval for the mean of the IQ of Mom's with gifted kids.

(c) Do your results from the hypothesis test and the confidence interval agree? Explain.

 Yes they do. The p-value shows a big difference between significance level and p-value. This means that there is only a 0.003 chance that the sample mean is valid for the general population and we can resoundly reject the null hypothesis. The confidence interval for the IQ of Mom's with gifted kids is also way off the population mean of 100. This means that with 90% confidence, we can reject the null hypothesis. 

4.34 CLT. Define the term “sampling distribution” of the mean, and describe how the shape, center, and spread of the sampling distribution of the mean change as sample size increases.

 Sampling distribution of the mean is when we take random, independent samples of a constant sample size n. The graph showing the distribution of the values of the mean from all the samples taken is called the sampling distriubtion of the mean. The sampling distribution of the mean obeys the Central Limit Theorem in that it has a normal distirubtion (given sample size >= 30, and not overly skewed) and would tend towards the mean (sread becomes narrower) as sample size increases.
 
 As the sample size increases -  
 
  1. shape - closer to a normal distribution (normal curve)
  2. center - becomes taller (increase frequency of values that is close to the true population mean)
  3. spread - becomes narrower
  
  

4.40 CFLBs. A manufacturer of compact fluorescent light bulbs advertises that the distribution of the lifespans of these light bulbs is nearly normal with a mean of 9,000 hours and a standard deviation of 1,000 hours.

(a) What is the probability that a randomly chosen light bulb lasts more than 10,500 hours?

        z = (10,500 - 9,000)/1,000 = 1.5
        1 - pnorm(1.5) = 0.07 

(b) Describe the distribution of the mean lifespan of 15 light bulbs.

Since the population distribution is nearly normal N(9000, 1000) is nearly normal, the SE for the sample is 1000/SQRT(15) = 258.    
        

(c) What is the probability that the mean lifespan of 15 randomly chosen light bulbs is more than 10,500 hours?

     z= (10,500 - 9,000)/258 = 5.81
     1 - pnorm(5.81) = 0 (no chance)

(d) Sketch the two distributions (population and sampling) on the same scale.

normalPlot(mean = 9000, sd = 1000)

normalPlot(mean = 9000, sd = 258)

(e) Could you estimate the probabilities from parts (a) and (c) if the lifespans of light bulbs had a skewed distribution?

No. The z values were based on the assumption of a normal distribution.

4.48 Same observation, different sample size. Suppose you conduct a hypothesis test based on a sample where the sample size is n = 50, and arrive at a p-value of 0.08. You then refer back to your notes and discover that you made a careless mistake, the sample size should have been n = 500. Will your p-value increase, decrease, or stay the same? Explain.

 As the sample size increases the spread becomes narrower and the sd deviation becomes smaller. A smaller SD results in a larger z-score which decreases the p-value. That is, if the null hypothesis is true, as you increase the sample size, you stregthen the case that the null hypothesis is true (p-value decreases).