![scipy stats pearsonr scipy stats pearsonr](https://ars.els-cdn.com/content/image/3-s2.0-B9781843345657500046-f04-05-9781843345657.gif)
#SCIPY STATS PEARSONR CODE#
With just that slight modification, the code executes without hitches. # Input Pandas Series of the Future and Lagged Variables into the function Weight_change_future = weight_change_future Weight_change_lagged = weight_change_lagged # Remove na values before inputting the data into the peasonr function (not within the function as I had done): So, I edited the problematic segment of the code as follows: # Define an object containing observations that are not NA However, complete Pandas Dataframes of the variables, even if they contain one column, do not work. Pandas Series of the input variables work.Scipy's pearsonr package only accepts arrays or array-like inputs. Through random tinkering, I managed to solve my problem as follows:
#SCIPY STATS PEARSONR HOW TO#
TypeError: no loop matching the specified signature and casting wasĭoes anyone have any clues on how to fix my problem? I looked through the forums and found no answers that fit my exact requirements. The loop executes well, however, it fails when it comes to calculating the pearsonr correlation and p-value, i.e at this section: (correlation, p-value)=pearsonr(weight_change_lagged, weight_change_future) (correlation, p-value)=pearsonr(weight_change_lagged, weight_change_future) Not_na = (weight_change_lagged.notna() & weight_change_future.notna()).values Weight_change_future = weight_change_future.iloc Weight_change_lagged = weight_change_lagged.iloc IndepSet=range(0, weight_change_lagged.shape, timelags) IndepSet=range(0, weight_change_lagged.shape, holddays) Weight_change_future = df.shift(-holddays).pct_change(periods=holddays) Weight_change_lagged = df.pct_change(periods=timelags) # and calculating the pearsonr correlation and p-value of each combinationįor timelags in :įor holddays in : # Loop for producing combinations of different timelags and holddays
![scipy stats pearsonr scipy stats pearsonr](https://re-thought.com/content/images/2020/02/Screen-Shot-2020-02-16-at-12.23.55.png)
The loop can be found below: from import pearsonr This is being done to evaluate the impact of holding livestock over time periods of various lengths. I am trying to use a "for-loop" to calculate the correlation between the percentage change of the "Weight" variable over various timeframes or timelags. The head of the dataframe, below, shows its structure: Weight It has one column and the following shape: (2000, 1). I have a timeseries Pandas dataframe called "df".