I often use R to create graphs for my web applications which are normally developed in PHP. I use the PHP command "exec" to shell out to the R script, passing any parameters on the command line. The params are then available in R using the commandArgs function. How to get the data back is another problem, when creating graphs its no biggy, I just create the graph as a file (using bitmap(type='png254') as the output device since png() wont function without an X environment). For simpler numeric data I guess at the worst you could write that to a text file then parse that but there may be better ways. On Fri, 2006-12-05 at 12:39 -0700, Shawn Mikula wrote:> Using R for this would be a bit bone-headed since it is not ideally suited > for web apps. Look at an active scripting language like PHP. I'm not sure > if Geocities provides for server-side active scripting languages, in which > case you will have to host your site elsewhere. > > > > ----- Original Message ----- > From: "percy tiglao" <prtiglao at gmail.com> > To: <r-help at stat.math.ethz.ch> > Sent: Friday, May 12, 2006 11:07 AM > Subject: [R] Newbie to R: would like to solve a problem > > > > Hello, I am very new to R (read the introduction a few hours ago), but > > experianced in several other languages (including Scheme, C, ObjC and > > C++, Java and Javascript) and I was wondering how to approach this > > problem: > > > > I am making a training calculator for a Video Game I play. Basically, > > I want to calculate the probability of killing an enemy within x hits, > > to help determine which is the best monster to train on in this game. > > > > The current implementation > > (http://geocities.com/dragontamer5788/maple2.html) uses the normal > > curve to estimate just about everything. Not necessarily a bad idea > > for when it takes 10 or 15 hits to destroy a single enemy, Central > > Limit Theorm kicks in and normal curve is probably a good idea, but my > > gut feeling is that it is not accurate for estimating kills in 1 or 2 > > shots (which happens in the majority of "training" monsters), or the > > probability of Knockback (which is always determined by a single > > shot). > > > > The (simplified) function to represent the damage would be: > > > > sdmg <- function(min, max, skill){ > > if(runif(1) > .4) (runif(1) * (max-min) + min) * skill > > else (runif(1) * (max-min) + min) * (skill+1) > > } > > > > total_damage <- function(min, max, skill, num_attacks){ > > total <- 0 > > for(i in 1:num_attacks){ > > total <- total + sdmg(min, max, skill) > > } > > total > > } > > > > -------------------- > > > > The above is how damage is calculated. So the probability distribution > > looks like 2 rectangles next to each other, one with height .6 and the > > other with height .4. Sdmg is a helper function, because some attacks > > attack 2 or 4 times in a single turn. > > > > Though, I dont want the simulation (what I'm doing with the runif > > here). I'd like to calculate the probability distribution of > > total_damage, total_damage + total damage (the convolution, > > representing 2 hits), and so on till about 5-8 hits (then I can just > > use normal curve in the web-calculator I'm making), then have a > > polynomial function estimate the probability distributions based on > > min, max, and skill. (So I can make my online calculator estimate the > > probability of killing enemies) > > > > Erm, I know it is a lot to ask for, but how would I go about making > > this in R? And if R isn't the right tool for this, is there any other > > tool you'd reccomend? Cause doing convolutions by hand sucks. > > > > Thanks a lot for reading this long message :) And thanks in advance > > for your help. > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Hello, I am very new to R (read the introduction a few hours ago), but experianced in several other languages (including Scheme, C, ObjC and C++, Java and Javascript) and I was wondering how to approach this problem: I am making a training calculator for a Video Game I play. Basically, I want to calculate the probability of killing an enemy within x hits, to help determine which is the best monster to train on in this game. The current implementation (http://geocities.com/dragontamer5788/maple2.html) uses the normal curve to estimate just about everything. Not necessarily a bad idea for when it takes 10 or 15 hits to destroy a single enemy, Central Limit Theorm kicks in and normal curve is probably a good idea, but my gut feeling is that it is not accurate for estimating kills in 1 or 2 shots (which happens in the majority of "training" monsters), or the probability of Knockback (which is always determined by a single shot). The (simplified) function to represent the damage would be: sdmg <- function(min, max, skill){ if(runif(1) > .4) (runif(1) * (max-min) + min) * skill else (runif(1) * (max-min) + min) * (skill+1) } total_damage <- function(min, max, skill, num_attacks){ total <- 0 for(i in 1:num_attacks){ total <- total + sdmg(min, max, skill) } total } -------------------- The above is how damage is calculated. So the probability distribution looks like 2 rectangles next to each other, one with height .6 and the other with height .4. Sdmg is a helper function, because some attacks attack 2 or 4 times in a single turn. Though, I dont want the simulation (what I'm doing with the runif here). I'd like to calculate the probability distribution of total_damage, total_damage + total damage (the convolution, representing 2 hits), and so on till about 5-8 hits (then I can just use normal curve in the web-calculator I'm making), then have a polynomial function estimate the probability distributions based on min, max, and skill. (So I can make my online calculator estimate the probability of killing enemies) Erm, I know it is a lot to ask for, but how would I go about making this in R? And if R isn't the right tool for this, is there any other tool you'd reccomend? Cause doing convolutions by hand sucks. Thanks a lot for reading this long message :) And thanks in advance for your help.
Using R for this would be a bit bone-headed since it is not ideally suited for web apps. Look at an active scripting language like PHP. I'm not sure if Geocities provides for server-side active scripting languages, in which case you will have to host your site elsewhere. ----- Original Message ----- From: "percy tiglao" <prtiglao@gmail.com> To: <r-help@stat.math.ethz.ch> Sent: Friday, May 12, 2006 11:07 AM Subject: [R] Newbie to R: would like to solve a problem> Hello, I am very new to R (read the introduction a few hours ago), but > experianced in several other languages (including Scheme, C, ObjC and > C++, Java and Javascript) and I was wondering how to approach this > problem: > > I am making a training calculator for a Video Game I play. Basically, > I want to calculate the probability of killing an enemy within x hits, > to help determine which is the best monster to train on in this game. > > The current implementation > (http://geocities.com/dragontamer5788/maple2.html) uses the normal > curve to estimate just about everything. Not necessarily a bad idea > for when it takes 10 or 15 hits to destroy a single enemy, Central > Limit Theorm kicks in and normal curve is probably a good idea, but my > gut feeling is that it is not accurate for estimating kills in 1 or 2 > shots (which happens in the majority of "training" monsters), or the > probability of Knockback (which is always determined by a single > shot). > > The (simplified) function to represent the damage would be: > > sdmg <- function(min, max, skill){ > if(runif(1) > .4) (runif(1) * (max-min) + min) * skill > else (runif(1) * (max-min) + min) * (skill+1) > } > > total_damage <- function(min, max, skill, num_attacks){ > total <- 0 > for(i in 1:num_attacks){ > total <- total + sdmg(min, max, skill) > } > total > } > > -------------------- > > The above is how damage is calculated. So the probability distribution > looks like 2 rectangles next to each other, one with height .6 and the > other with height .4. Sdmg is a helper function, because some attacks > attack 2 or 4 times in a single turn. > > Though, I dont want the simulation (what I'm doing with the runif > here). I'd like to calculate the probability distribution of > total_damage, total_damage + total damage (the convolution, > representing 2 hits), and so on till about 5-8 hits (then I can just > use normal curve in the web-calculator I'm making), then have a > polynomial function estimate the probability distributions based on > min, max, and skill. (So I can make my online calculator estimate the > probability of killing enemies) > > Erm, I know it is a lot to ask for, but how would I go about making > this in R? And if R isn't the right tool for this, is there any other > tool you'd reccomend? Cause doing convolutions by hand sucks. > > Thanks a lot for reading this long message :) And thanks in advance > for your help. > > ______________________________________________ > R-help@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide!http://www.R-project.org/posting-guide.html [[alternative HTML version deleted]]
Not sure I completely followed (especially what a Knockback is), but I'll give this a try. If you want to precisely define the convolution of multiple attacks, you would want to use a language that utilizes symbolic integration, which R currently does not support (e.g. see Mathematica). That said, it sounds like you don't actually want a precise answer, but you want to have a "polynomial function estimate" the true distribution. If this is the case, then it seems like doing a simulation may be precisely what you want. That is, model a polynomial approximation of the total damage from multiple attacks as a function of the number of attacks. For instance the function below will plot what the distribution of N multiple attacks would look like: distributionFun <- function(N, min, max, skill, num_attacks) { hist(replicate(1000, sum(replicate(N, total_damage(min, max, skill, num_attacks))))) } and sum(replicate(N, total_damage(min, max, skill, num_attacks))) will give you a sample from this distribution. Thus, distributionFun(1, 10, 20, 1, 1) will show the piecewise constant distribution that you would expect from a single attack, and setting N to a large number produces a Gaussian distribution (CLT). [NB: Here the N and num_attacks arguments may be redundant, but it's not clear to me how these multiple attacks work (i.e. are there multiple attacks multiple times, etc.)] Once you have the simulated distribution, you can either create a polynomial model as a function of N and use these values as inputs into your web simulation, or you can use the code above to grab a single sample from the distribution. Also, I imagine you would get better results modeling the moments of the distribution (mean, variance, skew, kurtosis, etc.) rather than using a polynomial function, but up to you. Also, if you're only doing this for the first 8 attacks, then integrating a piecewise constant function by hand (if Mathematica is unavailable) is not too difficult, and running simulations may be going overboard. Lastly, just as an FYI, your functions can be rewritten a bit more concisely as such: sdmg <- function(min, max, skill) { runif(1, min, max) * ifelse(runif(1) > 0.4, skill, skill + 1) } total_damage <- function(min, max, skill, num_attacks) { sum(replicate(num_attacks, sdmg(min, max, skill)) } Good luck, Robert -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of percy tiglao Sent: Friday, May 12, 2006 2:08 PM To: r-help at stat.math.ethz.ch Subject: [R] Newbie to R: would like to solve a problem Hello, I am very new to R (read the introduction a few hours ago), but experianced in several other languages (including Scheme, C, ObjC and C++, Java and Javascript) and I was wondering how to approach this problem: I am making a training calculator for a Video Game I play. Basically, I want to calculate the probability of killing an enemy within x hits, to help determine which is the best monster to train on in this game. The current implementation (http://geocities.com/dragontamer5788/maple2.html) uses the normal curve to estimate just about everything. Not necessarily a bad idea for when it takes 10 or 15 hits to destroy a single enemy, Central Limit Theorm kicks in and normal curve is probably a good idea, but my gut feeling is that it is not accurate for estimating kills in 1 or 2 shots (which happens in the majority of "training" monsters), or the probability of Knockback (which is always determined by a single shot). The (simplified) function to represent the damage would be: sdmg <- function(min, max, skill){ if(runif(1) > .4) (runif(1) * (max-min) + min) * skill else (runif(1) * (max-min) + min) * (skill+1) } total_damage <- function(min, max, skill, num_attacks){ total <- 0 for(i in 1:num_attacks){ total <- total + sdmg(min, max, skill) } total } -------------------- The above is how damage is calculated. So the probability distribution looks like 2 rectangles next to each other, one with height .6 and the other with height .4. Sdmg is a helper function, because some attacks attack 2 or 4 times in a single turn. Though, I dont want the simulation (what I'm doing with the runif here). I'd like to calculate the probability distribution of total_damage, total_damage + total damage (the convolution, representing 2 hits), and so on till about 5-8 hits (then I can just use normal curve in the web-calculator I'm making), then have a polynomial function estimate the probability distributions based on min, max, and skill. (So I can make my online calculator estimate the probability of killing enemies) Erm, I know it is a lot to ask for, but how would I go about making this in R? And if R isn't the right tool for this, is there any other tool you'd reccomend? Cause doing convolutions by hand sucks. Thanks a lot for reading this long message :) And thanks in advance for your help. ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
I'm not using R server-side. I'd like to use R to get a (say) Taylor Series polynomial based on the 2nd convolution of the probability distribution .>From there, I can make a Javascript function that is a pretty preciseestimation of that convolution. PS: Thanks for the "S Poetry" reccomendation Patrick Burns, I'll take a look at it. On 5/12/06, Shawn Mikula <brainmaps at gmail.com> wrote:> Using R for this would be a bit bone-headed since it is not ideally suited > for web apps. Look at an active scripting language like PHP. I'm not sure > if Geocities provides for server-side active scripting languages, in which > case you will have to host your site elsewhere. > > > > ----- Original Message ----- > From: "percy tiglao" <prtiglao at gmail.com> > To: <r-help at stat.math.ethz.ch> > Sent: Friday, May 12, 2006 11:07 AM > Subject: [R] Newbie to R: would like to solve a problem > > > > Hello, I am very new to R (read the introduction a few hours ago), but > > experianced in several other languages (including Scheme, C, ObjC and > > C++, Java and Javascript) and I was wondering how to approach this > > problem: > > > > I am making a training calculator for a Video Game I play. Basically, > > I want to calculate the probability of killing an enemy within x hits, > > to help determine which is the best monster to train on in this game. > > > > The current implementation > > (http://geocities.com/dragontamer5788/maple2.html) uses the normal > > curve to estimate just about everything. Not necessarily a bad idea > > for when it takes 10 or 15 hits to destroy a single enemy, Central > > Limit Theorm kicks in and normal curve is probably a good idea, but my > > gut feeling is that it is not accurate for estimating kills in 1 or 2 > > shots (which happens in the majority of "training" monsters), or the > > probability of Knockback (which is always determined by a single > > shot). > > > > The (simplified) function to represent the damage would be: > > > > sdmg <- function(min, max, skill){ > > if(runif(1) > .4) (runif(1) * (max-min) + min) * skill > > else (runif(1) * (max-min) + min) * (skill+1) > > } > > > > total_damage <- function(min, max, skill, num_attacks){ > > total <- 0 > > for(i in 1:num_attacks){ > > total <- total + sdmg(min, max, skill) > > } > > total > > } > > > > -------------------- > > > > The above is how damage is calculated. So the probability distribution > > looks like 2 rectangles next to each other, one with height .6 and the > > other with height .4. Sdmg is a helper function, because some attacks > > attack 2 or 4 times in a single turn. > > > > Though, I dont want the simulation (what I'm doing with the runif > > here). I'd like to calculate the probability distribution of > > total_damage, total_damage + total damage (the convolution, > > representing 2 hits), and so on till about 5-8 hits (then I can just > > use normal curve in the web-calculator I'm making), then have a > > polynomial function estimate the probability distributions based on > > min, max, and skill. (So I can make my online calculator estimate the > > probability of killing enemies) > > > > Erm, I know it is a lot to ask for, but how would I go about making > > this in R? And if R isn't the right tool for this, is there any other > > tool you'd reccomend? Cause doing convolutions by hand sucks. > > > > Thanks a lot for reading this long message :) And thanks in advance > > for your help. > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide!
Actually, if you're going to go out transform the data, why not use a Fourier transform and take advantage of the fact that F[f*g] = F[f]F[g]? That's the easiest way in my mind to do multiple convolutions of fitted data. Taylor series are neither good at modeling Schwartz functions (a rapidly decreasing function), or particularly good at multiple convolutions, and you want to go up to eight, right? You could also use the FFT as an approximation, although direct calculation of the first few terms should just a straight forward integral (and I'll say this without actually trying to do this Fourier transforms by hand). Then you can just store the FT function in your PHP code and calculate the Nth convolution as F[f]^N. Hopefully my memory isn't too far off here. -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of percy tiglao Sent: Friday, May 12, 2006 4:26 PM To: r-help at stat.math.ethz.ch Subject: Re: [R] Newbie to R: would like to solve a problem I'm not using R server-side. I'd like to use R to get a (say) Taylor Series polynomial based on the 2nd convolution of the probability distribution .>From there, I can make a Javascript function that is a pretty preciseestimation of that convolution. PS: Thanks for the "S Poetry" reccomendation Patrick Burns, I'll take a look at it. On 5/12/06, Shawn Mikula <brainmaps at gmail.com> wrote:> Using R for this would be a bit bone-headed since it is not ideallysuited> for web apps. Look at an active scripting language like PHP. I'm notsure> if Geocities provides for server-side active scripting languages, inwhich> case you will have to host your site elsewhere. > > > > ----- Original Message ----- > From: "percy tiglao" <prtiglao at gmail.com> > To: <r-help at stat.math.ethz.ch> > Sent: Friday, May 12, 2006 11:07 AM > Subject: [R] Newbie to R: would like to solve a problem > > > > Hello, I am very new to R (read the introduction a few hours ago),but> > experianced in several other languages (including Scheme, C, ObjCand> > C++, Java and Javascript) and I was wondering how to approach this > > problem: > > > > I am making a training calculator for a Video Game I play.Basically,> > I want to calculate the probability of killing an enemy within xhits,> > to help determine which is the best monster to train on in thisgame.> > > > The current implementation > > (http://geocities.com/dragontamer5788/maple2.html) uses the normal > > curve to estimate just about everything. Not necessarily a bad idea > > for when it takes 10 or 15 hits to destroy a single enemy, Central > > Limit Theorm kicks in and normal curve is probably a good idea, butmy> > gut feeling is that it is not accurate for estimating kills in 1 or2> > shots (which happens in the majority of "training" monsters), or the > > probability of Knockback (which is always determined by a single > > shot). > > > > The (simplified) function to represent the damage would be: > > > > sdmg <- function(min, max, skill){ > > if(runif(1) > .4) (runif(1) * (max-min) + min) * skill > > else (runif(1) * (max-min) + min) * (skill+1) > > } > > > > total_damage <- function(min, max, skill, num_attacks){ > > total <- 0 > > for(i in 1:num_attacks){ > > total <- total + sdmg(min, max, skill) > > } > > total > > } > > > > -------------------- > > > > The above is how damage is calculated. So the probabilitydistribution> > looks like 2 rectangles next to each other, one with height .6 andthe> > other with height .4. Sdmg is a helper function, because someattacks> > attack 2 or 4 times in a single turn. > > > > Though, I dont want the simulation (what I'm doing with the runif > > here). I'd like to calculate the probability distribution of > > total_damage, total_damage + total damage (the convolution, > > representing 2 hits), and so on till about 5-8 hits (then I can just > > use normal curve in the web-calculator I'm making), then have a > > polynomial function estimate the probability distributions based on > > min, max, and skill. (So I can make my online calculator estimatethe> > probability of killing enemies) > > > > Erm, I know it is a lot to ask for, but how would I go about making > > this in R? And if R isn't the right tool for this, is there anyother> > tool you'd reccomend? Cause doing convolutions by hand sucks. > > > > Thanks a lot for reading this long message :) And thanks in advance > > for your help. > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide!______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
I forgot to forward the message to the group :-/ And I have one more question, but it pertains somewhat to this last post (which is why I attached the last message) What is the name of the distribution that takes skewness and kurtosis as a factor? I figure, if I can find a distribution that better models the damage done in this game, it would be better off period. A normal curve estimates the data pretty darn well already, maybe adding in skewness and kurtosis will make the model close to exact. I tried searching for skewness and kertosis on google, and found some information on a "Weibull" distribution, but it only seems to model skewness and not as much kurtosis. Is there a specific distribution that can model data based on both skewness and kurtosis? ---------- Forwarded message ---------- From: percy tiglao <prtiglao at gmail.com> Date: May 12, 2006 5:16 PM Subject: Re: [R] Newbie to R: would like to solve a problem To: "McGehee, Robert" <Robert.McGehee at geodecapital.com> On 5/12/06, McGehee, Robert <Robert.McGehee at geodecapital.com> wrote:> Not sure I completely followed (especially what a Knockback is), but > I'll give this a try.Lol :-) Video game stuff. Unimportant. You got the main gist of what I want though.> If you want to precisely define the convolution of multiple attacks, you > would want to use a language that utilizes symbolic integration, which R > currently does not support (e.g. see Mathematica). That said, it sounds > like you don't actually want a precise answer, but you want to have a > "polynomial function estimate" the true distribution. If this is the > case, then it seems like doing a simulation may be precisely what you > want. That is, model a polynomial approximation of the total damage from > multiple attacks as a function of the number of attacks.Ah. So to get the "precise" formula, I need Mathematica? Gosh... I'll just do it by hand then, Lol. I'm so cheap :-/ Well, a simulation was the 2nd thing I was going to ask for if there was no way for this to work out.> For instance the function below will plot what the distribution of N > multiple attacks would look like: > > distributionFun <- function(N, min, max, skill, num_attacks) { > hist(replicate(1000, sum(replicate(N, total_damage(min, max, > skill, num_attacks))))) > } > > and > sum(replicate(N, total_damage(min, max, skill, num_attacks))) > will give you a sample from this distribution. > > Thus, distributionFun(1, 10, 20, 1, 1) will show the piecewise constant > distribution that you would expect from a single attack, and setting N > to a large number produces a Gaussian distribution (CLT). [NB: Here the > N and num_attacks arguments may be redundant, but it's not clear to me > how these multiple attacks work (i.e. are there multiple attacks > multiple times, etc.)]If I'm reading this code correctly, You got 100% what I was looking for, in terms of simulation anyway :)> Once you have the simulated distribution, you can either create a > polynomial model as a function of N and use these values as inputs into > your web simulation, or you can use the code above to grab a single > sample from the distribution. Also, I imagine you would get better > results modeling the moments of the distribution (mean, variance, skew, > kurtosis, etc.) rather than using a polynomial function, but up to you.Wow... there is so much more math I need to learn before I attempt this problem again... Kurtosis? Thank god for Wikipedia + googleing tutorials. And since when was skewness represented as a number? o.O> Also, if you're only doing this for the first 8 attacks, then > integrating a piecewise constant function by hand (if Mathematica is > unavailable) is not too difficult, and running simulations may be going > overboard.Not with high-school level Math knowledge it is. Lol. *note to self, start buying math books*> Lastly, just as an FYI, your functions can be rewritten a bit more > concisely as such: > > sdmg <- function(min, max, skill) { > runif(1, min, max) * ifelse(runif(1) > 0.4, skill, skill + 1) > } > > total_damage <- function(min, max, skill, num_attacks) { > sum(replicate(num_attacks, sdmg(min, max, skill)) > } > > Good luck, > RobertThanks so much for all your help. I'll be looking into other languages (like Yacas) to see if they can do this precisely (Idealy, I need it as a function of min, max, and skill. That way, It will be easy enough for Javascript to calculate.)