[R] Plotting every probability curve
Greg Snow
538280 at gmail.com
Wed Sep 12 18:01:35 CEST 2012
Thanks for including an example that could be copied and pasted.
However TkPredict and Predict.Plot both need at least one numeric (not
factor) predictor. So I added another column called x that was just
1:5 to the sample data frame and included it in the model.
Here are a couple of approaches using for loops:
tmp.df <- expand.grid( home=c('own','rent'), income=c('20','50'),
gender=c('F','M') )
par(ask=TRUE)
for( i in seq_len(nrow(tmp.df)) ) {
Predict.Plot(m1, 'x', home=tmp.df$home[i], income=tmp.df$income[i],
gender=tmp.df$gender[i])
title( sprintf( "Home %s, Income %s, Gender %s", tmp.df$home[i],
tmp.df$income[i], tmp.df$gender[i] ))
}
par(ask=FALSE)
Predict.Plot(m1, 'x', home='own', income='20', gender='F', x=-1:5)
for(i in 2:nrow(tmp.df) ) {
Predict.Plot(m1, 'x', home=tmp.df$home[i], income=tmp.df$income[i],
gender=tmp.df$gender[i], add=TRUE, plot.args=list(col=i))
}
legend( 'topleft', with(tmp.df, paste(home,income,gender)),
lty=1, col=1:8)
For the first example, if you want to save the individual plots you
should either open a pdf device, then run the loop. Or, include a
command in the loop to save the plot.
Hope this helps,
On Tue, Sep 11, 2012 at 5:16 PM, Abraham Mathew <abmathewks at gmail.com> wrote:
> I don't have a logistic regression model and am trying to generate
> probability curves for all possible combinations of
> the variables. My logit model has 5+ variables, and I want to draw curves
> for every scenario.
>
> See code below. When home_owner is 0 and 1, I want curves. The same goes
> for all other variables categories, so that
> I have permutations for all possible combinations.
>
> I've found that I can use the TeachingDemos package (see below) to
> construct a gui which makes this easier, but I want to form
> curve separately so that I can save them
>
> library(TeachingDemos)
> TkPredict(mod1)
>
> Predict.Plot(mod1, pred.var = "our_bid", our_bid = c(0,300),
> age_of_oldest_driver2 = "18 to 21",
> credit_type2 = "POOR", coverage_type2 = "BASIC", home_owner2 =
> "0",
> state2 = "other", currently_insured2 = "0",
> vehicle_driver_score = "0",
> plot.args = "list()", type = "response")
>
>
> Quick reproduceable example =
> df = data.frame(sell=c("0","1","0","0","1"),
> home=c("own","rent","rent","rent","own"),
> income=c(50,20,20,50,50), gender=c("M","M","F","F","F"))
> df$sell = as.factor(df$sell)
> df$home = as.factor(df$home)
> df$income = as.factor(df$income)
> df$gender = as.factor(df$gender)
> str(df)
> m1 = glm(factor(sell) ~ home + income + gender,
> data=df, family=binomial(link="logit"))
>
> summary(m1)
>
> library(TeachingDemos)
> TkPredict(m1)
>
> Thanks
>
> --
> *Abraham Mathew
> Statistical Analyst
> www.amathew.com
> 720-648-0108
> @abmathewks*
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
--
Gregory (Greg) L. Snow Ph.D.
538280 at gmail.com
More information about the R-help
mailing list