population variance (Equivalent of var.p command in excel in Matlab)
조회 수: 7 (최근 30일)
이전 댓글 표시
I have a time series data of 5 variables ( data is attached) and I grouped them based on month and year and take variance of each month by following code:
% reading daily data
data1=csvread('ffm.csv',1,0);
% extracting data into a MATLAB Table variable
T=table();
T.year=floor(data1(:,1)/10000);
T.mm=floor((data1(:,1)-T.year*10000)/100);
T.dd=data1(:,1)-T.year*10000-T.mm*100;
T.value=data1(:,2:6);
% compute monthly variance from daily data
v=[];
v=grpstats(T,{'year','mm'},'var');
However, this code by simply 'var' command in the last line of the code, gives the sample variance, but I need to obtain the population variance . The difference between population and sample variance is explained in the below link:
https://www.automateexcel.com/stats/var-p-vs-var-s/#:~:text=The%20VAR.,a%20sample%20of%20a%20populate.
댓글 수: 0
채택된 답변
Star Strider
2022년 10월 22일
편집: Star Strider
2022년 10월 22일
Try something like this —
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1165263/ffm.csv', 'VariableNamingRule','preserve')
T1.Var1 = datetime(string(T1.Var1), 'InputFormat',"yyyyMMdd"); % Create 'daterime' Array
TT1 = table2timetable(T1); % Convert To 'timetable'
popvar = @(x) sum((x - mean(x)).^2)/numel(x); % Population Variance
TT1 = retime(TT1, 'monthly', @(x)popvar(x)) % Monthly Variances For Each VAriable
NOTE — MATLAB calculates the ‘sample variance’ referred to in that link by default, as noted in the var documentation section on More About. So, this (using ‘popvar’) will produce the correct result.
.
댓글 수: 2
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!