<s> from __future__ import print_function <EOL> from statsmodels . compat import lzip <EOL> import numpy as np <EOL> import pandas as pd <EOL> import matplotlib . pyplot as plt <EOL> import statsmodels . api as sm <EOL> from statsmodels . formula . api import ols <EOL> prestige = sm . datasets . get_rdataset ( "<STR_LIT>" , "<STR_LIT>" , cache = True ) . data <EOL> prestige . head ( ) <EOL> prestige_model = ols ( "<STR_LIT>" , data = prestige ) . fit ( ) <EOL> print ( prestige_model . summary ( ) ) <EOL> fig , ax = plt . subplots ( figsize = ( <NUM_LIT:12> , <NUM_LIT:8> ) ) <EOL> fig = sm . graphics . influence_plot ( prestige_model , ax = ax , criterion = "<STR_LIT>" ) <EOL> fig , ax = plt . subplots ( figsize = ( <NUM_LIT:12> , <NUM_LIT:8> ) ) <EOL> fig = sm . graphics . plot_partregress ( "<STR_LIT>" , "<STR_LIT>" , [ "<STR_LIT>" , "<STR_LIT>" ] , data = prestige , ax = ax ) <EOL> ax = fig . axes [ <NUM_LIT:0> ] <EOL> ax . set_xlim ( - <NUM_LIT> , <NUM_LIT> ) <EOL> ax . set_ylim ( - <NUM_LIT> , <NUM_LIT:30> ) ; <EOL> fix , ax = plt . subplots ( figsize = ( <NUM_LIT:12> , <NUM_LIT> ) ) <EOL> fig = sm . graphics . plot_partregress ( "<STR_LIT>" , "<STR_LIT>" , [ "<STR_LIT>" ] , data = prestige , ax = ax ) <EOL> subset = ~ prestige . index . isin ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> prestige_model2 = ols ( "<STR_LIT>" , data = prestige , subset = subset ) . fit ( ) <EOL> print ( prestige_model2 . summary ( ) ) <EOL> fig = plt . figure ( figsize = ( <NUM_LIT:12> , <NUM_LIT:8> ) ) <EOL> fig = sm . graphics . plot_partregress_grid ( prestige_model , fig = fig ) <EOL> fig , ax = plt . subplots ( figsize = ( <NUM_LIT:12> , <NUM_LIT:8> ) ) <EOL> fig = sm . graphics . plot_ccpr ( prestige_model , "<STR_LIT>" , ax = ax ) <EOL> fig = plt . figure ( figsize = ( <NUM_LIT:12> , <NUM_LIT:8> ) ) <EOL> fig = sm . graphics . plot_ccpr_grid ( prestige_model , fig = fig ) <EOL> fig = plt . figure ( figsize = ( <NUM_LIT:12> , <NUM_LIT:8> ) ) <EOL> fig = sm . graphics . plot_regress_exog ( prestige_model , "<STR_LIT>" , fig = fig ) <EOL> fig , ax = plt . subplots ( figsize = ( <NUM_LIT:12> , <NUM_LIT:8> ) ) <EOL> fig = sm . graphics . plot_fit ( prestige_model , "<STR_LIT>" , ax = ax ) <EOL> dta = sm . datasets . statecrime . load_pandas ( ) . data <EOL> crime_model = ols ( "<STR_LIT>" , data = dta ) . fit ( ) <EOL> print ( crime_model . summary ( ) ) <EOL> fig = plt . figure ( figsize = ( <NUM_LIT:12> , <NUM_LIT:8> ) ) <EOL> fig = sm . graphics . plot_partregress_grid ( crime_model , fig = fig ) <EOL> fig , ax = plt . subplots ( figsize = ( <NUM_LIT:12> , <NUM_LIT:8> ) ) <EOL> fig = sm . graphics . plot_partregress ( "<STR_LIT>" , "<STR_LIT>" , [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] , ax = ax , data = dta ) <EOL> fig , ax = plt . subplots ( figsize = ( <NUM_LIT:8> , <NUM_LIT:6> ) ) <EOL> fig = sm . graphics . plot_leverage_resid2 ( crime_model , ax = ax ) <EOL> fig , ax = plt . subplots ( figsize = ( <NUM_LIT:8> , <NUM_LIT:6> ) ) <EOL> fig = sm . graphics . influence_plot ( crime_model , ax = ax ) <EOL> from statsmodels . formula . api import rlm <EOL> rob_crime_model = rlm ( "<STR_LIT>" , data = dta , <EOL> M = sm . robust . norms . TukeyBiweight ( <NUM_LIT:3> ) ) . fit ( conv = "<STR_LIT>" ) <EOL> print ( rob_crime_model . summary ( ) ) <EOL> weights = rob_crime_model . weights <EOL> idx = weights > <NUM_LIT:0> <EOL> X = rob_crime_model . model . exog [ idx ] <EOL> ww = weights [ idx ] / weights [ idx ] . mean ( ) <EOL> hat_matrix_diag = ww * ( X * np . linalg . pinv ( X ) . T ) . sum ( <NUM_LIT:1> ) <EOL> resid = rob_crime_model . resid <EOL> resid2 = resid ** <NUM_LIT:2> <EOL> resid2 /= resid2 . sum ( ) <EOL> nobs = int ( idx . sum ( ) ) <EOL> hm = hat_matrix_diag . mean ( ) <EOL> rm = resid2 . mean ( ) <EOL> from statsmodels . graphics import utils <EOL> fig , ax = plt . subplots ( figsize = ( <NUM_LIT:12> , <NUM_LIT:8> ) ) <EOL> ax . plot ( resid2 [ idx ] , hat_matrix_diag , '<STR_LIT:o>' ) <EOL> ax = utils . annotate_axes ( range ( nobs ) , labels = rob_crime_model . model . data . row_labels [ idx ] , <EOL> points = lzip ( resid2 [ idx ] , hat_matrix_diag ) , offset_points = [ ( - <NUM_LIT:5> , <NUM_LIT:5> ) ] * nobs , <EOL> size = "<STR_LIT>" , ax = ax ) <EOL> ax . set_xlabel ( "<STR_LIT>" ) <EOL> ax . set_ylabel ( "<STR_LIT>" ) <EOL> ylim = ax . get_ylim ( ) <EOL> ax . vlines ( rm , * ylim ) <EOL> xlim = ax . get_xlim ( ) <EOL> ax . hlines ( hm , * xlim ) <EOL> ax . margins ( <NUM_LIT:0> , <NUM_LIT:0> ) </s>
