Skip to content

Correlation - #3842

Closed
ghost wants to merge 9 commits into
mainfrom
unknown repository
Closed

ghost wants to merge 9 commits into
mainfrom
unknown repository

Conversation

@ghost

@ghost ghost commented Jul 28, 2017

Copy link
Copy Markdown

No description provided.

Rodrick Sarmento and others added 2 commits July 27, 2017 23:38
- Added KMO and Bartlett Sphericity functions
- Added respective tests functions
- Added tests.csv file with test Dataset
@ghost

ghost commented Jul 28, 2017

Copy link
Copy Markdown
Author

@josef-pkt Just to inform I've done this new PR with correlation.py and respective tests regarding KMO and Bartlett Sphericity tests. Cheers

@ghost

ghost commented Jul 28, 2017

Copy link
Copy Markdown
Author

@josef-pkt Please advise where to put the tests.csv file. The automated checks are failing due to errors like file not found. Cheers

@josef-pkt

Copy link
Copy Markdown
Member

@Sarmentor I'm not sure what the problem with the csv file is, there are several possibilities.

The easiest is to follow our standard pattern, put the file in tests/results and use the full file path

e.g. another test in stats
https://github.com/statsmodels/statsmodels/blob/master/statsmodels/stats/tests/test_mediation.py#L56

    cur_dir = os.path.dirname(os.path.abspath(__file__))
    data = pd.read_csv(os.path.join(cur_dir, 'results', "framing.csv"))

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.001%) to 90.881% when pulling 5008807 on Sarmentor:correlation into a29a596 on statsmodels:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.05%) to 90.836% when pulling 2851788 on Sarmentor:correlation into a29a596 on statsmodels:master.

…on v2 and v3 packages results, regarding decimal floating point values
@coveralls

coveralls commented Jul 28, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.008%) to 90.889% when pulling 2ccdc48 on Sarmentor:correlation into a29a596 on statsmodels:master.

@coveralls

coveralls commented Jul 28, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.0003%) to 84.53% when pulling 33198c7 on Sarmentor:correlation into 82d75bf on statsmodels:master.

@ghost

ghost commented Jul 28, 2017

Copy link
Copy Markdown
Author

@josef-pkt @bashtage Hi! I believe the tests are all ok. I'll be out for the weekend. I'll be back next monday, if you need more feedback from me please leave a message and I'll work on that next week.
Cheers

@ghost

ghost commented Jul 31, 2017

Copy link
Copy Markdown
Author

@josef-pkt Hi! Can you please confirm if this merge will be associated with release 0.9?

@josef-pkt josef-pkt added this to the 0.9 milestone Aug 2, 2017
@josef-pkt

Copy link
Copy Markdown
Member

@Sarmentor Yes, it will be in 0.9 for sure.
The main thing missing is an API review. (I'm still not sure what the return type should be, we don't use named tuples because they are not forward compatible.)

@ghost

ghost commented Aug 21, 2017

Copy link
Copy Markdown
Author

@josef-pkt Hi! Should I close the other two PRs that are now obsolete?

This was referenced Sep 19, 2017
@josef-pkt

Copy link
Copy Markdown
Member

@Sarmentor Do you have a reference that the bartlett_sphericity can be used with spearman correlation? i.e that it has the same distribution of the test statistic.

One confusing thing about it is that it is a test that the covariance matrix is diagonal, so the covariance does not need to be spherical. Stata has the same test statistic and pvalues for the diagonal covariance test.

@ghost

ghost commented Nov 9, 2017

Copy link
Copy Markdown
Author

@josef-pkt Depending on the variable type, different methods to obtain the correlation matrix should be used. In the case of quantitative variables, Pearson correlations have satisfactory results. However, in the case of nominal and ordinal variables, some authors (for example, Marôco, 2011) advocate the use of Spearman correlation. Please note that in the function provided I give the option to choose between Spearman or Pearson correlations. Default option is currently Spearman, however the user can change this.

@josef-pkt

Copy link
Copy Markdown
Member

Is Marôco, 2011 reference the SPSS book in Portuguese?

I don't like adding things where I don't see a theoretical justification or at least has been confirmed by some Monte Carlo studies. The correlation test is for Pearson correlation with a normal distributed sample and might be robust to some distributional deviation from normality. My guess would be that using spearman does not have the correct scaling.

To avoid the issue and to have more flexibility, for example with using outlier robust estimators, I'm thinking of just using the correlation matrix as argument, e.g.
corr_test_spherical(corr, nobs)

I went over the Stata manual and the chapter in Rencher Christensen that they refer to and I'm thinking of adding
cov_test_xxx(cov, ..., nobs, ...) for one sample, spherical, diagonal (which is the same as corr_test_spherical, k-sample equality and a few more.
#2765 (comment)

e.g. the call would then be with a dataframe

cov_test_diagonal(df1.cov(), df1.shape[0])

I would like some extra functions that take the data and call the underlying corr_test or cov_test, but I don't know yet what the design for that should be. I think that's not urgent given that computing various cov/corr is easy with pandas.

@josef-pkt

Copy link
Copy Markdown
Member

Paritally related: for multivariate outlier detection and robust covariance or correlation estimation, I wrote a function that converts some of the other correlation types to be consistent at normality https://github.com/statsmodels/statsmodels/pull/3230/files#diff-0999b4a53db162f8a6150dbeecb638d7R35

The corr_test would have lower requirements because under the null the correlations are zero, so only the neighborhood of zero is relevant.

@josef-pkt

Copy link
Copy Markdown
Member

just a piece of information:
for the fisher transform and testing a single correlation coefficient the variance differs between pearson and spearman, and kendall tau. According to the wikipedia page the variance for spearman z_s is 1.060 / (n - 3)
The Fieller, E. C.; Hartley, H. O.; Pearson, E. S. (1957). reference in Wikipedia also has it for kendall
var(z_K) = 0.437 / (n - 4)
z = tanh^{-1}(r) = artanh(r)
https://academic.oup.com/biomet/article-lookup/doi/10.1093/biomet/44.3-4.470

@ghost

ghost commented Nov 9, 2017

Copy link
Copy Markdown
Author

@josef-pkt Yes, that reference is in Portuguese. Another example might be "COMPARISON OF VALUES OF PEARSON’S AND SPEARMAN’S CORRELATION COEFFICIENTS ON THE SAME SETS OF DATA" by Jan Hauke, Tomasz Kossowski

@josef-pkt

Copy link
Copy Markdown
Member

Hauke, Kossowski is just a comparison of single correlation coefficients. They don't specify which significance test they use, but by default there should be two different versions for the two types of correlations.

In any case, if we use correlation matrix as argument to the function, then it is up to the user to decide what to use. (Right now my guess is that the test overrejects when spearman is used, but I have no guess how strong the distortion might be.)

@josef-pkt

Copy link
Copy Markdown
Member

looks like my guess about overrejection is wrong, at least in the normal case spearman is similar to pearson correlation in maintaining size

>>> cov_test_ = cov_test_diagonal
>>> k_vars = 5
>>> res_mc = [cov_test_(stats.spearmanr(np.random.randn(nobs, k_vars))[0], nobs) 
...           for _ in range(10000)]   
>>> res_mc = np.array(res_mc)
>>> (res_mc[:,1:] <= alphas).mean(0)
array([ 0.0114,  0.0506,  0.0981,  0.1998])

>>> res_mc = [cov_test_(stats.spearmanr(np.random.randn(nobs, k_vars))[0], nobs) 
...           for _ in range(30000)]   
>>> res_mc = np.array(res_mc)
>>> (res_mc[:,1:] <= alphas).mean(0)
array([ 0.00953333,  0.04823333,  0.09943333,  0.1999    ])
>>> alphas
[0.01, 0.05, 0.1, 0.2]

>>> nobs, k_vars
(500, 5)


>>> nobs=100
>>> res_mc = [cov_test_(stats.spearmanr(np.random.randn(nobs, k_vars))[0], nobs) 
...           for _ in range(30000)]   
>>> res_mc = np.array(res_mc)
>>> (res_mc[:,1:] <= alphas).mean(0)
array([ 0.01016667,  0.0498    ,  0.09846667,  0.1961    ])

@josef-pkt

josef-pkt commented Nov 9, 2017 •

Copy link
Copy Markdown
Member

Another MC run, it looks pretty good even with nobs, k_vars = 50, 10

# using spearman rho
cov_test_ = cov_test_diagonal
nobs, k_vars = 50, 10
# normal
t0 = time.time()
res_mc = [cov_test_(stats.spearmanr(np.random.randn(nobs, k_vars))[0], nobs) 
          for _ in range(30000)]    
t1 = time.time()
print()
print('time', t1 - t0)
res_mc = np.array(res_mc)
alphas = [0.001, 0.005, 0.01, 0.05, 0.1, 0.2]
prop_reject = (res_mc[:,1:] <= alphas).mean(0)

print(alphas)
print(prop_reject)
time 38.5162250995636
[0.001, 0.005, 0.01, 0.05, 0.1, 0.2]
[ 0.00126667  0.00543333  0.01046667  0.05036667  0.10133333  0.20196667]
>>> import statsmodels.stats.proportion as smprop
>>> smprop.proportion_confint(0.0054333 * 30000, 30000)
(0.0046014667741415861, 0.0062651332258584127)
>>> smprop.proportion_confint(0.0054333 * 30000, 30000, method='wilson')
(0.0046624338048734592, 0.0063308071530681466)

one more with large samples:
maybe underrejects a bit at low alpha, 0.001, 0.005,

cov_test_ = cov_test_diagonal
nobs, k_vars = 5000, 10
# normal
t0 = time.time()
res_mc = [cov_test_(stats.spearmanr(np.random.randn(nobs, k_vars))[0], nobs) 
          for _ in range(30000)]    
t1 = time.time()
...

time 223.24349999427795
[0.001, 0.005, 0.01, 0.05, 0.1, 0.2]
[ 0.00066667  0.0038      0.00906667  0.04923333  0.0995      0.20113333]

@ghost

ghost commented Nov 10, 2017

Copy link
Copy Markdown
Author

@josef-pkt What do you think? Robust?

@josef-pkt

Copy link
Copy Markdown
Member

Yes, based on the limited MC examples using spearman looks good. Maybe the difference in scaling is not large around zero correlation. I found another article that compares single correlation confidence intervals that compares pearson, spearman and a few others where spearman with the Bartlett version (1.06) does very well. (I only looked at abstract and some graphs)

However, for 0.9 I would prefer to stick to the basic versions that use either corr or cov as argument, and postpone both more API decision and more quality checks to 0.10.

What I'm still not sure about is what the functions should return. My trend is to think of returning some results instance by default. I don't like named tuples because they need to create the instance and class and don't allow adding additional returns in future (not future proof).
(statsmodels has still no consistent pattern for returns in hypothesis tests.)

@josef-pkt josef-pkt mentioned this pull request Dec 6, 2017
6 tasks done
@josef-pkt

Copy link
Copy Markdown
Member

postponed to 0.10 after all
needs decision on how to include this and related functions

@ghost

ghost commented Aug 24, 2020

Copy link
Copy Markdown
Author

@josef-pkt
Hi, is this somewhat forgotten or better options were taken to provide such tests in the package?

Best

@josef-pkt

Copy link
Copy Markdown
Member

I added sphericity and similar tests for covariance patterns in a simple way in #6693

KMO is still missing, and I haven't looked at the details again. I guess it will go in statsmodels.multivariate to support the statistical methods there, e.g. FA, and not in statsmodels.stats as standalone.

I'm still working my way around stats in several rounds.

@josef-pkt josef-pkt mentioned this pull request Feb 17, 2022
16 of 20 tasks
@josef-pkt josef-pkt modified the milestones: 0.9, 0.14 Feb 17, 2022
@ghost ghost closed this by deleting the head repository May 4, 2024
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants