Question about a statistics problem

1 view (last 30 days)
Lingbai Ren
Lingbai Ren on 16 Oct 2020
Commented: Lingbai Ren on 16 Oct 2020
I have done part a to c, got stuck on part d
the description of part a, b, and c is below:
(a) Begin by writing a script that will generate 500 samples drawn from a uniform distribution. We want to implement a true proportion of 0.7 (e.g., 70% of people are in favor of some new law). In the end, you should have an array filled with 1’s and 0’s, where any random number between 0 and 0.7 is assigned a 1, and any random number between 0.7 and 1 is assigned a 0.
(b) Write a script that will compute the proportion of the sample from part (a), which is an estimate of the true proportion. Use it to verify that you did part (a) correctly; the estimate should be fairly close to the true value.
(c) Write a script that computes the standard error of the estimate of the proportion. You must pretend that you don’t know the true proportion, so must use the estimate.
For part d, I was asked to construct a for loop to loop through step a to c 1000 times, each time through, I will take 500 smaples and estimate the proportion. In the end, I will have an array of 1000 estimates. I will also have an array of 1000 associated standard errors of the estimates.
I am assuming we are using nested for loop in this case, but I kept getting "number of success" as single value in script. I attached my code below, please help me to check where I did wrong!
%% (d) Construct a for loop to loop through (a) through (c) 1000 times
clc,clear
loop_times = 1000;
sample_size = 500;
number_of_success = 0; % number of success counter
u = rand(sample_size,1); % Generate 500 samples from a uniform distribution
trueVal = 0.7;
for k = 1:loop_times
for i = 1:sample_size
if u(i)<trueVal && u(i)>0
u(i) = 1; % Assign 1 to numbers between 0 and 0.7
number_of_success = number_of_success + 1;
else
u(i) = 0; % Assign 0 to numbers between 0.7 and 1
end
end
end
estimate_sample_proportion = number_of_success/sample_size;
standard_error = sqrt((estimate_sample_proportion * (1 - estimate_sample_proportion))...
/ sample_size)
  1 Comment
Lingbai Ren
Lingbai Ren on 16 Oct 2020
So, I think what Matt provided helped a lot!
Now I am worrying about another part of the problem:
(f). Construct a second FOR loop script that determines the ratio of the estimates for which the true proportion falls within the 95% confidence interval of each of the estimates. If you’ve done it correctly, you should find that the true proportion was within the confidence interval for approximately 95% of your estimates.
I am a little bit confused what are we trying to find? Is it the ratio of the estimates? And will that be a single value or will be an array? Could anyone goot at Statistics help?
Here is what I got:
%% (d) Construct a for loop to loop through (a) through (c) 1000 times
clc,clear
loop_times = 1000;
sample_size = 500;
% number of success counter
u = rand(sample_size,loop_times); % Generate 500 samples from a uniform distribution
trueVal = 0.7;
number_of_success = sum(u<trueVal);
for k = 1:loop_times
for i = 1:sample_size
if u(i)<trueVal && u(i)>0
u(i) = 1; % Assign 1 to numbers between 0 and 0.7
number_of_success = number_of_success + 1;
else
u(i) = 0; % Assign 0 to numbers between 0.7 and 1
end
end
end
estimate_sample_proportion = number_of_success/loop_times; %array of 1000 estimates
standard_error = sqrt((estimate_sample_proportion .* (1 - estimate_sample_proportion))...
/ loop_times) % array of 1000 standard errors
% (f) Construct a second for loop to determind the ratio of the estimates
for j = 1: length(estimate_sample_proportion)
ratio_of_estimates = (estimate_sample_proportion(j) + 1.96 * standard_error(j)...
+ (estimate_sample_proportion(j) - 1.96 * standard_error(j)))/1
end

Sign in to comment.

Answers (1)

Matt J
Matt J on 16 Oct 2020
I think you should just do,
u = rand(sample_size,loop_times);
number_of_success = sum(u<trueVal)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!