[R] function return
Daniel Hocking
dhocking at unh.edu
Fri Jul 23 00:52:27 CEST 2010
Thank you so much! I think I had tried those two pieces separately
and obviously had no success. I also typed panel3pt1.fn in the
console without the () following it.
On Jul 22, 2010, at 5:57 PM, David Winsemius wrote:
>
> On Jul 22, 2010, at 5:34 PM, David Winsemius wrote:
>
>>
>> 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.
>
> If you go to their website
> http://www.mbr-pwrc.usgs.gov/pubanalysis/roylebook/chapters.htm
> ... and:
> a) download the .Rd file and the similarly named .csv file
> referenced by that function to your working directory
> b) change the extension of the .csv file to match the function
> reference
> c) add , sep="," to the read.table call
> d) then do what I said above ....
>
> > panel3pt1.fn() # Voilà !!
>
> $minimum
> [1] 100.887223663
>
> $estimate
> [1] -0.542213281782 1.847343506809 -1.060864239423 0.646910762031
>
> $gradient
> [1] 2.34905428442e-05 3.14549972521e-05 6.51961177771e-05
> 4.88853402203e-05
>
> $hessian
> [,1] [,2] [,3] [,4]
> [1,] 33.38181500113 15.26441629493 27.8670270859 6.61193837459
> [2,] 15.26441629493 27.86699589010 30.5057286674 -9.66977848175
> [3,] 27.86702708591 30.50572866744 52.5892374981 -12.50448633528
> [4,] 6.61193837459 -9.66977848175 -12.5044863353 34.09027300450
>
> $code
> [1] 1
>
> $iterations
> [1] 16
>
> --
>> 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
>>
>> ______________________________________________
>> 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