import numpy as np import pandas as pd import seaborn as sns import statsmodels.formula.api as sm import statsmodels as stamo import patsy from statsmodels.regression.linear_model import OLS from statsmodels.sandbox.regression.predstd import wls_prediction_std from collections import defaultdict import matplotlib.pyplot as plt from sklearn.feature_selection import VarianceThreshold from sklearn.preprocessing import PolynomialFeatures from sklearn.feature_selection import SelectKBest from sklearn.feature_selection import chi2 from sklearn import linear_model from sklearn.linear_model import Ridge from sklearn.metrics import mean_squared_error, r2_score from ggplot import * import regressions myds = pd.read_csv('all_vars_with_social.csv') data_frame = pd.DataFrame({ "spotify_passive_streams": myds['SPOTIFY_STREAMS_PASSIVE_DISC_TOTAL'], "spotify_playlist_streams": myds['SPOTIFY_PLAYLISTS_STREAMS_TOTAL'], "spotify_active_streams": myds['SPOTIFY_STREAMS_ACTIVE_DISC_TOTAL'], "spotify_collection_streams": myds['SPOTIFY_STREAMS_COLLECTION_TOTAL'], "apple_passive_streams": myds['APPLE_STREAMS_PASSIVE_DISC_TOTAL'], "apple_active_streams": myds['APPLE_STREAMS_ACTIVE_DISC_TOTAL'], "apple_collection_streams": myds['APPLE_STREAMS_COLLECTION_TOTAL'], "apple_playlist_streams": myds['APPLE_PLAYLISTS_STREAMS_TOTAL'], "amazon_total_streams": myds['AMAZON_OVERALL_STREAMS_TOTAL'], "pandora_total_streams": myds['PANDORA_OVERALL_STREAMS_TOTAL'], "youtube_audio_streams": myds['YOUTUBE_AUDIO_OVERALL_STREAMS_TOTAL'], "shazam_total_streams": myds['SHAZAM_OVERALL_STREAMS_TOTAL'], "alexa_total_streams": myds['ALEXA_OVERALL_STREAMS_TOTAL'], "itunes_total_downloads": myds['ITUNES_TOTAL_DOWNLOADS'], "amazon_total_downloads": myds['AMAZON_TOTAL_DOWNLOADS'], "google_total_downloads": myds['GOOGLE_PLAY_DOWNLOADS_TOTAL'], "total_gross_revenue_last_year": myds['TOTAL_GROSS_REVENUE_USD_LAST_YEAR'], "apple_playlists_streams_last_year": myds['APPLE_PLAYLISTS_STREAMS_LAST_YEAR'], "spotify_playlists_streams_last_year": myds['SPOTIFY_PLAYLISTS_STREAMS_LAST_YEAR'], "total_physical_rev": myds['PHYSICAL_GROSS_REVENUE'], "alexa_overall_streams_last_year": myds['ALEXA_OVERALL_STREAMS_LAST_YEAR'], "wiki_pageviews_ly": myds['WIKI_PAGEVIEWS_LY'], "songkick_followers_ly": myds['SONGKICK_FOLLOWERS_LY'], "facebook_likes_total": myds['FB_LIKES_LTD'], "instagram_followers_total": myds['INSTA_FOLLOWERS_LTD'], 'y': myds['TOTAL_GROSS_REVENUE'], # "release_genre": myds["RELEASE_GENRE"], # "spotify_total_streams": myds["SPOTIFY_OVERALL_STREAMS_TOTAL"], # "apple_total_streams": myds["APPLE_OVERALL_STREAMS_TOTAL"], }) data_frame.fillna(0, inplace=True) data_frame = data_frame.replace(0, 1) genres = pd.get_dummies(myds["RELEASE_GENRE"]) rock = genres['Rock'] country = pd.get_dummies(myds["LABEL_COUNTRY"]) usa = country["USA"] data_frame = np.log(data_frame) # data_frame['y'] = myds['TOTAL_GROSS_REVENUE'] data_frame = data_frame.replace([np.inf, -np.inf, np.nan], 0) data_frame = pd.concat([data_frame, rock, usa], axis=1) # # fml = "y ~ np.log(total_physical_rev) + np.log(spotify_playlist_streams) + np.log(spotify_active_streams) + np.log(spotify_passive_streams) + np.log(spotify_collection_streams) + np.log(" \ # "apple_active_streams) + np.log(apple_passive_streams) + np.log(apple_collection_streams) + np.log(pandora_total_streams) + np.log(shazam_total_streams)" # Ols Regulization # model = sm.ols(fml, data=data_frame) # unregularized_regression = model.fit() # result = sm.ols(fml, data=data_frame).fit_regularized(method='elastic_net', L1_wt=0, alpha=5.0, start_params=unregularized_regression.params, profile_scale=False, refit=False) # print(result.params) # final = stamo.regression.linear_model.OLSResults(model, # result.params, # model.normalized_cov_params) # print(final.summary()) alpha_ridge = [1e-15, 1e-10, 1e-8, 1e-4, 1e-3, 1e-2, (1e-2 * 2), (1e-2 * 4), (1e-2 * 8), 1, 5, 10, 15, 20] # # predictors = ["spotify_passive_streams", "spotify_playlist_streams", "spotify_active_streams", "spotify_collection_streams", # "apple_passive_streams", "apple_active_streams", "apple_collection_streams", "apple_playlist_streams", # "amazon_total_streams", "pandora_total_streams", "youtube_audio_streams", "shazam_total_streams", # "alexa_total_streams", "itunes_total_downloads", "amazon_total_downloads", "google_total_downloads", # "total_gross_revenue_last_year", "apple_playlists_streams_last_year", "spotify_playlists_streams_last_year", # "alexa_overall_streams_last_year", "Metal", "Blues", "Classical", "Country", "Electronic", "Hip-hop/Rap", # "New Age", "Pop", "Punk", "R&B", "Rock", "wiki_pageviews_ly", "songkick_followers_ly", "facebook_likes_total", "instagram_followers_total"] ## predictors = ["spotify_passive_streams", "spotify_playlist_streams", "spotify_active_streams", "spotify_collection_streams", "apple_passive_streams", "apple_active_streams", "apple_collection_streams", "apple_playlist_streams", "amazon_total_streams", "pandora_total_streams", "youtube_audio_streams", "shazam_total_streams", "alexa_total_streams", "itunes_total_downloads", "amazon_total_downloads", "google_total_downloads", "total_gross_revenue_last_year", "apple_playlists_streams_last_year", "spotify_playlists_streams_last_year", "alexa_overall_streams_last_year", "Rock", "USA"] for i in range(len(alpha_ridge)): print(regressions.ridge_regression(data_frame, predictors, alpha_ridge[i]))