<s> from __future__ import print_function <EOL> from statsmodels . compat import urlopen <EOL> import numpy as np <EOL> np . set_printoptions ( precision = <NUM_LIT:4> , suppress = True ) <EOL> import statsmodels . api as sm <EOL> import pandas as pd <EOL> import matplotlib . pyplot as plt <EOL> from statsmodels . formula . api import ols <EOL> from statsmodels . graphics . api import interaction_plot , abline_plot <EOL> from statsmodels . stats . anova import anova_lm <EOL> try : <EOL> salary_table = pd . read_csv ( '<STR_LIT>' ) <EOL> except : <EOL> url = '<STR_LIT>' <EOL> fh = urlopen ( url ) <EOL> salary_table = pd . read_table ( fh ) <EOL> salary_table . to_csv ( '<STR_LIT>' ) <EOL> E = salary_table . E <EOL> M = salary_table . M <EOL> X = salary_table . X <EOL> S = salary_table . S <EOL> plt . figure ( figsize = ( <NUM_LIT:6> , <NUM_LIT:6> ) ) <EOL> symbols = [ '<STR_LIT:D>' , '<STR_LIT>' ] <EOL> colors = [ '<STR_LIT:r>' , '<STR_LIT:g>' , '<STR_LIT>' ] <EOL> factor_groups = salary_table . groupby ( [ '<STR_LIT:E>' , '<STR_LIT:M>' ] ) <EOL> for values , group in factor_groups : <EOL> i , j = values <EOL> plt . scatter ( group [ '<STR_LIT:X>' ] , group [ '<STR_LIT:S>' ] , marker = symbols [ j ] , color = colors [ i - <NUM_LIT:1> ] , <EOL> s = <NUM_LIT> ) <EOL> plt . xlabel ( '<STR_LIT>' ) ; <EOL> plt . ylabel ( '<STR_LIT>' ) ; <EOL> formula = '<STR_LIT>' <EOL> lm = ols ( formula , salary_table ) . fit ( ) <EOL> print ( lm . summary ( ) ) <EOL> lm . model . exog [ : <NUM_LIT:5> ] <EOL> lm . model . data . orig_exog [ : <NUM_LIT:5> ] <EOL> lm . model . data . frame [ : <NUM_LIT:5> ] <EOL> infl = lm . get_influence ( ) <EOL> print ( infl . summary_table ( ) ) <EOL> df_infl = infl . summary_frame ( ) <EOL> df_infl [ : <NUM_LIT:5> ] <EOL> resid = lm . resid <EOL> plt . figure ( figsize = ( <NUM_LIT:6> , <NUM_LIT:6> ) ) ; <EOL> for values , group in factor_groups : <EOL> i , j = values <EOL> group_num = i * <NUM_LIT:2> + j - <NUM_LIT:1> <EOL> x = [ group_num ] * len ( group ) <EOL> plt . scatter ( x , resid [ group . index ] , marker = symbols [ j ] , color = colors [ i - <NUM_LIT:1> ] , <EOL> s = <NUM_LIT> , edgecolors = '<STR_LIT>' ) <EOL> plt . xlabel ( '<STR_LIT>' ) <EOL> plt . ylabel ( '<STR_LIT>' ) <EOL> interX_lm = ols ( "<STR_LIT>" , salary_table ) . fit ( ) <EOL> print ( interX_lm . summary ( ) ) <EOL> from statsmodels . stats . api import anova_lm <EOL> table1 = anova_lm ( lm , interX_lm ) <EOL> print ( table1 ) <EOL> interM_lm = ols ( "<STR_LIT>" , data = salary_table ) . fit ( ) <EOL> print ( interM_lm . summary ( ) ) <EOL> table2 = anova_lm ( lm , interM_lm ) <EOL> print ( table2 ) <EOL> interM_lm . model . data . orig_exog [ : <NUM_LIT:5> ] <EOL> interM_lm . model . exog <EOL> interM_lm . model . exog_names <EOL> infl = interM_lm . get_influence ( ) <EOL> resid = infl . resid_studentized_internal <EOL> plt . figure ( figsize = ( <NUM_LIT:6> , <NUM_LIT:6> ) ) <EOL> for values , group in factor_groups : <EOL> i , j = values <EOL> idx = group . index <EOL> plt . scatter ( X [ idx ] , resid [ idx ] , marker = symbols [ j ] , color = colors [ i - <NUM_LIT:1> ] , <EOL> s = <NUM_LIT> , edgecolors = '<STR_LIT>' ) <EOL> plt . xlabel ( '<STR_LIT:X>' ) ; <EOL> plt . ylabel ( '<STR_LIT>' ) ; <EOL> drop_idx = abs ( resid ) . argmax ( ) <EOL> print ( drop_idx ) <EOL> idx = salary_table . index . drop ( drop_idx ) <EOL> lm32 = ols ( '<STR_LIT>' , data = salary_table , subset = idx ) . fit ( ) <EOL> print ( lm32 . summary ( ) ) <EOL> print ( '<STR_LIT:\n>' ) <EOL> interX_lm32 = ols ( '<STR_LIT>' , data = salary_table , subset = idx ) . fit ( ) <EOL> print ( interX_lm32 . summary ( ) ) <EOL> print ( '<STR_LIT:\n>' ) <EOL> table3 = anova_lm ( lm32 , interX_lm32 ) <EOL> print ( table3 ) <EOL> print ( '<STR_LIT:\n>' ) <EOL> interM_lm32 = ols ( '<STR_LIT>' , data = salary_table , subset = idx ) . fit ( ) <EOL> table4 = anova_lm ( lm32 , interM_lm32 ) <EOL> print ( table4 ) <EOL> print ( '<STR_LIT:\n>' ) <EOL> try : <EOL> resid = interM_lm32 . get_influence ( ) . summary_frame ( ) [ '<STR_LIT>' ] <EOL> except : <EOL> resid = interM_lm32 . get_influence ( ) . summary_frame ( ) [ '<STR_LIT>' ] <EOL> plt . figure ( figsize = ( <NUM_LIT:6> , <NUM_LIT:6> ) ) <EOL> for values , group in factor_groups : <EOL> i , j = values <EOL> idx = group . index <EOL> plt . scatter ( X [ idx ] , resid [ idx ] , marker = symbols [ j ] , color = colors [ i - <NUM_LIT:1> ] , <EOL> s = <NUM_LIT> , edgecolors = '<STR_LIT>' ) <EOL> plt . xlabel ( '<STR_LIT>' ) ; <EOL> plt . ylabel ( '<STR_LIT>' ) ; <EOL> lm_final = ols ( '<STR_LIT>' , data = salary_table . drop ( [ drop_idx ] ) ) . fit ( ) <EOL> mf = lm_final . model . data . orig_exog <EOL> lstyle = [ '<STR_LIT:->' , '<STR_LIT>' ] <EOL> plt . figure ( figsize = ( <NUM_LIT:6> , <NUM_LIT:6> ) ) <EOL> for values , group in factor_groups : <EOL> i , j = values <EOL> idx = group . index <EOL> plt . scatter ( X [ idx ] , S [ idx ] , marker = symbols [ j ] , color = colors [ i - <NUM_LIT:1> ] , <EOL> s = <NUM_LIT> , edgecolors = '<STR_LIT>' ) <EOL> plt . plot ( mf . X [ idx ] . dropna ( ) , lm_final . fittedvalues [ idx ] . dropna ( ) , <EOL> ls = lstyle [ j ] , color = colors [ i - <NUM_LIT:1> ] ) <EOL> plt . xlabel ( '<STR_LIT>' ) ; <EOL> plt . ylabel ( '<STR_LIT>' ) ; <EOL> U = S - X * interX_lm32 . params [ '<STR_LIT:X>' ] <EOL> plt . figure ( figsize = ( <NUM_LIT:6> , <NUM_LIT:6> ) ) <EOL> interaction_plot ( E , M , U , colors = [ '<STR_LIT>' , '<STR_LIT>' ] , markers = [ '<STR_LIT>' , '<STR_LIT:D>' ] , <EOL> markersize = <NUM_LIT:10> , ax = plt . gca ( ) ) <EOL> try : <EOL> jobtest_table = pd . read_table ( '<STR_LIT>' ) <EOL> except : <EOL> url = '<STR_LIT>' <EOL> jobtest_table = pd . read_table ( url ) <EOL> factor_group = jobtest_table . groupby ( [ '<STR_LIT>' ] ) <EOL> plt . figure ( figsize = ( <NUM_LIT:6> , <NUM_LIT:6> ) ) <EOL> colors = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> markers = [ '<STR_LIT:o>' , '<STR_LIT:v>' ] <EOL> for factor , group in factor_group : <EOL> plt . scatter ( group [ '<STR_LIT>' ] , group [ '<STR_LIT>' ] , color = colors [ factor ] , <EOL> marker = markers [ factor ] , s = <NUM_LIT:12> ** <NUM_LIT:2> ) <EOL> plt . xlabel ( '<STR_LIT>' ) ; <EOL> plt . ylabel ( '<STR_LIT>' ) ; <EOL> min_lm = ols ( '<STR_LIT>' , data = jobtest_table ) . fit ( ) <EOL> print ( min_lm . summary ( ) ) <EOL> plt . figure ( figsize = ( <NUM_LIT:6> , <NUM_LIT:6> ) ) ; <EOL> for factor , group in factor_group : <EOL> plt . scatter ( group [ '<STR_LIT>' ] , group [ '<STR_LIT>' ] , color = colors [ factor ] , <EOL> marker = markers [ factor ] , s = <NUM_LIT:12> ** <NUM_LIT:2> ) <EOL> plt . xlabel ( '<STR_LIT>' ) <EOL> plt . ylabel ( '<STR_LIT>' ) <EOL> abline_plot ( model_results = min_lm , ax = plt . gca ( ) ) ; <EOL> min_lm2 = ols ( '<STR_LIT>' , <EOL> data = jobtest_table ) . fit ( ) <EOL> print ( min_lm2 . summary ( ) ) <EOL> plt . figure ( figsize = ( <NUM_LIT:6> , <NUM_LIT:6> ) ) ; <EOL> for factor , group in factor_group : <EOL> plt . scatter ( group [ '<STR_LIT>' ] , group [ '<STR_LIT>' ] , color = colors [ factor ] , <EOL> marker = markers [ factor ] , s = <NUM_LIT:12> ** <NUM_LIT:2> ) <EOL> abline_plot ( intercept = min_lm2 . params [ '<STR_LIT>' ] , <EOL> slope = min_lm2 . params [ '<STR_LIT>' ] , ax = plt . gca ( ) , color = '<STR_LIT>' ) ; <EOL> abline_plot ( intercept = min_lm2 . params [ '<STR_LIT>' ] , <EOL> slope = min_lm2 . params [ '<STR_LIT>' ] + min_lm2 . params [ '<STR_LIT>' ] , <EOL> ax = plt . gca ( ) , color = '<STR_LIT>' ) ; <EOL> min_lm3 = ols ( '<STR_LIT>' , data = jobtest_table ) . fit ( ) <EOL> print ( min_lm3 . summary ( ) ) <EOL> plt . figure ( figsize = ( <NUM_LIT:6> , <NUM_LIT:6> ) ) ; <EOL> for factor , group in factor_group : <EOL> plt . scatter ( group [ '<STR_LIT>' ] , group [ '<STR_LIT>' ] , color = colors [ factor ] , <EOL> marker = markers [ factor ] , s = <NUM_LIT:12> ** <NUM_LIT:2> ) <EOL> abline_plot ( intercept = min_lm3 . params [ '<STR_LIT>' ] , <EOL> slope = min_lm3 . params [ '<STR_LIT>' ] , ax = plt . gca ( ) , color = '<STR_LIT>' ) ; <EOL> abline_plot ( intercept = min_lm3 . params [ '<STR_LIT>' ] + min_lm3 . params [ '<STR_LIT>' ] , <EOL> slope = min_lm3 . params [ '<STR_LIT>' ] , ax = plt . gca ( ) , color = '<STR_LIT>' ) ; <EOL> min_lm4 = ols ( '<STR_LIT>' , data = jobtest_table ) . fit ( ) <EOL> print ( min_lm4 . summary ( ) ) <EOL> plt . figure ( figsize = ( <NUM_LIT:6> , <NUM_LIT:6> ) ) ; <EOL> for factor , group in factor_group : <EOL> plt . scatter ( group [ '<STR_LIT>' ] , group [ '<STR_LIT>' ] , color = colors [ factor ] , <EOL> marker = markers [ factor ] , s = <NUM_LIT:12> ** <NUM_LIT:2> ) <EOL> abline_plot ( intercept = min_lm4 . params [ '<STR_LIT>' ] , <EOL> slope = min_lm4 . params [ '<STR_LIT>' ] , ax = plt . gca ( ) , color = '<STR_LIT>' ) ; <EOL> abline_plot ( intercept = min_lm4 . params [ '<STR_LIT>' ] + min_lm4 . params [ '<STR_LIT>' ] , <EOL> slope = min_lm4 . params [ '<STR_LIT>' ] + min_lm4 . params [ '<STR_LIT>' ] , <EOL> ax = plt . gca ( ) , color = '<STR_LIT>' ) ; <EOL> table5 = anova_lm ( min_lm , min_lm4 ) <EOL> print ( table5 ) <EOL> table6 = anova_lm ( min_lm , min_lm3 ) <EOL> print ( table6 ) <EOL> table7 = anova_lm ( min_lm , min_lm2 ) <EOL> print ( table7 ) <EOL> table8 = anova_lm ( min_lm2 , min_lm4 ) <EOL> print ( table8 ) <EOL> try : <EOL> rehab_table = pd . read_csv ( '<STR_LIT>' ) <EOL> except : <EOL> url = '<STR_LIT>' <EOL> rehab_table = pd . read_table ( url , delimiter = "<STR_LIT:U+002C>" ) <EOL> rehab_table . to_csv ( '<STR_LIT>' ) <EOL> plt . figure ( figsize = ( <NUM_LIT:6> , <NUM_LIT:6> ) ) <EOL> rehab_table . boxplot ( '<STR_LIT>' , '<STR_LIT>' , ax = plt . gca ( ) ) <EOL> rehab_lm = ols ( '<STR_LIT>' , data = rehab_table ) . fit ( ) <EOL> table9 = anova_lm ( rehab_lm ) <EOL> print ( table9 ) <EOL> print ( rehab_lm . model . data . orig_exog ) <EOL> print ( rehab_lm . summary ( ) ) <EOL> try : <EOL> kidney_table = pd . read_table ( '<STR_LIT>' ) <EOL> except : <EOL> url = '<STR_LIT>' <EOL> kidney_table = pd . read_table ( url , delimiter = "<STR_LIT>" ) <EOL> kidney_table . groupby ( [ '<STR_LIT>' , '<STR_LIT>' ] ) . size ( ) <EOL> kt = kidney_table <EOL> plt . figure ( figsize = ( <NUM_LIT:6> , <NUM_LIT:6> ) ) <EOL> interaction_plot ( kt [ '<STR_LIT>' ] , kt [ '<STR_LIT>' ] , np . log ( kt [ '<STR_LIT>' ] + <NUM_LIT:1> ) , <EOL> colors = [ '<STR_LIT>' , '<STR_LIT>' ] , markers = [ '<STR_LIT:D>' , '<STR_LIT>' ] , ms = <NUM_LIT:10> , ax = plt . gca ( ) ) <EOL> kidney_lm = ols ( '<STR_LIT>' , data = kt ) . fit ( ) <EOL> table10 = anova_lm ( kidney_lm ) <EOL> print ( anova_lm ( ols ( '<STR_LIT>' , <EOL> data = kt ) . fit ( ) , kidney_lm ) ) <EOL> print ( anova_lm ( ols ( '<STR_LIT>' , data = kt ) . fit ( ) , <EOL> ols ( '<STR_LIT>' , <EOL> data = kt ) . fit ( ) ) ) <EOL> print ( anova_lm ( ols ( '<STR_LIT>' , data = kt ) . fit ( ) , <EOL> ols ( '<STR_LIT>' , <EOL> data = kt ) . fit ( ) ) ) <EOL> sum_lm = ols ( '<STR_LIT>' , <EOL> data = kt ) . fit ( ) <EOL> print ( anova_lm ( sum_lm ) ) <EOL> print ( anova_lm ( sum_lm , typ = <NUM_LIT:2> ) ) <EOL> print ( anova_lm ( sum_lm , typ = <NUM_LIT:3> ) ) <EOL> nosum_lm = ols ( '<STR_LIT>' , <EOL> data = kt ) . fit ( ) <EOL> print ( anova_lm ( nosum_lm ) ) <EOL> print ( anova_lm ( nosum_lm , typ = <NUM_LIT:2> ) ) <EOL> print ( anova_lm ( nosum_lm , typ = <NUM_LIT:3> ) ) </s>
