tag:blogger.com,1999:blog-36940861693360725202017-02-08T20:56:03.140-08:00Joe Regan's blogJoe Reganhttp://www.blogger.com/profile/04575217740526266213noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-3694086169336072520.post-65083929605648430712011-05-15T17:28:00.000-07:002011-05-15T18:06:57.405-07:00How to play Nim on the command lineA bit of a deviation from pure statistics, because tonight I wrote something interesting, while attempting to learn the <a href="http://en.wikipedia.org/wiki/Python_(programming_language)">Python programming language</a> for work. The program is simple, a <a href="http://en.wikipedia.org/wiki/Nim#The_subtraction_game_S.281.2C2.2C....2Ck.29">normal play subtraction (Nim)</a> game. <br /><br />The object of the game is simple: given a pile of N elements (stones, we will call them), each player is allowed to select between 1 and k (where k is any number greater than or equal to 2, and less than N) elements to take. The winner is whichever player takes the last element from the pile.<br /><br />The winning strategy of this game is simple, if one does a modular division of the number of elements in the pile by the sum of the maximum move and 1 is zero, the player has a winning strategy if the opponent is moving, and a losing one if they are moving (example: a maximum move game of 2 and a pile of 9, the player is in a winning position if the opponent is moving, since 9 Mod 3 = 0). Therefore if, at the start of the game, N Mod (k+1) = 0, then a player would want to go second in order to have a winning strategy, first otherwise.<br /><br />How can one write a program in Python that can simulate this? Well, it's easy, and took me just 84 lines of code. First, the full program:<br /><br /><span style="font-style:italic;">#!C:/Python32<br /><br />#Joe Regan<br />#nims.py<br />#5/15/11<br /><br />import math<br />import random<br /><br />def play_nims(pile,maxRemove):<br /> print("Starting with ",pile," stones, select up to ",maxRemove," stones")<br /> while(pile>0):<br /> p1move=int(input("Select stones to remove "))<br /> while(p1move>maxRemove):<br /> p1move=int(input("Select stones to remove "))<br /> pile=pile-p1move<br /> print("Player 1 took ",p1move," stones, and ",pile," stones remain")<br /> if(pile==0):<br /> print("Player 1 wins!")<br /> else:<br /> p2move=int(input("Select stones to remove "))<br /> while(p2move>maxRemove):<br /> p2move=int(input("Select stones to remove "))<br /> pile=pile-p2move<br /> print("Player 2 took ",p2move," stones, and ",pile," stones remain")<br /> if(pile==0):<br /> print("Player 2 wins!")<br /><br /><br />def play_nims_comp(pile,maxRemove):<br /> print("Starting with ",pile," stones, select up to ",maxRemove," stones")<br /> turn=int(input("Enter 1 to go first, any other number to go second: ")) #allow user to decide in what order he wants to go<br /> while(pile>0):<br /> if(turn==1):<br /> move=int(input("Select stones to remove "))<br /> while(move>maxRemove):<br /> move=int(input("Select stones to remove "))<br /> pile=pile-move<br /> print("Player took ",move," stones, and ",pile," stones remain")<br /> if(pile==0):<br /> print("Player wins!")<br /> else:<br /> if(math.fmod(pile,(maxRemove+1))==0):<br /> CPUmove=random.randint(1,maxRemove)<br /> else:<br /> CPUmove=math.fmod(pile,(maxRemove+1))<br /> pile=pile-CPUmove<br /> print("CPU took ",CPUmove," stones, and ",pile," stones remain")<br /> if(pile==0):<br /> print("Player loses!")<br /> else:<br /> if(math.fmod(pile,(maxRemove+1))==0):<br /> CPUmove=random.randint(1,maxRemove)<br /> else:<br /> CPUmove=math.fmod(pile,(maxRemove+1))<br /> pile=pile-CPUmove<br /> print("CPU took ",CPUmove," stones, and ",pile," stones remain")<br /> if(pile==0):<br /> print("Player loses!")<br /> else:<br /> move=int(input("Select stones to remove "))<br /> while(move>maxRemove):<br /> move=int(input("Select stones to remove "))<br /> pile=pile-move<br /> print("Player took ",move," stones, and ",pile," stones remain")<br /> if(pile==0):<br /> print("Player wins!") <br /><br /><br />#Allow the user to select which game they would like to play<br />CPUorHuman=int(input("Let's play nim! First, select if you're playing a human (1) or the computer (2): "))<br />while(CPUorHuman!=1 and CPUorHuman!=2):<br /> CPUorHuman=int(input("Let's play nim! First, select if you're playing a human (1) or the computer (2): "))<br />params=input("Enter the starting pile and max move, separated by commas: ").split(",")<br />P=int(params[0])<br />M=int(params[1])<br />while(P<=M):<br /> params=input("Enter the starting pile and max move, separated by commas: ").split(",")<br /> P=int(params[0])<br /> M=int(params[1])<br />if(CPUorHuman==1):<br /> play_nims(P,M)<br />else:<br /> play_nims_comp(P,M)</span><br /><br />Now let's break it down to the functional levels.<br /><span style="font-style:italic;">#!C:/Python32<br /><br />#Joe Regan<br />#nims.py<br />#5/15/11<br /><br />import math<br />import random<br /></span><br /><br />Here's where I write the shebang, telling the system that I want to use python. After some documentation, I import the math package (redundant here) and the random package (which will provide highly useful for the CPU logic).<br /><br /><span style="font-style:italic;">def play_nims(pile,maxRemove):<br /> print("Starting with ",pile," stones, select up to ",maxRemove," stones")<br /> while(pile>0):<br /> p1move=int(input("Select stones to remove "))<br /> while(p1move>maxRemove):<br /> p1move=int(input("Select stones to remove "))<br /> pile=pile-p1move<br /> print("Player 1 took ",p1move," stones, and ",pile," stones remain")<br /> if(pile==0):<br /> print("Player 1 wins!")<br /> else:<br /> p2move=int(input("Select stones to remove "))<br /> while(p2move>maxRemove):<br /> p2move=int(input("Select stones to remove "))<br /> pile=pile-p2move<br /> print("Player 2 took ",p2move," stones, and ",pile," stones remain")<br /> if(pile==0):<br /> print("Player 2 wins!")<br /></span><br /><br />This is the method used in a human v. human game. As order of player does not matter to the computer, this is simple: we pass to the function the size of the original pile, and the maximum number of moves. While there's still elements within the pile, we allow players to take turns.<br /><br />First, player 1 makes his move (and continues to be asked until giving a valid response), and his move is subtracted from the pile. If he takes the last stone, player 1 wins, and if not, the same process goes to player 2. If player 2 does not win, we go back to the top of the while loop.<br /><br /><span style="font-style:italic;">def play_nims_comp(pile,maxRemove):<br /> print("Starting with ",pile," stones, select up to ",maxRemove," stones")<br /> turn=int(input("Enter 1 to go first, any other number to go second: ")) #allow user to decide in what order he wants to go<br /> while(pile>0):<br /> if(turn==1):<br /> move=int(input("Select stones to remove "))<br /> while(move>maxRemove):<br /> move=int(input("Select stones to remove "))<br /> pile=pile-move<br /> print("Player took ",move," stones, and ",pile," stones remain")<br /> if(pile==0):<br /> print("Player wins!")<br /> else:<br /> if(math.fmod(pile,(maxRemove+1))==0):<br /> CPUmove=random.randint(1,maxRemove)<br /> else:<br /> CPUmove=math.fmod(pile,(maxRemove+1))<br /> pile=pile-CPUmove<br /> print("CPU took ",CPUmove," stones, and ",pile," stones remain")<br /> if(pile==0):<br /> print("Player loses!")<br /> else:<br /> if(math.fmod(pile,(maxRemove+1))==0):<br /> CPUmove=random.randint(1,maxRemove)<br /> else:<br /> CPUmove=math.fmod(pile,(maxRemove+1))<br /> pile=pile-CPUmove<br /> print("CPU took ",CPUmove," stones, and ",pile," stones remain")<br /> if(pile==0):<br /> print("Player loses!")<br /> else:<br /> move=int(input("Select stones to remove "))<br /> while(move>maxRemove):<br /> move=int(input("Select stones to remove "))<br /> pile=pile-move<br /> print("Player took ",move," stones, and ",pile," stones remain")<br /> if(pile==0):<br /> print("Player wins!") <br /></span><br /><br />This is far more complex. First, we have to ask the player (who is opposing the computer, so now order does matter), whether he wants to go first or second (after passing the initial game rules again). If the player decides to go first, the same logic as before is followed for the human player. The second player, aka the computer, is designed to play an optimal game, however. Therefore, if the player leaves a pile of M where M Mod (k+1) > 0, the computer will simply take M Mod (k+1), and will have a winning strategy (and eventually win). If not, the computer will select a random amount between 1 and k, and continue to have a losing strategy, unless the human player makes an error. If the player decides to go second, the reverse order is followed.<br /><br /><span style="font-style:italic;">CPUorHuman=int(input("Let's play nim! First, select if you're playing a human (1) or the computer (2): "))<br />while(CPUorHuman!=1 and CPUorHuman!=2):<br /> CPUorHuman=int(input("Let's play nim! First, select if you're playing a human (1) or the computer (2): "))<br />params=input("Enter the starting pile and max move, separated by commas: ").split(",")<br />P=int(params[0])<br />M=int(params[1])<br />while(P<=M):<br /> params=input("Enter the starting pile and max move, separated by commas: ").split(",")<br /> P=int(params[0])<br /> M=int(params[1])<br />if(CPUorHuman==1):<br /> play_nims(P,M)<br />else:<br /> play_nims_comp(P,M)</span><br /><br />Here's where the user interacts with the command line to decide the game options. First, the user immediately decides if they are playing a human (1) or a computer (2), and this is asked until the user answers with a 1 or 2. From there, the user is asked to provide a starting pile and maximum move, and is asked again if the pile is less than or equal to the maximum move (comma separated). Finally, we pass to the if statement, where the program decides whether to call the human v. human, or human v. computer game.<br /><br />So how does this look? Let's play the computer, start with 30 stones, and allow a maximum move of 4.<br /><br /><span style="font-style:italic;">Let's play nim! First, select if you're playing a human (1) or the computer (2): 2<br />Enter the starting pile and max move, separated by commas: 30,4<br />Starting with 30 stones, select up to 4 stones<br />Enter 1 to go first, any other number to go second: 2<br />CPU took 4 stones, and 26 stones remain<br />Select stones to remove 1<br />Player took 1 stones, and 25 stones remain<br />CPU took 1 stones, and 24 stones remain<br />Select stones to remove 4<br />Player took 4 stones, and 20 stones remain<br />CPU took 4 stones, and 16 stones remain<br />Select stones to remove 1<br />Player took 1 stones, and 15 stones remain<br />CPU took 4 stones, and 11 stones remain<br />Select stones to remove 1<br />Player took 1 stones, and 10 stones remain<br />CPU took 2 stones, and 8 stones remain<br />Select stones to remove 3<br />Player took 3 stones, and 5 stones remain<br />CPU took 3 stones, and 2 stones remain<br />Select stones to remove 2<br />Player took 2 stones, and 0 stones remain<br />Player wins!</span><br /><br />As you can see, I elected to go first (since 30 Mod (4+1) = 0, I had a winning strategy if the computer went first), and won the game by following this basic rule. <br /><br />So, if you have python installed on your own system, try it out, and see if you can keep your friends stumped.<img src="http://feeds.feedburner.com/~r/JoeRegansBlog/~4/UuEkURwIqjc" height="1" width="1" alt=""/>Joe Reganhttp://www.blogger.com/profile/04575217740526266213noreply@blogger.com0http://joeregan.blogspot.com/2011/05/how-to-play-nim-on-command-line.htmltag:blogger.com,1999:blog-3694086169336072520.post-16585437686508120372011-02-21T17:09:00.000-08:002011-02-21T18:30:36.122-08:00Does Spending on Education Improve SAT scores?In case one has been living under a rock recently (as I seemingly have given my lack of posting), the big <a href="http://politics.blogs.foxnews.com/2011/02/21/wisconsin-protests-leave-districts-scrambling-staff-schools">teachers' union battle is occurring on a daily basis in Wisconsin</a>. While the Republican controlled government wants to increase teacher contribution to their benefits package, the unions and many teachers are fighting hard against this.<br /><br />While for years, we in the private sector have had to contribute to our retirements and benefits packages, this seems novel to many of these teachers. In their defense, though, many independent sources argue that spending more money on education would improve American performance, <a href="http://4brevard.com/choice/international-test-scores.htm">which is very clearly an issue</a>. However, does the data support this?<br /><br />My interest was piqued upon coming across a post on <a href="http://xenophilius.wordpress.com/2009/11/02/how-per-student-spending-on-education-compares-to-state-sat-score-rankings/">this blog</a>, which highlights state spend, and state SAT scores. While the source does show SAT scores and spending, he clearly underestimates the effects of participation rate (which can lead to sample bias) on the individual states. Even the biggest defender of educational spending cuts would not argue Kentucky has a better system than Massachusetts or Vermont.<br /><br />So I took this to the task of analyzing based on each individual score, and the aggregate total of all three, as dependent to the amount of spend and participation rate of all states. In order to do this, I created <a href="https://spreadsheets.google.com/ccc?key=0AgJLpCNZowZMdEt1NWlFTVhLR1QxS2RjQjdoeHJZc1E&hl=en#gid=0">this spreadsheet</a>.<br /><br />First I looked at the subject dear to my heart (and one we should be focusing more on in this country), mathematics. First, I looked at a simple linear model:<br /><br /><span style="font-style:italic;">> lmMath<-lm(math ~ partrate + spend) > summary(lmMath)<br /><br />Call:<br />lm(formula = math ~ partrate + spend)<br /><br />Residuals:<br />Min 1Q Median 3Q Max<br />-64.453 -15.305 -2.256 13.458 39.785<br /><br />Coefficients:<br /> Estimate Std. Error t value Pr(>|t|)<br />(Intercept) 5.565e+02 1.583e+01 35.15 <></span><br /><br />At first glance, math seems (minimally) correlated w/ educational spending, but far more correlated with participation rate. To find potentially better variables, we can analyze a function w/ quadratic versions of participation rate and spend, and then natural logarithmic transformations:<br /><br /><span style="font-style:italic;">> summary(lm(math ~ partrate + partrate_sq + spend + spend_sq))<br /><br />Call:<br />lm(formula = math ~ partrate + partrate_sq + spend + spend_sq)<br /><br />Residuals:<br />Min 1Q Median 3Q Max<br />-57.029 -8.593 -1.308 12.901 30.822<br /><br />Coefficients:<br /> Estimate Std. Error t value Pr(>|t|)<br />(Intercept) 5.238e+02 6.494e+01 8.065 2.37e-10 ***<br />partrate -2.343e+02 4.543e+01 -5.158 5.16e-06 ***<br />partrate_sq 1.454e+02 5.662e+01 2.569 0.0135 *<br />spend 1.286e-02 1.235e-02 1.041 0.3031<br />spend_sq -5.762e-07 5.865e-07 -0.982 0.3310<br />---<br />Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1<br /><br />Residual standard error: 19.83 on 46 degrees of freedom<br />Multiple R-squared: 0.7871, Adjusted R-squared: 0.7686<br />F-statistic: 42.53 on 4 and 46 DF, p-value: 6.712e-15<br /><br />> summary(lm(math ~ partrate + partrate_ln + spend + spend_ln))<br /><br />Call:<br />lm(formula = math ~ partrate + partrate_ln + spend + spend_ln)<br /><br />Residuals:<br />Min 1Q Median 3Q Max<br />-49.374 -8.838 -0.935 13.856 34.057<br /><br />Coefficients:<br /> Estimate Std. Error t value Pr(>|t|)<br />(Intercept) -463.83562 909.66724 -0.510 0.61256<br />partrate -5.60859 32.95449 -0.170 0.86561<br />partrate_ln -29.31128 8.09637 -3.620 0.00073 ***<br />spend -0.01099 0.01116 -0.985 0.32971<br />spend_ln 116.34990 111.24720 1.046 0.30109<br />---<br />Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1<br /><br />Residual standard error: 18.67 on 46 degrees of freedom<br />Multiple R-squared: 0.8114, Adjusted R-squared: 0.795<br />F-statistic: 49.47 on 4 and 46 DF, p-value: 4.278e-16<br /><br />> summary(lm(math ~ partrate_sq + spend_sq))<br /><br />Call:<br />lm(formula = math ~ partrate_sq + spend_sq)<br /><br />Residuals:<br />Min 1Q Median 3Q Max<br />-68.990 -18.437 -2.103 19.048 48.723<br /><br />Coefficients:<br /> Estimate Std. Error t value Pr(>|t|)<br />(Intercept) 5.536e+02 9.767e+00 56.687 <> summary(lm(math ~ partrate_ln + spend_ln))<br /><br />Call:<br />lm(formula = math ~ partrate_ln + spend_ln)<br /><br />Residuals:<br /> Min 1Q Median 3Q Max<br />-48.2752 -9.7688 -0.1039 12.6402 35.6967<br /><br />Coefficients:<br /> Estimate Std. Error t value Pr(>|t|)<br />(Intercept) 428.039 127.745 3.351 0.00158 **<br />partrate_ln -31.017 2.218 -13.984 <></span><br /><br />It seems like all but spend_ln had some correlation in one of the models, so let's throw all 6 variables into one and see what happens:<br /><br /><span style="font-style:italic;">> summary(lm(math ~ partrate + partrate_ln + partrate_sq + spend + spend_ln + spend_sq))<br /><br />Call:<br />lm(formula = math ~ partrate + partrate_ln + partrate_sq + spend +<br /> spend_ln + spend_sq)<br /><br />Residuals:<br /> Min 1Q Median 3Q Max<br />-47.376 -9.845 -1.071 11.698 33.868<br /><br />Coefficients:<br /> Estimate Std. Error t value Pr(>|t|)<br />(Intercept) -4.978e+03 4.881e+03 -1.020 0.3133<br />partrate 2.468e+01 1.288e+02 0.192 0.8489<br />partrate_ln -3.316e+01 1.471e+01 -2.254 0.0292 *<br />partrate_sq -1.863e+01 9.837e+01 -0.189 0.8507<br />spend -1.312e-01 1.287e-01 -1.020 0.3134<br />spend_ln 7.033e+02 6.358e+02 1.106 0.2746<br />spend_sq 2.974e-06 3.164e-06 0.940 0.3524<br />---<br />Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1<br /><br />Residual standard error: 18.87 on 44 degrees of freedom<br />Multiple R-squared: 0.8158, Adjusted R-squared: 0.7907<br />F-statistic: 32.48 on 6 and 44 DF, p-value: 1.287e-14 </span><br /><br />Yikes, I muddied the waters. Let's look at just the log of participation rate:<br /><span style="font-style:italic;">> summary(lm(math ~ partrate_ln))<br /><br />Call:<br />lm(formula = math ~ partrate_ln)<br /><br />Residuals:<br /> Min 1Q Median 3Q Max<br />-47.6408 -11.2827 -0.2747 13.3237 35.7253<br /><br />Coefficients:<br /> Estimate Std. Error t value Pr(>|t|) <br />(Intercept) 491.382 4.198 117.06 <2e-16></span><br /><br />This simple model fits the best of any we have seen. Is there any way to incorporate spend into this model?<br /><br />No matter what, I couldn't find a way to wedge spend into SAT Math scores. The amount of spending on education seemed to have minimal effect, if any, on performance, and one can estimate the state's average SAT score rather well by the formula:<br /><br />SAT_math = 491.382 - 30.795*ln(participation_rate).<br /><br />But does this hold true for Reading and Writing?<br />The good news for the add spending crowd is that spend was a positively relevant variable when factored with the untransformed participation rate variable, as well as when the log transformed version of spend is regressed with the square of participation rate on score.<br /><br />The bad news is, that the log transformation of participation rate indicates a much stronger correlation than either of these previous functions, with an adjusted r-squared of 87.02%.<br /><br />SAT_reading = 485.670 - 31.665*ln(participation_rate).<br /><br />Writing shares the same characteristic, the possibility of spending improving test scores, though the sample bias in the state's scores makes this tough to decipher.<br /><br />SAT_writing = 475.065 - 30.059*ln(participation_rate).<br /><br />For total SAT scores, this still holds true, where the log of participation rate trumps all other variables in terms of statistical significance, and takes the form:<br /><br />SAT_total = 1452.116 - 92.519*ln(participation_rate).<br /><br />For full disclosure, though, leaving participation_rate untransformed does provide some evidence that spending improves SAT scores:<br /><br /><span style="font-style:italic;">> summary(lm(total ~ partrate + spend_ln))<br /><br />Call:<br />lm(formula = total ~ partrate + spend_ln)<br /><br />Residuals:<br /> Min 1Q Median 3Q Max <br />-167.0151 -37.5985 0.6148 32.2768 111.0931 <br /><br />Coefficients:<br /> Estimate Std. Error t value Pr(>|t|) <br />(Intercept) 825.01 393.85 2.095 0.0415 * <br />partrate -367.49 27.34 -13.442 <2e-16 ***<br />spend_ln 98.63 43.20 2.283 0.0269 * <br />---<br />Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 <br /><br />Residual standard error: 55.8 on 48 degrees of freedom<br />Multiple R-squared: 0.7943, Adjusted R-squared: 0.7858 <br />F-statistic: 92.7 on 2 and 48 DF, p-value: < 2.2e-16 </span><br /><br />Suggesting a relationship of SAT_total = 825.01 - 367.49*participation_rate + 98.63*ln(spend). Given this, we can create a ranking of states using SAT participation rate and per pupil spending to derive a ranking list of the best performing states, as shown below.<br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/-aQRKrH8j7no/TWMfpnvm8XI/AAAAAAAAAAY/nlN9DdE0IjE/s1600/staterank.jpg"><img style="cursor:pointer; cursor:hand;width: 100px; height: 400px;" src="http://2.bp.blogspot.com/-aQRKrH8j7no/TWMfpnvm8XI/AAAAAAAAAAY/nlN9DdE0IjE/s400/staterank.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5576335563518570866" /></a><br /><br />So, it seems like both sides can lay claims to evidence that back their opinions. Readers, what do you think?<img src="http://feeds.feedburner.com/~r/JoeRegansBlog/~4/4HFZ0Ooxiqs" height="1" width="1" alt=""/>Joe Reganhttp://www.blogger.com/profile/04575217740526266213noreply@blogger.com0http://joeregan.blogspot.com/2011/02/does-spending-on-education-improve-sat.htmltag:blogger.com,1999:blog-3694086169336072520.post-21031298961254097412011-01-06T20:59:00.000-08:002011-01-06T22:16:22.879-08:00How Much Increase does the Dow need to gain employment?The Dow being up is big news to many people in America. It signifies a strengthening economy, and a possible end of hard times for millions of people.<div><br /></div><div>However, the Dow Jones goes up annually. On January 2nd, 1929, the DJI closed at 307.01. January 3rd, 2011, the DJI closed at 11,670.75. Despite a stretch in time that consisted of the Great Depression and the recent recession, the DJI has managed to climb at an annual rate of 4.54%, out-pacing inflation at 3.16%.</div><div><br /></div><div>So, it is clear, over time, the Dow is a good, though not risk-free investment for an individual. A person who put the equivalent of $1 in 2011 dollars into the DJI in 1929, the person would have $2.97 now. </div><div><br /></div><div>How does this affect main street, however? Namely, how is the annual performance of the Dow reflective on unemployment rates?</div><div><br /></div><div>While one immediately assumes a growth in the Dow equals growth in employment, that is not necessarily the case. Though it is a positive sign for growth, there needs to be a level fit to meet an ever growing workforce, but how does one determine this?</div><div><br /></div><div>Now, understanding the behavior of the Dow is easy, it generally follows a curve at the rate a*(1+r)^n, where a = starting value, r = rate of growth, and n = years since the starting point.</div><div><br /></div><div>So first, to get a feel for the data, I elected to view the growth rate of the dow as a function of two variables: years since 1970, and unemployment rate of the year, found in <a href="https://spreadsheets.google.com/ccc?key=0AgJLpCNZowZMdDlBM1hfZ093VWsxQ0tkNjgwbDBDWVE&hl=en&authkey=CPWS-f4K">this table</a>, with fields of year, year since 1970, average dow jones index for the year, and average unemployment rate.</div><div><br /></div><div>My first equation is as follows:</div><div><br /></div><div><div><i><span class="Apple-style-span" >> summary(nlsdow)</span></i></div><div><i><span class="Apple-style-span" ><br /></span></i></div><div><i><span class="Apple-style-span" >Formula: dow ~ c * exp(a * yr70 + b * unemp) + c</span></i></div><div><i><span class="Apple-style-span" ><br /></span></i></div><div><i><span class="Apple-style-span" >Parameters:</span></i></div><div><i><span class="Apple-style-span" > Estimate Std. Error t value Pr(>|t|) </span></i></div><div><i><span class="Apple-style-span" >a 0.091785 0.004272 21.483 </span></i></div><div><i><span class="Apple-style-span" >b -16.314535 2.978873 -5.477 3.21e-06 ***</span></i></div><div><i><span class="Apple-style-span" >c 951.696519 134.844962 7.058 2.38e-08 ***</span></i></div><div><i><span class="Apple-style-span" >---</span></i></div><div><i><span class="Apple-style-span" >Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 </span></i></div><div><i><span class="Apple-style-span" ><br /></span></i></div><div><i><span class="Apple-style-span" >Residual standard error: 1034 on 37 degrees of freedom</span></i></div><div><i><span class="Apple-style-span" ><br /></span></i></div><div><i><span class="Apple-style-span" >Number of iterations to convergence: 8 </span></i></div><div><i><span class="Apple-style-span" >Achieved convergence tolerance: 8.044e-06 </span></i></div></div><div><br /></div><div>Or, dow = 951.7*exp(0.091785*(yr-1970)-16.315*u_rate). Logical, the more years out, the more the DJI grows, and high unemployment is a good sign of a bad economy.</div><div><br /></div><div>Now, we have an idea of the form for regression using the unemployment rate as a dependent variable (after all, unemployment as a lagging indicator means it is a better candidate for a dependent variable). Since we see the initial equation takes the form Y = a*exp(b*X1+c*X2), we can turn this into b*X1+c*X2 = ln(Y/a) = ln(Y) - ln(a) = ln(Y) + A, where A = -ln(a). </div><div><br /></div><div>Solving for X2 gives us [ln(Y) + A - b*X1]/c, and of course we can change this into u_rate = p*ln(DJI) + q*(year since 1970) + r, where p = 1/c, q = -b/c, and r = -ln(a)/c.</div><div><br /></div><div>So, simply put, one can regress for the unemployment rate by regressing against the natural log of the Dow Jones Index and the year since the particular starting point (in this case, 1970).</div><div><br /></div><div>Fortunately, transforming a variable array is easy in R. To write a variable in log space, simply enter:</div><div><br /></div><div><i><span class="Apple-style-span" >> lnDow<-log(dow)</span></i></div><div><br /></div><div>You are now all set to run the regression. Let's try it:</div><div><br /></div><div><div><i><span class="Apple-style-span" >> lmU<-lm(unemp ~ yr70 + lnDow)</span></i></div><div><i><span class="Apple-style-span" >> summary(lmU)</span></i></div><div><i><span class="Apple-style-span" ><br /></span></i></div><div><i><span class="Apple-style-span" >Call:</span></i></div><div><i><span class="Apple-style-span" >lm(formula = unemp ~ yr70 + lnDow)</span></i></div><div><i><span class="Apple-style-span" ><br /></span></i></div><div><i><span class="Apple-style-span" >Residuals:</span></i></div><div><i><span class="Apple-style-span" > Min 1Q Median 3Q Max </span></i></div><div><i><span class="Apple-style-span" >-0.0184135 -0.0058837 0.0002107 0.0038651 0.0209755 </span></i></div><div><i><span class="Apple-style-span" ><br /></span></i></div><div><i><span class="Apple-style-span" >Coefficients:</span></i></div><div><i><span class="Apple-style-span" > Estimate Std. Error t value Pr(>|t|) </span></i></div><div><i><span class="Apple-style-span" >(Intercept) 0.3038799 0.0347165 8.753 1.52e-10 ***</span></i></div><div><i><span class="Apple-style-span" >yr70 0.0028846 0.0004947 5.831 1.06e-06 ***</span></i></div><div><i><span class="Apple-style-span" >lnDow -0.0376083 0.0055375 -6.792 5.38e-08 ***</span></i></div><div><i><span class="Apple-style-span" >---</span></i></div><div><i><span class="Apple-style-span" >Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 </span></i></div><div><i><span class="Apple-style-span" ><br /></span></i></div><div><i><span class="Apple-style-span" >Residual standard error: 0.009397 on 37 degrees of freedom</span></i></div><div><i><span class="Apple-style-span" >Multiple R-squared: 0.5931, Adjusted R-squared: 0.5711 </span></i></div><div><i><span class="Apple-style-span" >F-statistic: 26.96 on 2 and 37 DF, p-value: 5.976e-08 </span></i></div></div><div><br /></div><div>I see a lot of highly significant coefficients. Though the R-squared leaves a bit to be desired, there seems to be a good deal being said through this function.</div><div><br /></div><div>The coefficients we care about, though, are for yr70 and lnDow. To get a point estimate of the growth rate needed to sustain employment, we need to know how much lnDow has to increase to equal the coefficient of yr70, and effectively balance it out to keep unemployment stable. Simple algebra says that this means: </div><div><br /></div><div>.0028846(yr70) = .0376083(lnDow), </div><div><br /></div><div>so a delta of one year means we solve </div><div><br /></div><div>.0376083(delta_lnDow) = .0028846, or delta_lnDow=.0767.</div><div><br /></div><div>To find the point-estimate percentage increase needed, we simply solve exp(.0767) - 1 = 7.972%.</div><div><br /></div><div>While high, the good news is, the DJI achieved this mark 17 of 39 years in the sample, and averages 6.5% growth a year from the average of 1970 to the average of 2009, including the downturn after 2007. </div><div><br /></div><div>So if you were to ask me what my break-even point is for a "main street approved" economy is? Watch for that DJI to crack 8% growth during the year.</div><img src="http://feeds.feedburner.com/~r/JoeRegansBlog/~4/8a9aqS3ARlU" height="1" width="1" alt=""/>Joe Reganhttp://www.blogger.com/profile/04575217740526266213noreply@blogger.com0http://joeregan.blogspot.com/2011/01/how-much-increase-does-dow-need-to-gain.htmltag:blogger.com,1999:blog-3694086169336072520.post-60545324655848905522011-01-03T20:18:00.001-08:002011-01-03T21:10:48.277-08:00The Logit Model and Baseball: Projecting Chance of Making the Hall of FameIt is the favorite time of year for many a sports nerd like myself: the time when the Baseball Writers Association of America will make their picks for the Hall of Fame, and when the blogosphere is best equipped to mock and ridicule the inconsistent logic of many esteemed writers.<div><br /></div><div>It is also a favorite time for anyone who has ever said "I cannot believe they voted for...", or how someone was robbed (see Whitaker, Lou). </div><div><br /></div><div>Last year's epic battle, in my mind, was the one about Edgar Martinez. Supporters cited his batting numbers that were comparable to legends while playing in a mediocre hitting park, and his career that shows no signs of PED use. His detractors cited that he was a Designated Hitter, and that his career is short.</div><div><br /></div><div>When all was said and done, Martinez received a mere 36.2% of the vote, less than half what one needs to reach the Hall of Fame. So, what would Edgar's chance of reaching Cooperstown, knowing this?</div><div><br /></div><div>Would you believe a 69.09% chance?</div><div><br /></div><div>It seems counter-intuitive that when one is yet to convince almost two-thirds of the remaining voting base of his greatness, 6 years after his career ended, that anything would change so rapidly. However, it occurs constantly, as only 2 men from 1976-97 received a higher share of the vote on their first ballot and missed out on the writer's election. One of which, Jim Bunning, eventually gained access through the Veterans Committee. </div><div><br /></div><div>As we saw from <a href="http://joeregan.blogspot.com/2011/01/logarithmic-regression-analysis-in-r.html">yesterday's post</a>, the logit model can provide a powerful probability estimator given a dummy dependent variable. In this case, we test whether someone reached the Hall of Fame (y=1) or not (y=0).</div><div><br /></div><div>To perform this analysis, I looked at all Hall of Fame votes from 1976-1997, and took the percent share of the vote obtained by all players on their first ballot, excluding those who received less than 5% of the vote (indicating a probability of being elected to the Hall of 0, and a small chance of being elected by the veterans' committee). Through this process, I obtained a data sheet of 59 players, as can be seen <a href="https://docs.google.com/leaf?id=0BwJLpCNZowZMNzY0ZGRjMzItMjQ1Zi00MGUwLWI4MTUtYTM5NGY5MjY3ZjQ5&hl=en&authkey=CPaa0oUD">here</a>.</div><div><br /></div><div>Right away, one can make general assumptions. Thirty-three of the fifty-nine players listed were eventually elected to the Hall, a 55.93% success rate. Additionally, 3 more were elected by the veterans' committee, leaving the total success rate of the group at 61.02%. Simply clearing the first obstacle of making it past the first ballot seems to bode well for the eventual success of candidates.</div><div><br /></div><div>However, this analysis is imperfect. The success rate includes players who were elected on the first ballot, and had no resistance in making the Hall. Once again, though, we can easily run a logit model regression on the data. Yesterday I showed a sample script that can run all the needed commands for you, but today I'll simply enter these in, command by command, and show you another useful feature: creating a function.</div><div><br /></div><div>Yesterday I showed you the format for writing a logit model regression, so let's skip right to a summary. This regresses share of the first ballot vote to probability of eventually making the Hall of Fame:</div><div><br /></div><div><div><span class="Apple-style-span"><i>> summary(probabilityHOF)</i></span></div><div><span class="Apple-style-span"><i><br /></i></span></div><div><span class="Apple-style-span"><i>Formula: hof ~ 1/(1 + exp(-1 * (A + B * initial)))</i></span></div><div><span class="Apple-style-span"><i><br /></i></span></div><div><span class="Apple-style-span"><i>Parameters:</i></span></div><div><span class="Apple-style-span"><i> Estimate Std. Error t value Pr(>|t|) </i></span></div><div><span class="Apple-style-span"><i>A -3.0108 0.7995 -3.766 0.000395 ***</i></span></div><div><span class="Apple-style-span"><i>B 10.5387 3.0206 3.489 0.000942 ***</i></span></div><div><span class="Apple-style-span"><i>---</i></span></div><div><span class="Apple-style-span"><i>Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 </i></span></div><div><span class="Apple-style-span"><i><br /></i></span></div><div><span class="Apple-style-span"><i>Residual standard error: 0.2891 on 57 degrees of freedom</i></span></div><div><span class="Apple-style-span"><i><br /></i></span></div><div><span class="Apple-style-span"><i>Number of iterations to convergence: 6 </i></span></div><div><span class="Apple-style-span"><i>Achieved convergence tolerance: 8.11e-06 </i></span></div></div><div><br /></div><div>And this, in turn, summarizes the probability of being elected by the BBWAA:</div><div><br /></div><div><div><i><span class="Apple-style-span">> summary(probHOFbbwaa)</span></i></div><div><i><span class="Apple-style-span"><br /></span></i></div><div><i><span class="Apple-style-span">Formula: hof_bbwaa ~ 1/(1 + exp(-1 * (A + B * initial)))</span></i></div><div><i><span class="Apple-style-span"><br /></span></i></div><div><i><span class="Apple-style-span">Parameters:</span></i></div><div><i><span class="Apple-style-span"> Estimate Std. Error t value Pr(>|t|) </span></i></div><div><i><span class="Apple-style-span">A -3.5384 0.8171 -4.330 6.1e-05 ***</span></i></div><div><i><span class="Apple-style-span">B 10.4765 2.5601 4.092 0.000136 ***</span></i></div><div><i><span class="Apple-style-span">---</span></i></div><div><i><span class="Apple-style-span">Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 </span></i></div><div><i><span class="Apple-style-span"><br /></span></i></div><div><i><span class="Apple-style-span">Residual standard error: 0.2479 on 57 degrees of freedom</span></i></div><div><i><span class="Apple-style-span"><br /></span></i></div><div><i><span class="Apple-style-span">Number of iterations to convergence: 19 </span></i></div><div><i><span class="Apple-style-span">Achieved convergence tolerance: 5.858e-06 </span></i></div></div><div><br /></div><div>First thing to stand out is that the equations are very similar. The coefficients are strong on both regressions, though a bit stronger on the second one, measuring whether or not a player would be elected by the sports writers or not.</div><div><br /></div><div>This is fine if we want to hard code, but we would rather not: we would rather simply enter a value in question and have R return a probability for us. Well this can be done simply using a function.</div><div><br /></div><div>As shown at the <a href="http://fs6.depauw.edu:50080/~harvey/Chem%20351/PDF%20Files/Handouts/RDocs/Writing%20Functions%20Using%20R.pdf">Depauw website</a>, writing a function is performed by: </div><div><i>name=function(argument 1, argument 2, etc...) { [expression] }</i><br /></div><div><i><br /></i></div><div>Since we have the coefficients available at our disposal from our previous equations, let's define functions. First, a function to determine the probability of being elected into the Hall of Fame by the sports writers:</div><div><br /></div><div><div><i><span class="Apple-style-span">> xChance=function(x) {</span></i></div><div><i><span class="Apple-style-span">+ 1/(1+exp(-1*(-3.5384+10.4765*x))) }</span></i></div></div><div><br /></div><div>Then, a second one, to determine the probability of reaching the Hall via the BBWAA or the Veterans' Committee:</div><div><br /></div><div><div><i><span class="Apple-style-span">> xHOF=function(x) {</span></i></div><div><i><span class="Apple-style-span">+ 1/(1+exp(-1*(-3.0108+10.5387*x))) }</span></i></div></div><div><br /></div><div>So of course, the last question is, how do you use a function? Well, that's the simplest part of all, and will help us obtain our answer. All you need to do is write in this format: </div><div><i>FnName(value)</i></div><div><i><br /></i></div><div>So, let's try both of these equations where x=.362, or Edgar Martinez's share of votes in 2010:</div><div><br /></div><div><div><i><span class="Apple-style-span">> xChance(0.362)</span></i></div><div><i><span class="Apple-style-span">[1] 0.5631837</span></i></div><div><i><span class="Apple-style-span">> <span class="blsp-spelling-error" id="SPELLING_ERROR_0">xHOF</span>(0.362)</span></i></div><div><i><span class="Apple-style-span">[1] 0.6908742</span></i></div></div><div><br /></div><div>Well, these are probability moderately encouraging for my fellow Edgar fans. Not even including the Veterans Committee option, Edgar currently stands as better than a coin flip chance of reaching the Hall, at 56.32%. With the <span class="blsp-spelling-error" id="SPELLING_ERROR_1">Veterans Committee</span>, this probability spikes to 69.09%.</div><div><br /></div><div>So where are the break-even (50-50) points for both equations? For just the <span class="blsp-spelling-error" id="SPELLING_ERROR_2">BBWAA</span> vote, it is at around 33.8% of the initial vote. For overall Hall of Fame chances, it is at around 28.6%.</div><div><br /></div><div>After the fifth, we can re-visit this post and project the chances of ballot newcomers like Jeff <span class="blsp-spelling-error" id="SPELLING_ERROR_3">Bagwell</span> and Larry Walker. For now, rest assured that Barry <span class="blsp-spelling-error" id="SPELLING_ERROR_4">Larkin</span> has a 91.9% chance of reaching Cooperstown, and Robbie <span class="blsp-spelling-error" id="SPELLING_ERROR_5">Alomar</span> is almost a stone cold lock. Heck, even Fred <span class="blsp-spelling-error" id="SPELLING_ERROR_6">McGriff</span> has a 32.2% chance of reaching the Hall someday given his first ballot performance.</div><div><br /></div><img src="http://feeds.feedburner.com/~r/JoeRegansBlog/~4/xCwpNTliPIg" height="1" width="1" alt=""/>Joe Reganhttp://www.blogger.com/profile/04575217740526266213noreply@blogger.com0http://joeregan.blogspot.com/2011/01/logit-model-and-baseball-projecting.htmltag:blogger.com,1999:blog-3694086169336072520.post-3504171178690038182011-01-02T16:34:00.000-08:002011-01-02T18:06:02.698-08:00Logistic Regression Analysis in R-programmingThis time last year, I had a very vague knowledge of Logistic Analysis, an analysis that is now a cornerstone to my profession.<div><br /></div><div>Many times as an analyst, I come across the various question of whether or not a person acted upon an advertisement (y=1), or not (y=0), and the various attributes (independent variables) that factored into the equation.</div><div><br /></div><div>For a more broad audience, and to summarize this concept further, let's take the role of a college professor, who teaches the same course to a total of 210 students over the course of a school year, and wants to determine the point where a student has a 80% probability of getting at least a B on the final exam.</div><div><br /></div><div>After looking over the 210 students, the professor formed <a href="https://spreadsheets.google.com/ccc?key=0AgJLpCNZowZMdDRZWWRDQ1ZrQ3lDb3BPbHFrT3NPOFE&hl=en&authkey=CJ3ElbgB">this excel file</a>, with two very simple variables: an independent variable of hours spent studying, and a dependent variable of whether or not a B-grade was achieved. </div><div><br /></div><div>Could we use a linear regression here? It would show us a positive or negative correlation and...not much else. Since we would have a cluster of 121 points at y=1, and 89 at y=0, this would not really describe much of anything.</div><div><br />Enter logistic regression.</div><div><br /></div><div>What is logistic regression? Per <a href="http://en.wikipedia.org/wiki/Logistic_regression">wikipedia</a>, it is "used for prediction of the <a href="http://en.wikipedia.org/wiki/Probability" title="Probability" style="text-decoration: none; color: rgb(6, 69, 173); background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; ">probability</a> of occurrence of an event by fitting data to a logit function <a href="http://en.wikipedia.org/wiki/Logistic_curve" title="Logistic curve" class="mw-redirect" style="text-decoration: none; color: rgb(6, 69, 173); background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; ">logistic curve</a>." Essentially, it is transforming the data into a model where the dependent variable becomes a probability prediction.</div><div><br /></div><div>The form of a logit model is:<br />f(z) = 1/[1-exp(-z)], where z = B0 + B1X1 + B2X2 + ... + BnXn. In the previous example, we only have one independent variable X1, so z = A + BX. </div><div><br /></div><div>Now that we have the gritty covered, how about the actual analysis? </div><div><br /></div><div>Well, this can be performed with a script in R. The best part of all, a "lengthy" version of this only requires 14 lines!</div><div><br /></div><div><div><span class="Apple-style-span"><i>#!/usr/bin/env Rscript</i></span></div><div><span class="Apple-style-span"><i><br /></i></span></div><div><span class="Apple-style-span"><i>bgrade<-read.csv("C:/Users/Joseph/Documents/Blogfolder/bgrade.csv", header=TRUE)</i></span></div><div><span class="Apple-style-span"><i>Yreal<-bgrade$b_grade</i></span></div><div><span class="Apple-style-span"><i>Xreal<-bgrade$studytime</i></span></div><div><span class="Apple-style-span"><i>nlsProb<-nls(Yreal ~ 1/(1+exp(-1*(A+B*Xreal))),start=list(A=-0.5,B=0.05),control=nls.control(maxiter=20000))</i></span></div><div><span class="Apple-style-span"><i>Coeffs<-coefficients(nlsProb)</i></span></div><div><span class="Apple-style-span"><i>xAchieve<-NULL</i></span></div><div><span class="Apple-style-span"><i>A = Coeffs[[1]][[1]]</i></span></div><div><span class="Apple-style-span"><i>B = Coeffs[[2]][[1]]</i></span></div><div><span class="Apple-style-span"><i>for(i in 1:20) {</i></span></div><div><span class="Apple-style-span"><i><span class="Apple-tab-span" style="white-space:pre"> </span>xAchieve[i] = 1/(1+exp(-1*(A + B*i)))</i></span></div><div><span class="Apple-style-span"><i>}</i></span></div><div><span class="Apple-style-span"><i>xTime = (log(4)-A)/B</i></span></div></div><div><br /></div><div>Let's analyze what is happening. </div><div>Line 1: Shebang line. Let's the system know you're about to use R.</div><div>Line 3: Reads the .csv file into a data frame (and indicates headers in the file).</div><div>Lines 4+5: Assigns our independent and dependent fields to variable arrays.</div><div>Line 6: Determines equation. This is the "kicker" here, and it's important to know two things:</div><div><ol><li>You SHOULD have a general idea of how the equation should fit, or else it may not reduce. In this example, I assumed a less than 50% chance of achieving a B would occur when x=0, and as x grew, so would y (hence A = -0.5, B = 0.05)</li><li>control=nls.control is an important control here. The default amount of iterations is 50, though I often spike it to 20,000 (I've never had a major runtime issue as a result, so I highly suggest using as many iterations as you need).</li></ol><div>Line 7: Writes coefficients to variable, this will allow us to determine where the 90% breakthrough point is without leaving our script.</div></div><div>Line 8: Declare an empty array for later use.</div><div>Lines 9+10: Write coefficients to variables to use in for loop.</div><div>Lines 11+13: For loop. Makes a nifty array (xAchieve) where one can input any integer from 1-20 (in the form xAchieve[i]) later, or print out the entire array to map x hours of studying to a certain percent chance of achievement.</div><div>Line 14: Simply the logit model, now solving for x with a Y set at 0.8, or 80% (determining our breakthrough point).</div><div><br /></div><div>From here, it's as simple as opening up an R GUI interface, or starting R from the command prompt (whatever your preference, I'm a Windows user and prefer the GUI) and type:</div><div><br /></div><div><i><span class="Apple-style-span">source("(path)<path>/bgrade.r")</path></span></i></div><div><br /></div><div>Now it's all written out! Let's take a look at some of our key outputs.</div><div><div><i><span class="Apple-style-span">> summary(nlsProb)</span></i></div><div><i><span class="Apple-style-span"><br /></span></i></div><div><i><span class="Apple-style-span">Formula: Yreal ~ 1/(1 + exp(-1 * (A + B * Xreal)))</span></i></div><div><i><span class="Apple-style-span"><br /></span></i></div><div><i><span class="Apple-style-span">Parameters:</span></i></div><div><i><span class="Apple-style-span"> Estimate Std. Error t value Pr(>|t|) </span></i></div><div><i><span class="Apple-style-span">A -1.30595 0.30481 -4.284 2.80e-05 ***</span></i></div><div><i><span class="Apple-style-span">B 0.16816 0.03072 5.475 1.26e-07 ***</span></i></div><div><i><span class="Apple-style-span">---</span></i></div><div><i><span class="Apple-style-span">Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 </span></i></div><div><i><span class="Apple-style-span"><br /></span></i></div><div><i><span class="Apple-style-span">Residual standard error: 0.4433 on 208 degrees of freedom</span></i></div><div><i><span class="Apple-style-span"><br /></span></i></div><div><i><span class="Apple-style-span">Number of iterations to convergence: 4 </span></i></div><div><i><span class="Apple-style-span">Achieved convergence tolerance: 7.859e-06 </span></i></div><div><i><span class="Apple-style-span"><br /></span></i></div><div><i><span class="Apple-style-span">> xAchieve</span></i></div><div><i><span class="Apple-style-span"> [1] 0.2427264 0.2749548 0.3097123 0.3467622 0.3857673 0.4262967 0.4678409 0.5098359 0.5516924 0.5928293 0.6327044 0.6708427 0.7068557 0.7404530 0.7714447 0.7997367</span></i></div><div><i><span class="Apple-style-span">[17] 0.8253195 0.8482544 0.8686572 0.8866832</span></i></div><div><i><span class="Apple-style-span">> xTime</span></i></div><div><i><span class="Apple-style-span">[1] 16.00978</span></i></div></div><div><br /></div><div>What do these three commands mean? </div><div>The first shows the summary of our logit model. While my assumptions where right, my estimations were off. A=-1.30595, and B=0.16816.</div><div><br /></div><div>The second is our array xAchieve. The first data point is a student that only studies for 1 hour can be expected to score a B or higher only 24.27% of the time. Yikes. At the other end, a student that studies for 20 hours can be expected to score a B or higher 88.67% of the time. </div><div><br /></div><div>The third is the "answer" to our initial question of where the 80% breakthrough point is. As our array shows us reasonably, and xTime shows us more precisely, it is right around 16 hours. Apparently this professor does not believe in grade inflation.</div><div><br /></div><div>After you are done with the data, a simple command:</div><div><br /></div><div><span class="Apple-style-span"><i>rm(list=ls())</i></span></div><div><br /></div><div>Cleans up your workspace for more analysis. </div><div><br /></div><div>Clearly, the ability to perform robust analysis is powerful using the logit model. It was <a href="http://professionals.collegeboard.com/profdownload/pdf/RN-30.pdf">used recently by The College Board</a> to make a powerful conclusion: that an 1180 on the old-SAT was the "breakthrough" point where a student had a 65% chance of achieving just a 2.7 GPA in their freshman year (a telling statistic in an age of grade inflation and more students attending college than ever). </div><div><br /></div><div>The logit model also is a powerful analysis for marketing and advertising, to measure various aspects of an advertisement, the way it was published, and various other factors to determine how each independent variable affects the one major question: whether or not it causes the individual to act upon the advertisement or not. While an important analysis for a statistician, knowledge of the logit model is valuable for all sorts of professions, from biology and academia, to business and marketing. </div><img src="http://feeds.feedburner.com/~r/JoeRegansBlog/~4/OOyBSrxuwRY" height="1" width="1" alt=""/>Joe Reganhttp://www.blogger.com/profile/04575217740526266213noreply@blogger.com1http://joeregan.blogspot.com/2011/01/logarithmic-regression-analysis-in-r.htmltag:blogger.com,1999:blog-3694086169336072520.post-50403732969696019902011-01-02T01:07:00.000-08:002011-01-02T01:29:28.596-08:00Simple Statistics 101: How To Write a Perl Script that performs a Hypothesis TestThe hypothesis test.<div><br /></div><div>It's something many of us have, and will, encounter on a regular basis in many courses, and in professional settings.</div><div><br /></div><div>This initial blog post is, simply put, an easy way to show the potential power that just a little bit of programming knowledge can have on data analysis and manipulation. After the jump is a sample script I wrote in Perl that accepts an input of values, and a test point, and prints out summary statistics.</div><div><br /></div><div><div><span class="Apple-style-span" ><i>#!/usr/bin/perl</i></span></div><div><span class="Apple-style-span" ><i><br /></i></span></div><div><span class="Apple-style-span" ><i>use URI;</i></span></div><div><span class="Apple-style-span" ><i>use URI::Escape;</i></span></div><div><span class="Apple-style-span" ><i>use URI::Split qw(uri_split uri_join);</i></span></div><div><span class="Apple-style-span" ><i><br /></i></span></div><div><span class="Apple-style-span" ><i>print "Enter numbers, seperated by commas: \n";</i></span></div><div><span class="Apple-style-span" ><i>my $list = <>;</i></span></div><div><span class="Apple-style-span" ><i>print "Enter test point for z-score:\n";</i></span></div><div><span class="Apple-style-span" ><i>my $hypothesis = <>;</i></span></div><div><span class="Apple-style-span" ><i><br /></i></span></div><div><span class="Apple-style-span" ><i>my @nums = split(/,/, $list);</i></span></div><div><span class="Apple-style-span" ><i>$total = scalar(@nums);</i></span></div><div><span class="Apple-style-span" ><i><br /></i></span></div><div><span class="Apple-style-span" ><i>my $sum = 0;</i></span></div><div><span class="Apple-style-span" ><i>foreach(@nums) {</i></span></div><div><span class="Apple-style-span" ><i><span class="Apple-tab-span" style="white-space:pre"> </span>chomp;</i></span></div><div><span class="Apple-style-span" ><i><span class="Apple-tab-span" style="white-space:pre"> </span>my $num = $_;</i></span></div><div><span class="Apple-style-span" ><i><span class="Apple-tab-span" style="white-space:pre"> </span>$sum = $sum + $num;</i></span></div><div><span class="Apple-style-span" ><i>}</i></span></div><div><span class="Apple-style-span" ><i><br /></i></span></div><div><span class="Apple-style-span" ><i>my $mean = $sum / $total;</i></span></div><div><span class="Apple-style-span" ><i>my $varsum = 0;</i></span></div><div><span class="Apple-style-span" ><i>foreach(@nums) {</i></span></div><div><span class="Apple-style-span" ><i><span class="Apple-tab-span" style="white-space:pre"> </span>chomp;</i></span></div><div><span class="Apple-style-span" ><i><span class="Apple-tab-span" style="white-space:pre"> </span>my $num2 = $_;</i></span></div><div><span class="Apple-style-span" ><i><span class="Apple-tab-span" style="white-space:pre"> </span>$varsum = $varsum + ($num2-$mean)**2;</i></span></div><div><span class="Apple-style-span" ><i>}</i></span></div><div><span class="Apple-style-span" ><i><br /></i></span></div><div><span class="Apple-style-span" ><i>my $popvar = $varsum / $total;</i></span></div><div><span class="Apple-style-span" ><i>my $sampvar = $varsum / ($total - 1);</i></span></div><div><span class="Apple-style-span" ><i>my $popsd = $popvar**0.5;</i></span></div><div><span class="Apple-style-span" ><i>my $poperr = $popsd / ($total**0.5);</i></span></div><div><span class="Apple-style-span" ><i>my $sampsd = $sampvar**0.5;</i></span></div><div><span class="Apple-style-span" ><i>my $samperr = $sampsd / ($total**0.5);</i></span></div><div><span class="Apple-style-span" ><i>my $zpop = ($hypothesis - $mean) / $poperr;</i></span></div><div><span class="Apple-style-span" ><i>my $zsamp = ($hypothesis - $mean) / $samperr;</i></span></div><div><span class="Apple-style-span" ><i><br /></i></span></div><div><span class="Apple-style-span" ><i>my $m = sprintf("%.3f", $mean);</i></span></div><div><span class="Apple-style-span" ><i>my $p = sprintf("%.4f", $popsd);</i></span></div><div><span class="Apple-style-span" ><i>my $s = sprintf("%.4f", $sampsd);</i></span></div><div><span class="Apple-style-span" ><i>my $zp = sprintf("%.4f", $zpop);</i></span></div><div><span class="Apple-style-span" ><i>my $zs = sprintf("%.4f", $zsamp);</i></span></div><div><span class="Apple-style-span" ><i><br /></i></span></div><div><span class="Apple-style-span" ><i>print "Sample:\t$total\nMean:\t$m\nPop. St.Dev\t$p\nPop Z-score:\t$zp\nSample St.Dev\t$s\nSample Z-score:\t$zs\n";</i></span></div></div><div><br /></div><div>Not always the cleanest, but it is effective. Let's analyze what is occurring in every block of code:</div><div>1) Accepts the list of data points, and the hypothesis (example, testing to see if a track coach's claim of improving his athlete's 100 meter times or not, the null can be set at 0.0 seconds).</div><div>2) Splits the numbers, counts the total and sums.</div><div>3) Determines mean, and then determines the sum of squares.</div><div>4) Determines two separate lists of statistics: one if the list is a population, one if the list is a sample (though this is easy enough to write into the input fields, this script outputs both).</div><div>5) Prints results, and sample t-scores.</div><div><br /></div><div>Of course, this is hardly credible without a trial run, so let's test this out.</div><div><br /></div><div><div><span class="Apple-style-span" ><i>$ perl hyptestcalc.pl</i></span></div><div><span class="Apple-style-span" ><i>Enter numbers, seperated by commas:</i></span></div><div><span class="Apple-style-span" ><i>0.2,0.2,0,-0.1,0,0.2,0.1,0,-0.2,0.1,0.3,0.4,0,-0.2,0.1,0.1</i></span></div><div><span class="Apple-style-span" ><i>Enter test point for z-score:</i></span></div><div><span class="Apple-style-span" ><i>0</i></span></div><div><span class="Apple-style-span" ><i>Sample: 16</i></span></div><div><span class="Apple-style-span" ><i>Mean: 0.075</i></span></div><div><span class="Apple-style-span" ><i>Pop. St.Dev 0.1601</i></span></div><div><span class="Apple-style-span" ><i>Pop Z-score: -1.8741</i></span></div><div><span class="Apple-style-span" ><i>Sample St.Dev 0.1653</i></span></div><div><span class="Apple-style-span" ><i>Sample Z-score: -1.8146</i></span></div></div><div><span class="Apple-style-span" ><i><br /></i></span></div><div><span class="Apple-style-span" >In this case, we continued on the "track coach" theme. Using a 16 athlete sample, we saw the mean improvement in times for the group was 0.075 seconds, with a standard deviation in the sample of 0.1653 seconds. This results in a Sample z-score of -1.8146.</span></div><div><span class="Apple-style-span" ><br /></span></div><div><span class="Apple-style-span" >Using our <a href="http://www.sjsu.edu/faculty/gerstman/StatPrimer/t-table.pdf">t-table</a>, given degrees of freedom of 15, we would see that 0 falls below the 1-tailed t-test of -1.753 (alpha = 0.05), but inside alpha=0.025 (test score of -2.131). Therefore, it would make it logical to say that the probability that this track coach improves the times of his clients is between 95% and 97.5%, and probably around 96%.</span></div><div><span class="Apple-style-span" ><br /></span></div><div><span class="Apple-style-span" >In the meantime, any new readers of my blog can feel free to contribute all sorts of ideas in the mix, whether is be a specific analysis piece, how to write certain blocks of code in various data-intensive languages like R and Perl, sports analysis, and many other topics.</span></div><img src="http://feeds.feedburner.com/~r/JoeRegansBlog/~4/otAR94KO1mQ" height="1" width="1" alt=""/>Joe Reganhttp://www.blogger.com/profile/04575217740526266213noreply@blogger.com0http://joeregan.blogspot.com/2011/01/simple-statistics-101-how-to-write-perl.html