Skip to content

Instantly share code, notes, and snippets.

View rtkilian's full-sized avatar

Rhys Kilian rtkilian

View GitHub Profile
from scipy.stats import fisher_exact
# Example contingency table
table = [[100, 80],[150, 20]]
# Calculate test statistic and p-value
stat, p = fisher_exact(table)
# Interpreation
print('stat=%.3f, p=%.3f' % (stat, p))
from statsmodels.stats.rates import test_poisson_2indep
# Example inputs taken from Gu, Ng, Tang, Schucany 2008: Testing the Ratio of Two Poisson Rates
count1 = 60
exposure1 = 51477.5
count2 = 30
exposure2 = 54308.7
# Calculate test statistic and p-value
stat, p = test_poisson_2indep(count1, exposure1, count2, exposure2, method='etest-wald')
@rtkilian
rtkilian / sktime_forecast_linear_regression.py
Created July 9, 2022 23:40
Linear regression forecaster with sktime
from sktime.forecasting.model_selection import temporal_train_test_split
from sktime.forecasting.base import ForecastingHorizon
from sktime.forecasting.compose import make_reduction
from sktime.utils.plotting import plot_series
from sktime.performance_metrics.forecasting import mean_absolute_percentage_error
from sklearn.linear_model import LinearRegression
# Split data
y_train, y_test = temporal_train_test_split(y, test_size=26) # Predict from 1st July 2019
from sktime.forecasting.model_selection import temporal_train_test_split
from sktime.forecasting.base import ForecastingHorizon
from sktime.forecasting.compose import make_reduction
from sktime.utils.plotting import plot_series
from sktime.performance_metrics.forecasting import mean_absolute_percentage_error
from xgboost import XGBRegressor
# Create an exogenous dataframe indicating the month
X = pd.DataFrame({'month': y.index.month}, index=y.index)
from sktime.forecasting.model_selection import temporal_train_test_split, SingleWindowSplitter, ForecastingRandomizedSearchCV
from sktime.forecasting.base import ForecastingHorizon
from sktime.forecasting.compose import make_reduction
from sktime.utils.plotting import plot_series
from sktime.performance_metrics.forecasting import mean_absolute_percentage_error
from xgboost import XGBRegressor
# Create an exogenous dataframe indicating the month
X = pd.DataFrame({'month': y.index.month}, index=y.index)
from sktime.forecasting.model_selection import temporal_train_test_split, SingleWindowSplitter, ForecastingRandomizedSearchCV
from sktime.forecasting.base import ForecastingHorizon
from sktime.forecasting.compose import make_reduction, TransformedTargetForecaster
from sktime.utils.plotting import plot_series
from sktime.performance_metrics.forecasting import mean_absolute_percentage_error
from sktime.transformations.series.detrend import Deseasonalizer, Detrender
from sktime.forecasting.trend import PolynomialTrendForecaster
from xgboost import XGBRegressor
@rtkilian
rtkilian / analysis.py
Created July 25, 2022 23:31
Scheduling a Amazon SageMaker Notebook Instance example.
# Packages
import pandas as pd
import matplotlib.pyplot as plt
import boto3
# Default parameters
bucket = 'rtkilian-writing'
image_name = 'covid_cumulative_aus_state.png'
# Read