Question about a statistics problem
이전 댓글 표시
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개)
Matt J
2020년 10월 16일
I think you should just do,
u = rand(sample_size,loop_times);
number_of_success = sum(u<trueVal)
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!