Pesky homemade function code

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Pesky homemade function code

trekvana
Hi all-

this seems to be simple to figure out but since im new to writing functions I dont know what is happening. Here is my code along with the error I am receiving:

semivario=function(data,ids,times,resids){
id=unique(data$ids)
index=combinations(length(data$times[data$ids==id[1]]),2)
time=gamma=numeric(dim(index)[1])
for (j in 1:dim(index)[1]){
        time[j]=abs(data$times[data$ids==id[1]][index[j,1]]-data$times[data$ids==id[1]][index[j,2]])
        gamma[j]=.5*(data$resids[data$ids==id[1]][index[j,1]]-data$resids[data$ids==id[1]][index[j,2]])^2
}

for (i in id[-1]){
        index=combinations(length(data$times[data$ids==i]),2)
        time1=gamma1=numeric(dim(index)[1])
        for (j in 1:dim(index)[1]){
                time1[j]=abs(data$times[data$ids==i][index[j,1]]-data$times[data$ids==i][index[j,2]])
                gamma1[j]=.5*(data$resids[data$ids==i][index[j,1]]-data$resids[data$ids==i][index[j,2]])^2
        }
time=c(time,time1)
gamma=c(gamma,gamma1)
}
value=list(time=time,gamma=gamma)
return(value)
}

and I would run it as semivario(data=hope,ids=id,times=sctime,resids=scResid)

What I want to do is everywhere we see the words data,ids,times,resids to be replaced with, for example, hope,id,sctime,scResid. Think of macros in SAS where we use the & before every macro input variable name. Im not sure if this is how R handles function inputs.

Here is my error:
Error in combinations(length(data$times[data$ids == id[1]]), 2) :
  bad value of n


Thanks
Reply | Threaded
Open this post in threaded view
|

Re: Pesky homemade function code

Thierry Onkelinx
The calculation of semivraiogram is allready implemented is many R
packages. So why do it yourself?

library("sos")
findFn("variogram")

Gives you 190 hits!

------------------------------------------------------------------------
----
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek
team Biometrie & Kwaliteitszorg
Gaverstraat 4
9500 Geraardsbergen
Belgium

Research Institute for Nature and Forest
team Biometrics & Quality Assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium

tel. + 32 54/436 185
[hidden email]
www.inbo.be

To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to
say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of
data.
~ John Tukey
 

> -----Oorspronkelijk bericht-----
> Van: [hidden email]
> [mailto:[hidden email]] Namens trekvana
> Verzonden: donderdag 16 september 2010 8:35
> Aan: [hidden email]
> Onderwerp: [R] Pesky homemade function code
>
>
> Hi all-
>
> this seems to be simple to figure out but since im new to
> writing functions I dont know what is happening. Here is my
> code along with the error I am
> receiving:
>
> semivario=function(data,ids,times,resids){
> id=unique(data$ids)
> index=combinations(length(data$times[data$ids==id[1]]),2)
> time=gamma=numeric(dim(index)[1])
> for (j in 1:dim(index)[1]){
>
> time[j]=abs(data$times[data$ids==id[1]][index[j,1]]-data$times
> [data$ids==id[1]][index[j,2]])
>
> gamma[j]=.5*(data$resids[data$ids==id[1]][index[j,1]]-data$res
> ids[data$ids==id[1]][index[j,2]])^2
> }
>
> for (i in id[-1]){
> index=combinations(length(data$times[data$ids==i]),2)
> time1=gamma1=numeric(dim(index)[1])
> for (j in 1:dim(index)[1]){
>
> time1[j]=abs(data$times[data$ids==i][index[j,1]]-data$times[da
> ta$ids==i][index[j,2]])
>
> gamma1[j]=.5*(data$resids[data$ids==i][index[j,1]]-data$resids
> [data$ids==i][index[j,2]])^2
> }
> time=c(time,time1)
> gamma=c(gamma,gamma1)
> }
> value=list(time=time,gamma=gamma)
> return(value)
> }
>
> and I would run it as
> semivario(data=hope,ids=id,times=sctime,resids=scResid)
>
> What I want to do is everywhere we see the words
> data,ids,times,resids to be replaced with, for example,
> hope,id,sctime,scResid. Think of macros in SAS where we use
> the & before every macro input variable name. Im not sure if
> this is how R handles function inputs.
>
> Here is my error:
> Error in combinations(length(data$times[data$ids == id[1]]), 2) :
>   bad value of n
>
>
> Thanks
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Pesky-homemade-function-code-tp2
> 541597p2541597.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.
>

Druk dit bericht a.u.b. niet onnodig af.
Please do not print this message unnecessarily.

Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer
en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
door een geldig ondertekend document. The views expressed in  this message
and any annex are purely those of the writer and may not be regarded as stating
an official position of INBO, as long as the message is not confirmed by a duly
signed document.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Pesky homemade function code

trekvana
@Thierry-

I am writing my own semivariogram code since I could not find a pre-made function that did what I need it do. The code in the function correctly works but I not sure how to get the function itself to work. In other words if I fill in the commands (hope,id,sctime,scResid) by hand then everything is ok -- but I dont want to do that.

Plus this will help be better understand how to program functions in R.
Reply | Threaded
Open this post in threaded view
|

Re: Pesky homemade function code

Sebastian Gibb
In reply to this post by trekvana
Hello,

why you don't use the last 3 arguments?
>semivario=function(data,ids,times,resids){
> id=unique(data$ids)
...
It seems "data" be a list. In that case the last 3 arguments are useless.
Could you please send us a runable code example. That means some example data
and your all neede functions (e.g. combinations, because there is the error
you looking for.)
> Error in combinations(length(data$times[data$ids == id[1]]), 2) :
>   bad value of n

Kind regards,

Sebastian

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Pesky homemade function code

Rafael Björk
In reply to this post by trekvana
Checking on the function 'combinations' in gplot i find this:

if (mode(n) != "numeric" || length(n) != 1 || n < 1 || (n%%1) !=
        0)
        stop("bad value of n")

So essentially, the argument 'length(data$times[data$ids == id[1]])' in the
line
'combinations(length(data$times[data$ids == id[1]]), 2)' does not meet one
of those four conditions. That's as much as i can tell you without some kind
of reproducible code.



2010/9/16 trekvana <[hidden email]>

>
> Hi all-
>
> this seems to be simple to figure out but since im new to writing functions
> I dont know what is happening. Here is my code along with the error I am
> receiving:
>
> semivario=function(data,ids,times,resids){
> id=unique(data$ids)
> index=combinations(length(data$times[data$ids==id[1]]),2)
> time=gamma=numeric(dim(index)[1])
> for (j in 1:dim(index)[1]){
>
>
> time[j]=abs(data$times[data$ids==id[1]][index[j,1]]-data$times[data$ids==id[1]][index[j,2]])
>
>
> gamma[j]=.5*(data$resids[data$ids==id[1]][index[j,1]]-data$resids[data$ids==id[1]][index[j,2]])^2
> }
>
> for (i in id[-1]){
>        index=combinations(length(data$times[data$ids==i]),2)
>        time1=gamma1=numeric(dim(index)[1])
>        for (j in 1:dim(index)[1]){
>
>
> time1[j]=abs(data$times[data$ids==i][index[j,1]]-data$times[data$ids==i][index[j,2]])
>
>
> gamma1[j]=.5*(data$resids[data$ids==i][index[j,1]]-data$resids[data$ids==i][index[j,2]])^2
>        }
> time=c(time,time1)
> gamma=c(gamma,gamma1)
> }
> value=list(time=time,gamma=gamma)
> return(value)
> }
>
> and I would run it as
> semivario(data=hope,ids=id,times=sctime,resids=scResid)
>
> What I want to do is everywhere we see the words data,ids,times,resids to
> be
> replaced with, for example, hope,id,sctime,scResid. Think of macros in SAS
> where we use the & before every macro input variable name. Im not sure if
> this is how R handles function inputs.
>
> Here is my error:
> Error in combinations(length(data$times[data$ids == id[1]]), 2) :
>  bad value of n
>
>
> Thanks
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Pesky-homemade-function-code-tp2541597p2541597.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.