[R] function return
David Winsemius
dwinsemius at comcast.net
Thu Jul 22 23:34:55 CEST 2010
On Jul 22, 2010, at 5:27 PM, Daniel Hocking wrote:
> I am sorry if this question is vague or uninformed. I am just
> learning R and struggling. I am using the book Hierarchical Modeling
> and Inference in Ecology and they provide examples of R code. I have
> the following code from the book but when I run it I don't get any
> output. I cannot get the values of 'out' to show up. Basically, I
> just want to see my estimates for b0, b1, b2, and b3. Any time that I
> have used or see function( ) there have been arguments and I just call
> for the value of the function. In this case there doesn't seem to be
> a value for the function. Any help would be appreciated.
You have defined a function that has no arguments. You should be
typing this at the console:
panel3pt1.fn()
I get an error because there is no such file on my machine and you
have not provided a link to an accessible copy.
--
David.
>
> Thanks,
> Dan
>
>
> `panel3pt1.fn` <-
> function(){
>
> source("utilfns.Rd")
>
> data<- read.table("wtmatrix.txt",header=T,na.strings=T)
> elev<-data[,"elev"]
> forest<-data[,"forest"]
> elev<-scale(elev,center=TRUE)
> forest<-scale(forest,center=TRUE)
> pamat<-data[,c("y.1","y.2","y.3")]
> z<-pamat[,1]
> M<-length(z)
>
>
> lik<-function(parms){
> b0<-parms[1]
> b1<-parms[2]
> b2<-parms[3]
> b3<-parms[4]
> ones<-rep(1,M)
> ### Compute binomial success probabilities
> probs<-expit(b0*ones+b1*elev+b2*(elev^2)+b3*forest)
> lik<-rep(0,length(z))
> ### evaluate log of binomial pmf
> tmp<-log(dbinom(z,1,probs))
> ### substitute 0 for missing values
> lik[!is.na(z)] <- tmp[!is.na(z)]
> lik<- -1*sum(lik)
> return(lik)
> }
>
> out <- nlm(lik,c(0,0,0,0),hessian=TRUE)
>
> return(out)
>
>
> }
>
> ------------------------------------------------------------------------------------
> Daniel J. Hocking
> 122 James Hall
> Department of Natural Resources & the Environment
> University of New Hampshire
> Durham, NH 03824
>
> dhocking at unh.edu
> http://sites.google.com/site/danieljhocking/
> www.hockingphotography.smugmug.com
>
> "Without data, all you are is just another person with an opinion."
> -------------------------------------------------------------------------------------
>
>
>
>
>
>
>
> [[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.
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list