You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have two binary variables for which I want to see how they relate to each other, so I tried correlations and a simple regression and found the following error when I tried to get the regresison summary:
Can you provide the data, or an artificial dataset that raises the same error?
There is too little information to figure out what's going on, and I haven't seen an error in this part yet or in a while.
This "should" work. What's the shape of your df?
to contingency table:
scipy.stats has a chisquare test for independence in contingency tables, and fisher_exact. There are some additional tests for proportion in the current master of statsmodels.
There are a few more tests, like tests for marginal homogeneity.
Note:if your published is binary, then you have a model that would be better analyzed with discrete Logit or Probit, or the corresponding GLM family.
my suspect was that formula converts endog to a 2-dimensional dummy variable.
This happens if endog is an array of strings, but not if endog is an array of integers
import numpy as np
import statsmodels.api as sm
import statsmodels.formula.api as smf
nobs = 20
x = np.repeat([0,1], [10, 10])
y = np.random.randint(0, 2, nobs).astype(str)
res = smf.ols("y ~ x", {'y':y, 'x':x}).fit()
print res.model.endog
I haven't looked at the OLS, Logit version before:
For two sample test of proportion, using Logit, OLS, ..., the proportion tests should be almost the same in large samples. In small samples, discreteness (binomial), different transformations, whether t or normal/chisquare distribution is used, and the degrees of freedom will result in some differences.
bool doesn't work, you need to convert your df to integers, astype(int)
there is no xtabs is as a single step function, xtabs contingency table creation can be done with pandas (AFAIK), but the test statistics are in scipy.stats or statsmodels.
I haven't looked at the contingency table class in scipy.stats recently, but IIRC it uses the count table as input.
So, currently it's a two step process.
related: mean of the booleans give you the proportion
the statsmodels.stats functions take proportion and nobs.
statsmodels.sourceforge.net/devel/generated/statsmodels.stats.proportion.proportions_chisquare.html
I haven't yet thought much about a more user friendly interface for proportions.
I'm not a user for these things and only know how to use numpy directly. It would be possible to add a formula based wrapper around pandas and the stats functions.
I opened #879 (but I won't have time to work on it, especially the pandas and patsy parts which I don't know so well)
From the statistical backend:
We have many but not all tests that are in the SPSS description. Part of the work is to just collect them together as methods to a class and write some nice "summary()" tables.
One question for the implementation is that 2x2, 2xk, 2x2xk and arbitrarily sized contingency tables will have different options/tests available, many tests are for 2x2 only, and some like chisquare or kendall_tau work for any contingency tables.
2x2 and 2xk are largely covered by statsmodels.stats.proportions (once that has a wrapper/summary interface).
To continue on topic: I would vote that using a formula and bool/string-variables for the LHS catches such cases with a helpful error message ("Can't run regressions with bool on LHS: use '(%s)' in the formula" % offending_variable).
Maybe add a new transformation in patsy like "as_int(...)" which would transform bool and strings (if a variable is string and has only two values) to int and error on all other cases?
On the other hand, sometimes it's convenient to use factor levels as LHS...
Activity
josef-pkt commentedon Jun 19, 2013
Can you provide the data, or an artificial dataset that raises the same error?
There is too little information to figure out what's going on, and I haven't seen an error in this part yet or in a while.
This "should" work. What's the shape of your df?
to contingency table:
scipy.stats has a chisquare test for independence in contingency tables, and fisher_exact. There are some additional tests for proportion in the current master of statsmodels.
There are a few more tests, like tests for marginal homogeneity.
Note:if your
publishedis binary, then you have a model that would be better analyzed with discrete Logit or Probit, or the corresponding GLM family.josef-pkt commentedon Jun 19, 2013
my suspect was that formula converts endog to a 2-dimensional dummy variable.
This happens if endog is an array of strings, but not if endog is an array of integers
the simplest way, that I know, to convert string levels to integer levels is with np.unique
josef-pkt commentedon Jun 19, 2013
I haven't looked at the OLS, Logit version before:
For two sample test of proportion, using Logit, OLS, ..., the proportion tests should be almost the same in large samples. In small samples, discreteness (binomial), different transformations, whether t or normal/chisquare distribution is used, and the degrees of freedom will result in some differences.
jankatins commentedon Jun 19, 2013
Sorry, should have send that in the intial report:
jankatins commentedon Jun 19, 2013
Re contingency table:
So nothing like
sm.xtabs("published ~ connection", df)exists yet, which would output per default a contingency table and a chi squared test result?josef-pkt commentedon Jun 19, 2013
bool doesn't work, you need to convert your df to integers,
astype(int)there is no xtabs is as a single step function, xtabs contingency table creation can be done with pandas (AFAIK), but the test statistics are in scipy.stats or statsmodels.
I haven't looked at the contingency table class in scipy.stats recently, but IIRC it uses the count table as input.
So, currently it's a two step process.
related: mean of the booleans give you the proportion
the statsmodels.stats functions take proportion and nobs.
statsmodels.sourceforge.net/devel/generated/statsmodels.stats.proportion.proportions_chisquare.html
I haven't yet thought much about a more user friendly interface for proportions.
suggestions and pull requests are welcome.
josef-pkt commentedon Jun 19, 2013
http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.chi2_contingency.html
josef-pkt commentedon Jun 19, 2013
did you mean this http://www.inside-r.org/packages/cran/prettyR/docs/xtab xtab in R?
I'm not a user for these things and only know how to use numpy directly. It would be possible to add a formula based wrapper around pandas and the stats functions.
jankatins commentedon Jun 19, 2013
More something like this: http://www.csub.edu/ssricrem/SPSS/SPSS11-5/11-5.htm (but with a formular interface; not sure if the R function does)
josef-pkt commentedon Jun 19, 2013
I opened #879 (but I won't have time to work on it, especially the pandas and patsy parts which I don't know so well)
From the statistical backend:
We have many but not all tests that are in the SPSS description. Part of the work is to just collect them together as methods to a class and write some nice "summary()" tables.
One question for the implementation is that 2x2, 2xk, 2x2xk and arbitrarily sized contingency tables will have different options/tests available, many tests are for 2x2 only, and some like chisquare or kendall_tau work for any contingency tables.
2x2 and 2xk are largely covered by statsmodels.stats.proportions (once that has a wrapper/summary interface).
jankatins commentedon Jun 20, 2013
To continue on topic: I would vote that using a formula and bool/string-variables for the LHS catches such cases with a helpful error message ("Can't run regressions with bool on LHS: use '(%s)' in the formula" % offending_variable).
Maybe add a new transformation in patsy like "as_int(...)" which would transform bool and strings (if a variable is string and has only two values) to int and error on all other cases?
On the other hand, sometimes it's convenient to use factor levels as LHS...
josef-pkt commentedon Jul 27, 2013
renamed issue title from
Error while computing durbin_watson