Stats for Engineers Lecture 7 Confidence intervals During component manufacture, a random sample of 500 are weighed each day, and each day a 95% confidence interval is calculated for the mean weight đ of the components. On the first day we obtain a confidence interval for the mean weight of 0.998 ± 1. 2. 3. 4. On 95% of days, the mean weight đ is in the range 0.998 ± 0.003 Kg 95% of the daily sample means lie in the range 0.998 ± 0.003 Kg On 95% of days the calculated confidence interval contains đ 95% of the components have weights in the range 0.998 ± 0.003 Kg 74% 13% 13% 0% 1 2 3 4 Recap: Confidence Intervals for the mean Random sample đ1 , đ2 , … đđ from đ đ, đ 2 , where đ 2 is known [or large data sample so can estimate it accurately, đ 2 ≈ đ 2 ] but đ is unknown. We want a confidence interval for đ. Reminder: đ ∼ đ đ2 đ, đ ) With probability 0.95, a Normal random variables lies within 1.96 standard deviations of the mean. 95% of the time expect đ in đ ± 1.96 P=0.025 P=0.025 đ2 đ đ2 đ2 đ = đ ± 1.96 ⇒đ=đ± đ đ A 95% confidence interval for đ if we measure a sample mean đ and already know đ 2 is đ2 đ ± 1.96 đ Confidence interval interpretation During component manufacture, a random sample of 500 are weighed each day, and each day a 95% confidence interval is calculated for the mean weight đ of the components. On the first day we obtain a confidence interval for the mean weight of 0.998 ± đ∼đ đ2 đ, đ , so 95% of the time the sample mean đ lies in đ = đ ± 1.96 here đ ± 1.96 đ2 đ = đ ± 0.003 Every day we get a different sample, so different đ. Confidence interval e.g. Day 1: Day 2: Day 3: Day 4: … đ đ đ đ = 0.998 = 0.995 = 1.000 = 0.997 95% of the time, đ is in đ ± 0.003 đ2 đ 0.998 ± 0.003 0.995 ± 0.003 1.000 ± 0.003 0.997 ± 0.003 … ⇒95% of the time, đ is in X ± 0.003 Other answers? On 95% of days, the mean weight is in the range 0.998 ± 0.003 Kg NO - the mean weight đ is assumed to be a constant. It is either in the range or it isn’t – if true for one day it will be true for all days 95% of the components have weights in the range 0.998 ± 0.003 Kg NO – the confidence interval we calculated is for the mean weight, not individual weights (and đ ≠ đ so the mid-point is incorrect) 95% of the daily sample means lie in the range 0.998 ± 0.003 Kg NO – the correct statement is that 95% of the time đ lies in đ ± 0.003 Kg Statement would only be true if on the first day we got đ = đ, which has negligible probability In general can use any confidence level, not just 95%. 95% confidence level has 5% in the tails, i.e. p=0.05 in the tails. In general to have probability đ in the tails; for two tail, đ/2 in each tail: A 100 1 − đ % confidence interval for đ if we measure a sample mean đ and already know đ 2 is đ ± đ§ p/2 đ2 đ , where đ đ§ = 1 − đ/2 p/2 E.g. for a 99% confidence interval, we would want đ đ§ = 0.995. Q Two tail versus one tail Does the distribution have two small tails or one? Or are we only interested in upper or lower limits? If the distribution is one sided, or we want upper or lower limits, a one tail interval may be more appropriate. 95% Two tail 95% One tail P=0.05 P=0.025 P=0.025 Example You are responsible for calculating average extraurban fuel efficiency figures for new cars. You test a sample of 100 cars, and find a sample mean of đ = 55.40mpg. The standard deviation is đ = 1.2 mpg. What is the 95% confidence interval for the average fuel efficiency? Answer: Sample size if đ = 100 and 95% confidence interval is đ ± 1.96 đ2 . đ 1.22 ⇒ 55.4 ± 1.96 = 55.4 ± 0.235 100 i.e. mean đ in 55.165 to 55.63 mpg at 95% confidence Confidence interval Given the confidence interval just constructed, it is correct to say that approximately 95% of new cars will have efficiencies between 55.165 and 55.63 mpg? Question from Derek Bruff 1. 2. 3. 4. YES – high confidence YES – low confidence NO – high confidence NO – low confidence NO: đ = 1.2mpg given in the question is the standard deviation of the individual car efficiencies (i.e. expect new cars in a range ±1.96đ). The confidence interval we calculated is the range we expect the mean efficiency to lie in (much smaller range). 0% 1 0% 0% 2 3 0% 4 10 Countdown Example: Polling A sample of 1000 random voters were polled, with 350 saying they will vote for the Conservatives and 650 saying another party. What is the 95% confidence interval for the Conservative share of the vote? Answer: this is Binomial data, but large đ = 1000 so can approximate as Normal Random variable đ is the number voting Conservative, đ ∼ đ đ, đ 2 ) 350 Take variance from the Binomial result with đ ≈ 1000 = 0.35 đ 2 = đđ 1 − đ ≈ 1000 × 0.35 × 1 − 0.35 = 227.5 ⇒ đ = 227.5 ≈ 15.1 95% confidence interval for the total votes is 350 ± 1.96đ = 350 ± 1.96 × 15.1 = 350 ± 29.6 ⇒ 95% confidence interval for the fraction of the votes is 350 ± 29.6 ≈ 0.35 ± 0.03 1000 i.e. ±3% confidence interval Example – variance unknown A large number of steel plates will be used to build a ship. A sample of ten are tested and found to have sample mean đ = 2.13kg and sample variance đ 2 = 0.25 kg 2 . What is the 95% confidence interval for the mean weight đ? Reminder: Sample Variance: đ 2 = 2 2 đ đđ −đđ đ−1 Normal data, variance unknown Random sample đ1 , đ2 , … đđ from đ đ, đ 2 , where đ 2 and đ are both unknown. Want a confidence interval for đ, using observed sample mean and variance. When we know the variance: use đ§ = đ−đ đ/ đ which is normally distributed Remember: đ ∼ đ đ, But don’t know đ 2 , so have to use sample estimate đ 2 đ−đ đ When we don’t know the variance: use đĄ = đ / (with đ − 1 d.o.f) which has a t-distribution Sometimes more fully as “Student’s t-distribution” Wikipedia đ2 đ đ =đ−1=1 đ =đ−1=5 đ = đ − 1 = 50 Normal t-distribution For large đ the t-distribution tends to the Normal - in general broader tails Confidence Intervals for the mean 2 If đ is known, confidence interval for đ is đ − đ§ đ2 đ to đ + đ§ đ2 , đ where đ§ is obtained from Normal tables (z=1.96 for two-tailed 95% confidence limit). If đ 2 is unknown, we need to make two changes: (i) Estimate đ 2 by đ 2 , the sample variance; (ii) replace z by đĄđ−1 , the value obtained from t-tables, The confidence interval for đ if we measure a sample mean đ and 2 sample variance đ is: đ − đĄđ−1 đ 2 đ to đ + đĄđ−1 đ 2 . đ t-tables give đĄđ for different values Q of the cumulative Student's t-distributions, and for different values of đ đ đĄđ ) = đĄđ đđ đĄ đđĄ −∞ The parameter đ is called the number of degrees of freedom. (when the mean and variance are unknown, there are đ − 1 degrees of freedom to estimate the variance) Q đĄđ Q đĄđ For a 95% confidence interval, we want the middle 95% region, so Q = 0.975 (0.05/2=0.025 in both tails). Similarly, for a 99% confidence interval, we would want Q = 0.995. t-distribution example: A large number of steel plates will be used to build a ship. A sample of ten are tested and found to have sample mean đ = 2.13kg and sample variance đ 2 = 0.25 kg 2 . What is the 95% confidence interval for the mean weight đ? Answer: From t-tables, đ = đ − 1 = 9 for Q = 0.975 đĄ9 = 2.2622. 95% confidence interval for đ is: đ = đ ± đĄđ−1 đ 2 đ 0.252 ⇒ đ = 2.13 ± 2.2622 = 2.13 ± 0.18 kg 10 i.e. 1.95 to 2.31 Confidence interval width We constructed a 95% confidence interval for the mean using a random sample of size n = 10 with sample mean đ = 2.13kg . Which of the following conditions would NOT probably lead to a narrower confidence interval? Question adapted from Derek Bruff 1. 2. 3. 4. If you decreased your confidence level If you increased your sample size If the sample mean was smaller If the population standard deviation was smaller 48% 33% 14% 5% 1 2 3 4 Confidence interval width We constructed a 95% confidence interval for the mean using a random sample of size n = 10 with sample mean đ = 2.13kg . Which of the following conditions would NOT probably lead to a narrower confidence interval? 95% confidence interval for đ is:đ = đ ± đĄđ−1 đ 2 ; đ width is 2đĄđ−1 đ 2 đ Decrease your confidence level? ⇒ larger tail ⇒ smaller đĄđ ⇒ smaller confidence interval Increase your sample size? đ 2 ⇒ đ larger ⇒ smaller confidence interval ( đ and đĄđ−1 both likely to be smaller) Smaller sample mean? ⇒ đ smaller ⇒ just changes mid-point, not width Smaller population standard deviation? ⇒ đ 2 likely to be smaller ⇒ smaller confidence interval Sample size How many random samples do you need to reach desired level of precision? For example, for Normal data, confidence interval for đ is đ ± đĄđ−1 đ 2 . đ Suppose we want to estimate đ to within ±đŋ, where đŋ (and the degree of confidence) is given. Want đŋ = đĄđ−1 đ 2 đ 2 đĄđ−1 đ 2 ⇒đ= đŋ2 Need: - Estimate of đ 2 (e.g. previous experiments) - Estimate of đĄđ−1 . This depends on n, but not very strongly. e.g. take đĄđ−1 = 2.1 for 95% confidence. Rule of thumb: for 95% confidence, choose đ = 2.12 ×Estimate of variance δ2 Example A large number of steel plates will be used to build a ship. Ten are tested and found to have sample mean weight đ = 2.13kg and sample variance đ 2 = 0.25 kg 2 . How many need to be tested to determine the mean weight with 95% confidence to within ±0.1 kg? Answer: Want đŋ = 0.1kg = đĄđ−1 đ 2 đ Take đĄđ−1 = 2.1 for 95% confidence. 2 đĄđ−1 đ 2 2.12 0.252 ⇒đ= = = 27.6 đŋ2 0.12 i.e. need to test about 28 Number of samples If you need 28 samples for the confidence interval to be ±0.1 kg, approximately how many samples would you need to get a more accurate answer with confidence interval ±0.01 kg? 1. 2. 3. 4. 88.5 280 2800 28000 39% 28% 17% đŋ = đĄđ−1 đŋ ⇒ 10 = đĄđ−1 17% đ 2 đ đ 2 100đ so need 100 × more. i.e. 2800 1. 2 3 4 Linear regression We measure a response variable đĻ at various values of a controlled variable đĨ e.g. measure fuel efficiency đĻ at various values of an experimentally controlled external temperature đĨ 250 200 y 150 100 0 10 20 30 40 x Linear regression: fitting a straight line to the mean value of đĻ as a function of đĨ đĻ = đđĨ + đ đĻ Distribution of đĻ when đĨ = đĨ1 đĨ1 đĨ2 đĨ3 đĨ Regression curve: fits the mean values of the đĻ distributions From a sample of đĻ values at various đĨ, we want to fit the regression curve. e.g. 250 200 y 150 100 0 10 30 20 x 40 Or is it 250 200 y 150 100 0 10 30 20 40 x What do we mean by a line being a ‘good fit’? đĻ Straight line plots Which graph is of the line đĻ = 2đĨ − 4? 1. đĨ 2. 56% 3. 33% 4. 11% 0% 1 2 3 4 Equation of straight line is đĻ = đ + đđĨ Simple model for data: đĻđ = đ + đ đĨđ + đđ Straight line Random error Simplest assumption: đđ ∼ đ 0, đ 2 ) for all đ, and đđ 's are independent - Linear regression model Model is đĻđ = đ + đ đĨđ + đđ Want to estimate parameters a and b, using the data. e.g. - choose đ and đ to minimize the errors Maximum likelihood estimate = least -squares estimate Minimize đđ2 = đ¸= đ đĻđ − đĻđ ) = đ đĻđ − đ − đđĨđ 2 đ Data point Straight-line prediction E is defined and can be minimized even when errors not Normal – least-squares is simple general prescription for fitting a straight line (but statistical interpretation in general less clear) The line đĻ = 4 + 2đĨ has been proposed as a line of best fit for the following four sets of data. For which data set is this line the best fit (minimum đ¸ = đ đđ2 )? Question from Derek Bruff 1. 2. 57% 3. 4. 24% 19% 0% 1 2 3 4 2 đ đđ How to find đ and đ that minimize đ¸ = đđ¸ = đ đĻđ − đ − đđĨđ 2 ? đđ¸ For minimum want đđ = 0 and đđ = 0, see notes for derivation Solution is the least-squares estimates đ and đ: đđĨđĻ and đ = đĻ − đ đĨ đđĨđĨ đ= Sample means Where đĨđ2 đđĨđĨ = đ đĨđ − đ đ đđĨđĻ = đĨđ đĻđ − đ 2 đ đĨđ đ đ đĻđ = đĨđ − đĨ 2 đ = đĨđ − đĨ) đĻđ − đĻ đ Equation of the fitted line is đĻ = đ + đđĨ Note that since đ = đĻ − đ đĨ đĻ = đ + đđĨ = đĻ − đđĨ + đđĨ ⇒ đĻ − đĻ = đ đĨ − đĨ) i.e. đĨ, đĻ) is on the line 250 200 y đĻ 150 100 0 10 20 đĨ x 30 40 Example: The data y has been observed for various values of x, as follows: y x 240 1.6 181 9.4 193 15.5 155 20.0 172 22.0 110 35.5 113 43.0 75 40.5 94 33.0 Fit the simple linear regression model using least squares. Want to fit đĻ = đ + đđĨ 2 đ đĨđ đ đĻđ = 1333.0 2 đ đĻđ = 7053.7, đđĨđĨ = 220549, 220.52 = 7053.7 − 9 đđĨđĻ = 26864 − ⇒đ= đĨđ2 đđĨđĨ = đ đĨđ đĻđ = 26864 = 1651.42 220.50 × 1333.0 = −5794.1 9 đđĨđĻ 5794.5 =− đđĨđĨ 1651.45 = −3.5086 − đ đđĨđĻ = đĨđ đĻđ − đ Answer: n=9 đ đĨđ = 220.5 , đđĨđĻ đ= and đ = đĻ − đ đĨ đđĨđĨ đ đĨđ 2 đ đ đĨđ đ đ đĻđ Answer: đđĨđĻ đ= and đ = đĻ − đ đĨ đđĨđĨ Want to fit đĻ = đ + đđĨ n=9 đ đĨđ = 220.5 , 2 đ đĨđ đ đĻđ đđĨđĨ = 220549, 220.52 = 7053.7 − 9 đđĨđĻ = 26864 − ⇒đ= đđĨđĨ = = 1333.0 2 đ đĻđ = 7053.7, đ đĨđ đĻđ = 1651.42 220.50 × 1333.0 = −5794.1 9 đđĨđĻ 5794.5 =− đđĨđĨ 1651.45 = −3.5086 đ = đĻ − đđĨ 1333.0 220.50 − −3.5086) × = 234.1 9 9 So the fit is approximately đĻ = 234.1 − 3.509đĨ − đ = 26864 Now just need đ = đĨđ2 đđĨđĻ = đĨđ đĻđ − đ đ đĨđ 2 đ đ đĨđ đ đ đĻđ Which of the following data are likely to be most appropriately modelled using a linear regression model? 1. 2. 55% 3. 25% 20% 1 2 3 Quantifying the goodness of the fit Estimating đđ : variance of y about the fitted line Estimated error is: đđ = đĻđ − đĻđ 1 đđ = 0, so the ordinary sample variance of the đđ 's is ∼ đ−1 2 đ đđ In fact, this is biased since two parameters, a and b have been estimated. The unbiased estimate is: đ2 = 1 đ−2 đđ2 = = 1 đ−2 đĻđ − đĻđ đđĻđĻ − đđđĨđĻ đ−2 2 [derivation in notes] Residual sum of squares Which of the following plots would have the greatest residual sum of squares [variance of đĻ about the fitted line]? Question from Derek Bruff 1. 2. 3. 72% 11% 1 17% 2 3 Confidence interval for the slope, b E.g. if you want to see if đ is significantly non-zero Reminder: Normal data with unknown variance, confidence interval for đ is: s X ī t n ī1 2 n to X īĢ t n ī1 s 2 n đ 2 /đ is the estimate of đ 2 /đ, the variance of đ It can be shown that var đ = đ2 , đđĨđĨ estimated by đ2 đđĨđĨ (đ − 2 degrees of freedom). Confidence interval for b is bī¤ ī t n ī 2 īŗī¤ 2 S xx to bī¤ īĢ t n ī 2 īŗī¤ 2 S xx Predictions For given đĨ of interest, what is mean đĻ? 250 200 y Predicted mean value: đĻ = đ + đđĨ. 150 100 0 What is the error bar? It can be shown that var đĻ đĨ = var đ + đđĨ 1 đĨ−đĨ 2 2 =đ + đ đđĨđĨ Confidence interval for mean y at given x đĻ ± đĄđ−2 1 đ2 đ + đĨ−đĨ 2 đđĨđĨ Extrapolation: Often not reliable 10 20 x 30 40