{"id":272,"date":"2018-04-06T12:32:41","date_gmt":"2018-04-06T19:32:41","guid":{"rendered":"http:\/\/www.ootp.cavebutter.net\/blog\/?p=272"},"modified":"2018-04-06T12:38:41","modified_gmt":"2018-04-06T19:38:41","slug":"batting-stats-11-wrc-revisited","status":"publish","type":"post","link":"http:\/\/www.ootp.cavebutter.net\/blog\/archives\/272","title":{"rendered":"Batting Stats 11: wRC+ Revisited"},"content":{"rendered":"<p>The results that we got from testing the wRC+ stat weren&#8217;t great. \u00a0In testing some other stuff, I realized that we&#8217;re getting duplicate entries for batters on the CalcBatting table:<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/www.ootp.cavebutter.net\/blog\/wp-content\/uploads\/2018\/04\/wrcplus-redo1.jpg\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"268\" data-permalink=\"http:\/\/www.ootp.cavebutter.net\/blog\/?attachment_id=268\" data-orig-file=\"https:\/\/i0.wp.com\/www.ootp.cavebutter.net\/blog\/wp-content\/uploads\/2018\/04\/wrcplus-redo1.jpg?fit=783%2C100\" data-orig-size=\"783,100\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"wrcplus redo1\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/www.ootp.cavebutter.net\/blog\/wp-content\/uploads\/2018\/04\/wrcplus-redo1.jpg?fit=300%2C38\" data-large-file=\"https:\/\/i0.wp.com\/www.ootp.cavebutter.net\/blog\/wp-content\/uploads\/2018\/04\/wrcplus-redo1.jpg?fit=739%2C94\" class=\"aligncenter size-full wp-image-268\" src=\"https:\/\/i0.wp.com\/www.ootp.cavebutter.net\/blog\/wp-content\/uploads\/2018\/04\/wrcplus-redo1.jpg?resize=739%2C94\" alt=\"\" width=\"739\" height=\"94\" srcset=\"https:\/\/i0.wp.com\/www.ootp.cavebutter.net\/blog\/wp-content\/uploads\/2018\/04\/wrcplus-redo1.jpg?w=783 783w, https:\/\/i0.wp.com\/www.ootp.cavebutter.net\/blog\/wp-content\/uploads\/2018\/04\/wrcplus-redo1.jpg?resize=300%2C38 300w, https:\/\/i0.wp.com\/www.ootp.cavebutter.net\/blog\/wp-content\/uploads\/2018\/04\/wrcplus-redo1.jpg?resize=768%2C98 768w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/a><\/p>\n<p>Clearly the same player, same year, stint, team, and stats. \u00a0Well, all stats except for wRC+. \u00a0The issue is not being able to return a unique subleague for each row. \u00a0To deal with this, we&#8217;re going to make a couple of adjustments:<\/p>\n<p>First, we&#8217;re going to change the sub_league_history_batting table to mirror the structure of the <a href=\"http:\/\/www.ootp.cavebutter.net\/blog\/archives\/250\">sub_league_history_pitching table<\/a>. \u00a0This doesn&#8217;t directly solve the problem, but on reflection, I didn&#8217;t like how this table was calling data from a table it was sending data to. \u00a0It seems like a circular reference to me, even if the data is static.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-sql\">DROP TABLE IF EXISTS sub_league_history_batting;\r\nCREATE TABLE IF NOT EXISTS sub_league_history_batting AS\r\n\r\nSELECT\r\n       year\r\n     , league_id\r\n     , sub_league_id\r\n     , slg_PA\r\n     , slg_r\r\n     \r\n     FROM  (        \r\n     SELECT p.year\r\n          , p.league_id\r\n          , t.sub_league_id\r\n          , sum(pa) AS slg_PA\r\n          , sum(r) AS slg_r\r\n     FROM players_career_batting_stats AS p INNER JOIN team_relations AS t ON p.team_id=t.team_id\r\n         INNER JOIN players ON p.player_id=players.player_id\r\n     WHERE p.split_id=1 AND players.position&lt;&gt;1\r\n     GROUP BY year, league_id, sub_league_id\r\n      ) AS x ;<\/code><\/pre>\n<p>Now we have the subleague data pulling from the game-generated players career batting stats table. \u00a0A quick check shows that subleague runs are unchanged by this, but PA counts have changed a tiny bit &#8211; less than 10 over tens of thousands. \u00a0Not sure why, but I think the raw data from players_career_batting_stats table is more accurate.<\/p>\n<p>Next, we&#8217;re going to add a sub_league column to CalcBatting, but we are going to do it in a way that avoids circular references. We&#8217;re actually going to alter the players_career_batting_stats table to include a subleague. \u00a0Then, we&#8217;ll get fancy and write a trigger that adds subleague to new records while leaving old ones unchanged. \u00a0Let&#8217;s start with altering the existing records.<\/p>\n<p>We&#8217;ll add the subleague column thusly:<\/p>\n<p><code>ALTER TABLE players_career_batting_stats<br \/>\nADD COLUMN sub_league_id INT AFTER league_id;<\/code><\/p>\n<p>Then, populate it:<\/p>\n<p><code>UPDATE players_career_batting_stats2 AS b<br \/>\nINNER JOIN team_relations AS t ON b.league_id=t.league_id AND b.team_id=t.team_id<br \/>\nSET b.sub_league_id=t.sub_league_id;<\/code><\/p>\n<p>We&#8217;ll come back to the trigger in another post. \u00a0We include the sub_league_id column near the top of CalcBatting returning the sub_league column we just included above:<\/p>\n<pre class=\"line-numbers\"><code class=\"language-sql\">USE mystruggle;\r\n#Calculated batting stats for OOTP\r\n    DROP TABLE IF EXISTS CalcBatting;\r\n    CREATE TABLE IF NOT EXISTS CalcBatting AS\r\n\r\n    SELECT b.year\r\n    , b.league_id\r\n    , b.player_id\r\n    , b.stint \r\n    , b.split_id \r\n    , b.team_id \r\n    , b.sub_league_id\r\n    , b.g\r\n    , b.ab\r\nEtc, etc, etc<\/code><\/pre>\n<p>You&#8217;ll notice I did a little other cleanup as well; I removed the league_abbr and team_abbr from the table. \u00a0We don&#8217;t need these columns taking up space in the table when we can easily pull them in when we need them.<\/p>\n<p>Finally, and the cause of the duplicate records problem, was adding a missing JOIN element between players_career_batting_stats and sub_league_history_batting. \u00a0I had joined on league and sub-league, but not year. \u00a0Adding year brought me back to normal.<\/p>\n<p>After I made a few more adjustments that I will talk about in \u00a0a later post, I re-ran a test for wRC+, adjusting my Happy Zone down to 5 points.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/www.ootp.cavebutter.net\/blog\/wp-content\/uploads\/2018\/04\/wrcplus-v2.jpg\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"282\" data-permalink=\"http:\/\/www.ootp.cavebutter.net\/blog\/archives\/272\/wrcplus-v2\" data-orig-file=\"https:\/\/i0.wp.com\/www.ootp.cavebutter.net\/blog\/wp-content\/uploads\/2018\/04\/wrcplus-v2.jpg?fit=567%2C502\" data-orig-size=\"567,502\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"wrcplus v2\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/www.ootp.cavebutter.net\/blog\/wp-content\/uploads\/2018\/04\/wrcplus-v2.jpg?fit=300%2C266\" data-large-file=\"https:\/\/i0.wp.com\/www.ootp.cavebutter.net\/blog\/wp-content\/uploads\/2018\/04\/wrcplus-v2.jpg?fit=567%2C502\" class=\"aligncenter size-full wp-image-282\" src=\"https:\/\/i0.wp.com\/www.ootp.cavebutter.net\/blog\/wp-content\/uploads\/2018\/04\/wrcplus-v2.jpg?resize=567%2C502\" alt=\"\" width=\"567\" height=\"502\" srcset=\"https:\/\/i0.wp.com\/www.ootp.cavebutter.net\/blog\/wp-content\/uploads\/2018\/04\/wrcplus-v2.jpg?w=567 567w, https:\/\/i0.wp.com\/www.ootp.cavebutter.net\/blog\/wp-content\/uploads\/2018\/04\/wrcplus-v2.jpg?resize=300%2C266 300w\" sizes=\"auto, (max-width: 567px) 100vw, 567px\" \/><\/a><\/p>\n<ul>\n<li>20 out of 30 are within 5 points of the game<\/li>\n<li>24 out of 30 are within 7 points of the game<\/li>\n<li>27 out of 30 are within 10 points of the game<\/li>\n<li>3 missed by more than 10, the highest being 14 points.<\/li>\n<\/ul>\n<p>14 points is really a lot. \u00a0However, a 90% pass rate is really pretty good considering where I was before I straightened out the sub-league situation.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The results that we got from testing the wRC+ stat weren&#8217;t great. \u00a0In testing some other stuff, I realized that we&#8217;re getting duplicate entries for batters on the CalcBatting table: Clearly the same player, same year, stint, team, and stats. \u00a0Well, all stats except for wRC+. \u00a0The issue is not being able to return a&hellip; <a class=\"more-link\" href=\"http:\/\/www.ootp.cavebutter.net\/blog\/archives\/272\">Continue reading <span class=\"screen-reader-text\">Batting Stats 11: wRC+ Revisited<\/span> <span class=\"meta-nav\" aria-hidden=\"true\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[25],"tags":[14,7,4],"class_list":["post-272","post","type-post","status-publish","format-standard","hentry","category-batting","tag-batting","tag-mysql","tag-ootp"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9cxb5-4o","jetpack_likes_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"http:\/\/www.ootp.cavebutter.net\/blog\/wp-json\/wp\/v2\/posts\/272","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.ootp.cavebutter.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.ootp.cavebutter.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.ootp.cavebutter.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.ootp.cavebutter.net\/blog\/wp-json\/wp\/v2\/comments?post=272"}],"version-history":[{"count":3,"href":"http:\/\/www.ootp.cavebutter.net\/blog\/wp-json\/wp\/v2\/posts\/272\/revisions"}],"predecessor-version":[{"id":286,"href":"http:\/\/www.ootp.cavebutter.net\/blog\/wp-json\/wp\/v2\/posts\/272\/revisions\/286"}],"wp:attachment":[{"href":"http:\/\/www.ootp.cavebutter.net\/blog\/wp-json\/wp\/v2\/media?parent=272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.ootp.cavebutter.net\/blog\/wp-json\/wp\/v2\/categories?post=272"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.ootp.cavebutter.net\/blog\/wp-json\/wp\/v2\/tags?post=272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}