0

I have a function

    predictshrine<-0*rain-399.8993+5*crops+50.4296*log(citysize)+
    4.5071*wonders*chief+.02301*children*deaths+1.806*children+
   .10799*deaths-2.0755*wonders-.0878*children^2+.001062*children^3-
   .000004288*children^4-.009*deaths^2+.0000530238*deaths^3+
   7.974*sqrt(children)+.026937*wonders^2-.0001305*wonders^3

I also have a sequence

    children<-seq(0,100,length=500)

And a for loop

    for(deaths in c(0,5,10,50,100,200))

Now what i want to do is be able to plot predictshrine vs children when deaths equals certain amounts and display those graphs all at once with the par(mfrow) function

I have

    plot(predictshrine, children)

I want to be able to do this function when death=0, when death=10, when death=50 etc.

That way I could have 6 different graphs showing the change in the regression

Ideally i could do something like

    plot(predictshrine, children, when deaths = 10 & 20 & 50)

but that won't work as code.

I want to incorporate my 4 loop into the equation.

Just to make it clear, deaths and children are two variables in my multivariable equation

Thanks in advance

-Max

Maxwell Chandler
  • 626
  • 8
  • 18
  • But what about all the over variables in the regression? eg "wonder" and "crops". Please make a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data to make it easier to help you. – MrFlick Apr 23 '15 at 22:01

1 Answers1

1

You can use mapply to iterate over several arguments. Keep in mind that you need to define all the other variables if you want to do this. Also, this isn't the most memory-efficient way, but it should work for smaller sizes of combinations.

predictshrine<- function(rain,citysize,wonders,chief,children,deaths,crops) {
    0*rain-399.8993+5*crops+50.4296*log(citysize)+
    4.5071*wonders*chief+.02301*children*deaths+1.806*children+
    .10799*deaths-2.0755*wonders-.0878*children^2+.001062*children^3-
    .000004288*children^4-.009*deaths^2+.0000530238*deaths^3+
    7.974*sqrt(children)+.026937*wonders^2-.0001305*wonders^3
}

deathlist = c(0,5,10,50,100,200)
#note that the children value is recycled
res = mapply(predictshrine,children = 1:100,deaths = 
 rep(deathlist,each = 100),
 rain = 0.1, wonders = 1, chief = 1, crops = 0.5,citysize = 1000)

Then you can plot this six times.

#allow six plots on the panel if you want
par(mfrow = c(3,2))
#loop through different plots
for (i in 1:6)
    plot(1:100,res[1:100 + (i-1)*100])

This is what it looks like

enter image description here

Max Candocia
  • 4,294
  • 35
  • 58