Re: [R] posting for R-help on the predict function and categorical variables in R

2014-03-08 Thread Greg Snow
How predict works depends on the method written for that type of
object.  The zeroinfl function is not in any of the standard packages,
so it must be in another package, but you did not tell us which.
Since it is from a package other than the main ones, it may work
similarly to the regular predict functions, or it may work completely
different.  Have you tried reading the help pages for zeroinfl and the
predict method for it?  Have you worked through any vignettes for that
package?

On Fri, Mar 7, 2014 at 10:16 AM, Roman, Sally (MRC)
sally.ro...@mrc.virginia.gov wrote:
 Hi - I would like to post to the R-help mailing list.  Here is my post:

 -This is more of a general question about how the predict function treats 
 categorical variables and how to interpret the output from predict.

 I have a zeroinfl model to predict the number of animals encountered:

   b9-zeroinfl(Count ~ as.factor (Area)  + as.factor(Season)|1, 
 dist=negbin,data = total)

 where Count is the number of animals and the explanatory variables are Area 
 and Season, both are coded as factors in the model.  Area has three levels 
 and Season has 4 levels.  Coding the two as factors allows for R to create 
 dummy variables for each variable for use in the model.

 When I use the predict function to predict the number of animals for a larger 
 data set, I want to make sure I'm understanding what is happening.

 My newdata for predict is:
 newdata-  as.data.frame(Season, Area)
 Both variables are coded as factors and the dataframe is in a long format.  
 There are records for each combination of Season and Area that correspond to 
 trips taken over the course of 8 years.  There are 113,804 rows of data in 
 the newdata data,frame.

 str(newdata)
 'data.frame':   113804 obs. of  2 variables:
 $ Season: Factor w/ 4 levels 1,2,3,4: 1 1 1 1 1 1 1 1 1 1 ...
 $ Area  : Factor w/ 3 levels 625,631,Bay: 3 3 3 3 3 3 3 3 3 3 ...

 Example:
Season Area
 1   1  Bay
 2   1  625
 3   1  631
 4   2  Bay
 5   2  625
 6   2  631
 7   3  Bay
 8   3  625
 9   3  631
 10  4  Bay
 11  4  625
 12  4  631

 1.  Do I need to create dummy variables for all levels for the two variables 
 for input into predict, or does predict function act like zeroinfl where if 
 the variables are coded as factors, this is done automatically by R.

 2.  Predict returns values of 0.0461 - 0.6015.  If I am trying to predict the 
 number of animals how do I interpret this?  Since no predicted values are 
 greater than 1 and I need whole numbers, I rounded the predicted data so that 
 any value less than 0.5 was equal to 0 and any value greater than 0.5 was 
 equal to 1.  Does this seem correct?

 Thanks for any help.

 Sally Roman
 Fisheries Management Specialist
 Virginia Marine Resources Commission
 2600 Washington Avenue, 3rd Floor
 Newport News, VA  23607
 Phone: 757-247-2243


 [[alternative HTML version deleted]]

 __
 R-help@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.
538...@gmail.com

__
R-help@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.


[R] posting for R-help on the predict function and categorical variables in R

2014-03-07 Thread Roman, Sally (MRC)
Hi - I would like to post to the R-help mailing list.  Here is my post:

-This is more of a general question about how the predict function treats 
categorical variables and how to interpret the output from predict.

I have a zeroinfl model to predict the number of animals encountered:

  b9-zeroinfl(Count ~ as.factor (Area)  + as.factor(Season)|1, 
dist=negbin,data = total)

where Count is the number of animals and the explanatory variables are Area and 
Season, both are coded as factors in the model.  Area has three levels and 
Season has 4 levels.  Coding the two as factors allows for R to create dummy 
variables for each variable for use in the model.

When I use the predict function to predict the number of animals for a larger 
data set, I want to make sure I'm understanding what is happening.

My newdata for predict is:
newdata-  as.data.frame(Season, Area)
Both variables are coded as factors and the dataframe is in a long format.  
There are records for each combination of Season and Area that correspond to 
trips taken over the course of 8 years.  There are 113,804 rows of data in the 
newdata data,frame.

str(newdata)
'data.frame':   113804 obs. of  2 variables:
$ Season: Factor w/ 4 levels 1,2,3,4: 1 1 1 1 1 1 1 1 1 1 ...
$ Area  : Factor w/ 3 levels 625,631,Bay: 3 3 3 3 3 3 3 3 3 3 ...

Example:
   Season Area
1   1  Bay
2   1  625
3   1  631
4   2  Bay
5   2  625
6   2  631
7   3  Bay
8   3  625
9   3  631
10  4  Bay
11  4  625
12  4  631

1.  Do I need to create dummy variables for all levels for the two variables 
for input into predict, or does predict function act like zeroinfl where if the 
variables are coded as factors, this is done automatically by R.

2.  Predict returns values of 0.0461 - 0.6015.  If I am trying to predict the 
number of animals how do I interpret this?  Since no predicted values are 
greater than 1 and I need whole numbers, I rounded the predicted data so that 
any value less than 0.5 was equal to 0 and any value greater than 0.5 was equal 
to 1.  Does this seem correct?

Thanks for any help.

Sally Roman
Fisheries Management Specialist
Virginia Marine Resources Commission
2600 Washington Avenue, 3rd Floor
Newport News, VA  23607
Phone: 757-247-2243


[[alternative HTML version deleted]]

__
R-help@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.


[R] - help with the predict function

2012-06-05 Thread Joachim Audenaert
Hi all,

I would like to predict some values for an nls regression function 
(functional response model Rogers type II). This is an asymptotic function 
of which I would like to predict the asymptotic value
I estimated the paramters with nls, but can't seem to get predictions for 
values of m choice..
This is my script:

RogersII_N - 
nls(FR~N0-lambertW(attackR3_N*Th3_N*N0*exp(-attackR3_N*(24-Th3_N*N0)))/(attackR3_N*Th3_N),start=list(attackR3_N=0.04,Th3_N=1.46),control=list(maxiter=1))
lista - c(1,2,100,1000)
predict(RogersII_N,newdata=lista)

I created a list (lista) with some values of which I would like the 
predict function to give me function values

What am I doing wrong?

Kind regards,
Met vriendelijke groeten,
Joachim

Don't waste paper! Think about the environment before printing this e-mail

__

Joachim Audenaert
Adviesdienst Gewasbescherming
Proefcentrum voor Sierteelt
Schaessestraat 18
B-9070 Destelbergen
Belgium
Tel. +32 9 353 94 71
Fax +32 9 353 94 95
E-mail: joachim.audena...@pcsierteelt.be
www.pcsierteelt.be
__
[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] - help with the predict function

2012-06-05 Thread Ben Bolker
Joachim Audenaert Joachim.Audenaert at pcsierteelt.be writes:

 
 Hi all,
 
 I would like to predict some values for an nls regression function 
 (functional response model Rogers type II). This is an asymptotic function 
 of which I would like to predict the asymptotic value
 I estimated the paramters with nls, but can't seem to get predictions for 
 values of m choice..
 This is my script:
 
 RogersII_N - 
 nls(FR~N0-lambertW(attackR3_N*Th3_N*N0*
 exp(-attackR3_N*(24-Th3_N*N0)))/(attackR3_N*Th3_N),
 start=list(attackR3_N=0.04,Th3_N=1.46),control=list(maxiter=1))
 lista - c(1,2,100,1000)
 predict(RogersII_N,newdata=lista)
 
 I created a list (lista) with some values of which I would like the 
 predict function to give me function values
 
 What am I doing wrong?
 

  If you want to use the predict() function, it is best
(necessary?) to use the form of nls() where you pass data
explicitly within a data frame, as the data= argument, instead
of using variables in the global workspace.  Also: for this
model, the asymptotic value is (time/(handling time)) = 24/Th3_N ...
so you don't really need predict() (although it is still useful)

library(emdbook)
rogers.pred - function(N0,a,h,T) {
N0 - lambertW(a*h*N0*exp(-a*(T-h*N0)))/(a*h)
}

params0 - list(attackR3_N=0.05,Th3_N=1.5)
params1 - list(attackR3_N=0.04,Th3_N=1.46)

dat - expand.grid(N0=1:20,rep=1:10)
dat$FR0 - with(c(dat,params0),
rogers.pred(N0,a=attackR3_N,h=Th3_N,T=24))
par(las=1,bty=l)
with(dat,plot(N0,FR0,ylim=c(0,12)))
set.seed(101)
dat$FR - rnorm(nrow(dat),dat$FR0,sd=1)
with(dat,points(N0,FR,col=rgb(1,0,0,alpha=0.5)))
RogersII_N - nls(FR~rogers.pred(N0,attackR3_N,Th3_N,T=24),
  start=params1,data=dat,control=list(maxiter=1))
lista - c(1,2,100,1000,1)
predict(RogersII_N,newdata=data.frame(N0=lista))
N0vec - 1:20
lines(N0vec,predict(RogersII_N,newdata=data.frame(N0=N0vec)),
  col=4)
with(as.list(coef(RogersII_N)),24/Th3_N)

__
R-help@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.