population variance (Equivalent of var.p command in excel in Matlab)
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
neda fathi
le 22 Oct 2022
Commenté : Star Strider
le 22 Oct 2022
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 commentaires
Réponse acceptée
Star Strider
le 22 Oct 2022
Modifié(e) : Star Strider
le 22 Oct 2022
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 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!