Question about a statistics problem
Show older comments
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
on 16 Oct 2020
Answers (1)
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 Loops and Conditional Statements 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!