[R] subset and or operator

2009-03-23 Thread lauramorg...@bluewin.ch
Hello,
I'm trying to subset a dataframe where I have many observation taken in 
different years.
I would like to subset the dataframe (in this example called table) to get a 
new dataframe containing only the 
observation of year 1995, 1998 and 2000.
I've tried to use subset and the or operator | like this:
subset(table, year==1995|1998|2000)--table2
but the dataframe didn't subset and I get an object called table2 that is 
identical to table.
I guess I'm doing something wrong!!

Thank in advance for any suggestions!
Laura

__
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] mean over previous cells

2009-02-20 Thread lauramorg...@bluewin.ch
If, suppose, the name of your dataframe is dataframe , you could try 
something like:
mean60days-c()
for(i in 1:length(dataframe$SST))
+ {
+ mean60days[[i]]-mean(dataframe$SST[i-10:i])
+ }
I'm not really sure it will work... my skills aren't that great...
Anyway, it worked on a sample I tried it on, but you have to be careful and 
discard the first 60 values!!
Hope it works,
ciao
Laura




Dear RUsers,
I guess this is an easy question for someone a little familiar with
programming...(which I am not)...

I've got 2 colummns, one shows just dates(SST_date, Class 'Date' num), the
other one shows the SeaSurfaceTemperature (SST, num) at that certain date.

SST_dateSST 
2008-01-01 22.2 
2008-01-02 21.8
2008-01-03 22.8
2008-01-04 22.9
2008-01-05 23.1
2008-01-06 23.2  
...
...

now, I would like to add a column that shows the mean SST over the last
(e.g.) 60 days (for that specific date).

My biological question is, whether the birthweight of an animal at a
specific birthdate changes due to the SST over the last 60 days before
birth. (SST is an indicator for food abundance - if food is scarce, mothers
can't feed much and hence, pups are born lighter!?)
Up to now, I can only show, whether the SeaSurfaceTemperatures on the
birthdate can have an influence on birthweight, which is not what I want to
do...

Can anybody tell me, how to start, where to look it up or help me even more? 
thanks in advance.
Birte

-- 
View this message in context: 
http://www.nabble.com/mean-over-previous-cells-tp22116807p22116807.html
Sent from the R help mailing list archive at Nabble.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-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] mean over previous cells

2009-02-20 Thread lauramorg...@bluewin.ch
Sorry, I meant:

If, suppose, the name of your dataframe is dataframe , you could try 
something like:
mean60days-c()
for(i in 1:length(dataframe$SST))
+ {
+ mean60days[[i]]-mean(dataframe$SST[i-60:i])
+ }
I'm not really sure it will work... my skills aren't that great...
Anyway, it worked on a sample I tried it on, but you have to be careful and 
discard the first 60 values!!
Hope it works,
ciao
Laura




Dear RUsers,
I guess this is an easy question for someone a little familiar with
programming...(which I am not)...

I've got 2 colummns, one shows just dates(SST_date, Class 'Date' num), the
other one shows the SeaSurfaceTemperature (SST, num) at that certain date.

SST_dateSST 
2008-01-01 22.2 
2008-01-02 21.8
2008-01-03 22.8
2008-01-04 22.9
2008-01-05 23.1
2008-01-06 23.2  
...
...

now, I would like to add a column that shows the mean SST over the last
(e.g.) 60 days (for that specific date).

My biological question is, whether the birthweight of an animal at a
specific birthdate changes due to the SST over the last 60 days before
birth. (SST is an indicator for food abundance - if food is scarce, mothers
can't feed much and hence, pups are born lighter!?)
Up to now, I can only show, whether the SeaSurfaceTemperatures on the
birthdate can have an influence on birthweight, which is not what I want to
do...

Can anybody tell me, how to start, where to look it up or help me even more? 
thanks in advance.
Birte

-- 
View this message in context: 
http://www.nabble.com/mean-over-previous-cells-tp22116807p22116807.html
Sent from the R help mailing list archive at Nabble.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-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] write.table

2009-02-20 Thread lauramorg...@bluewin.ch
Sorry, I'm using R 2.8.1 on Microsoft Windows XP professional 2002 Service Pack 
2.
The error I get is 
 Error in write.table(x, file, nrow(x), p, rnames, sep, eol, 
 na, dec, as.integer(quote),  : 
'list' type not implemented in 'EncodeElement'
The problem is that I can' t manage to save the dataframe...
With other dataframe I usually have no problems!!!

 
 Hello,
 I tried to turn lists into vectors and then bind them 
 together in order to create a dataframe but if, after this, I 
 try to use the function write.table I get the following error message:
 Error in write.table(x, file, nrow(x), p, rnames, sep, eol, 
 na, dec, as.integer(quote),  : 
'list' type not implemented in 'EncodeElement'
  
 Here is what I've done:
 as.vector(c(unique(portate$Anno)))-anno
 as.vector(loadListPO4)-loadPO4
 as.vector(loadListPTG)-loadPTG
 as.vector(loadListNT)-loadNT
 as.vector(loadListNH4)-loadNH4
 as.vector(loadListNO3)-loadNO3
 as.vector(loadListBOD5)-loadBOD5
 as.vector(loadListSiO2)-loadSiO2
 cbind(anno,loadPTG,loadPO4,loadNT,loadNH4,loadNO3,loadBOD5,loa
 dSiO2)-carichi
 as.data.frame(carichi)-carichi.annui
 
 #if I type  
 carichi.annui
 #I get
  anno   loadPTG   loadPO4   loadNT  loadNH4  loadNO3 loadBOD5 loadSiO2
 1 2002  3.399518  1.382235 390.6959 22.07992 256.2244 
 492.9177 150.6505
 2 2003  1.559606 0.6271712 202.9181 6.198592 145.9498 
 63.07578 68.08632
 3 2004  2.363862 0.9493779 292.0841 12.21207 200.2545 
 141.0533 105.2409
 4 2005  1.64 0.6570313 217.2192  6.58045 155.7393 
 66.44154  73.0394
 5 2006  1.827174 0.7290634 235.7914 7.858396 166.8327  
 79.2474 80.92576
 6 2007  1.742629 0.6891045 228.0253 7.130082 162.6692 
 71.91434 77.22507
 7 2008 0.8382246 0.3612176 110.9079 2.024197 86.60459 
 25.46127  32.9733
 
 #which looks lika a data frame
 #And if I type
 is.data.frame(carichi.annui)
 # I get this:
 [1] TRUE
 
 #but if I try 
 write.table(carichi.annui, carichi.annui.lav)
 #It doesn't work?!?!?!
 Does someone have an explanation?
 Thanks a lot for any help!!
 Laura
 
Laura,

What do you mean by It doesn't work?  Do you get error messages?  Or, do
you just not get what you want?  And if so, what is that you want?

Dan

Daniel Nordlund
Bothell, WA USA

__
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@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] write.table

2009-02-20 Thread lauramorg...@bluewin.ch
Thank you for your advice, but I didn't manage to make it work...
I tried 

carichi.annui - 
data.frame(anno,loadPTG,loadPO4,loadNT,loadNH4,loadNO3,loadBOD5,loadSiO2)

And I got this error message:

Error in data.frame(anno, loadPTG, loadPO4, loadNT, loadNH4, loadNO3,  : 
  the arguments have a different numer of rows: 4, 1

If I do

sapply(carichi.annui, class)

I get:
anno  loadPTG  loadPO4   loadNT  loadNH4  loadNO3 loadBOD5 loadSiO2 
  list   list   list   list   list   list   list   list  

I thought that the function as.vector() could turn a list of numbers into a 
vector... was I wrong?

Messaggio originale
Da: rip...@stats.ox.ac.uk
Data: 20.02.2009 12.40
A: lauramorg...@bluewin.chlauramorg...@bluewin.ch
Copia: r-help@r-project.org
Oggetto: Re: [R] write.table

On Fri, 20 Feb 2009, lauramorg...@bluewin.ch wrote:

 Sorry, I'm using R 2.8.1 on Microsoft Windows XP professional 2002 Service 
 Pack 2.
 The error I get is
 Error in write.table(x, file, nrow(x), p, rnames, sep, eol,
 na, dec, as.integer(quote),  :
'list' type not implemented in 'EncodeElement'
 The problem is that I can' t manage to save the dataframe...
 With other dataframe I usually have no problems!!!

What does sapply(carichi.annui, class) tell you?

It would be better to use

carichi.annui - 
data.frame(anno,loadPTG,loadPO4,loadNT,loadNH4,loadNO3,loadBOD5,loadSiO2)

as you don't need an intermediate matrix.



 Hello,
 I tried to turn lists into vectors and then bind them
 together in order to create a dataframe but if, after this, I
 try to use the function write.table I get the following error message:
 Error in write.table(x, file, nrow(x), p, rnames, sep, eol,
 na, dec, as.integer(quote),  :
'list' type not implemented in 'EncodeElement'

 Here is what I've done:
 as.vector(c(unique(portate$Anno)))-anno
 as.vector(loadListPO4)-loadPO4
 as.vector(loadListPTG)-loadPTG
 as.vector(loadListNT)-loadNT
 as.vector(loadListNH4)-loadNH4
 as.vector(loadListNO3)-loadNO3
 as.vector(loadListBOD5)-loadBOD5
 as.vector(loadListSiO2)-loadSiO2
 cbind(anno,loadPTG,loadPO4,loadNT,loadNH4,loadNO3,loadBOD5,loa
 dSiO2)-carichi
 as.data.frame(carichi)-carichi.annui

 #if I type
 carichi.annui
 #I get
  anno   loadPTG   loadPO4   loadNT  loadNH4  loadNO3 loadBOD5 loadSiO2
 1 2002  3.399518  1.382235 390.6959 22.07992 256.2244
 492.9177 150.6505
 2 2003  1.559606 0.6271712 202.9181 6.198592 145.9498
 63.07578 68.08632
 3 2004  2.363862 0.9493779 292.0841 12.21207 200.2545
 141.0533 105.2409
 4 2005  1.64 0.6570313 217.2192  6.58045 155.7393
 66.44154  73.0394
 5 2006  1.827174 0.7290634 235.7914 7.858396 166.8327
 79.2474 80.92576
 6 2007  1.742629 0.6891045 228.0253 7.130082 162.6692
 71.91434 77.22507
 7 2008 0.8382246 0.3612176 110.9079 2.024197 86.60459
 25.46127  32.9733

 #which looks lika a data frame
 #And if I type
 is.data.frame(carichi.annui)
 # I get this:
 [1] TRUE

 #but if I try
 write.table(carichi.annui, carichi.annui.lav)
 #It doesn't work?!?!?!
 Does someone have an explanation?
 Thanks a lot for any help!!
 Laura

 Laura,

 What do you mean by It doesn't work?  Do you get error messages?  Or, do
 you just not get what you want?  And if so, what is that you want?

 Dan

 Daniel Nordlund
 Bothell, WA USA

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


-- 
Brian D. Ripley,  rip...@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595

__
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] write.table

2009-02-20 Thread lauramorg...@bluewin.ch
Thank you!!!
unlist() worked perfectly!!!
Have a nice weekend
Laura
Messaggio originale
Da: petr.pi...@precheza.cz
Data: 20.02.2009 14.58
A: lauramorg...@bluewin.ch
Copia: r-help@r-project.org
Oggetto: Re: [R] write.table

Hi

r-help-boun...@r-project.org napsal dne 20.02.2009 12:54:41:

 Thank you for your advice, but I didn't manage to make it work...
 I tried 
 
 carichi.annui - data.frame
 (anno,loadPTG,loadPO4,loadNT,loadNH4,loadNO3,loadBOD5,loadSiO2)
 
 And I got this error message:
 
 Error in data.frame(anno, loadPTG, loadPO4, loadNT, loadNH4, loadNO3,  : 

   the arguments have a different numer of rows: 4, 1
 
 If I do
 
 sapply(carichi.annui, class)
 
 I get:
 anno  loadPTG  loadPO4   loadNT  loadNH4  loadNO3 loadBOD5 loadSiO2 
   list   list   list   list   list   list   list   list 
 

Well, here is finally something. You can transfer lists to data frames.


lll-list(x=rnorm(5), y=rnorm(5))
do.call(data.frame,lll)

or if you have vector list like that

x-list(rnorm(5))
 x
[[1]]
[1] -0.7349324 -0.4697276 -1.2026201 -1.4366955 -1.4713526

use unlist

 unlist(x)
[1] -0.7349324 -0.4697276 -1.2026201 -1.4366955 -1.4713526


Regards
Petr


 I thought that the function as.vector() could turn a list of numbers 
into a 
 vector... was I wrong?
 
 Messaggio originale
 Da: rip...@stats.ox.ac.uk
 Data: 20.02.2009 12.40
 A: lauramorg...@bluewin.chlauramorg...@bluewin.ch
 Copia: r-help@r-project.org
 Oggetto: Re: [R] write.table
 
 On Fri, 20 Feb 2009, lauramorg...@bluewin.ch wrote:
 
  Sorry, I'm using R 2.8.1 on Microsoft Windows XP professional 2002 
Service Pack 2.
  The error I get is
  Error in write.table(x, file, nrow(x), p, rnames, sep, eol,
  na, dec, as.integer(quote),  :
 'list' type not implemented in 'EncodeElement'
  The problem is that I can' t manage to save the dataframe...
  With other dataframe I usually have no problems!!!
 
 What does sapply(carichi.annui, class) tell you?
 
 It would be better to use
 
 carichi.annui - 
 
data.frame(anno,loadPTG,loadPO4,loadNT,loadNH4,loadNO3,loadBOD5,loadSiO2)
 
 as you don't need an intermediate matrix.
 
 
 
  Hello,
  I tried to turn lists into vectors and then bind them
  together in order to create a dataframe but if, after this, I
  try to use the function write.table I get the following error 
message:
  Error in write.table(x, file, nrow(x), p, rnames, sep, eol,
  na, dec, as.integer(quote),  :
 'list' type not implemented in 'EncodeElement'
 
  Here is what I've done:
  as.vector(c(unique(portate$Anno)))-anno
  as.vector(loadListPO4)-loadPO4
  as.vector(loadListPTG)-loadPTG
  as.vector(loadListNT)-loadNT
  as.vector(loadListNH4)-loadNH4
  as.vector(loadListNO3)-loadNO3
  as.vector(loadListBOD5)-loadBOD5
  as.vector(loadListSiO2)-loadSiO2
  cbind(anno,loadPTG,loadPO4,loadNT,loadNH4,loadNO3,loadBOD5,loa
  dSiO2)-carichi
  as.data.frame(carichi)-carichi.annui
 
  #if I type
  carichi.annui
  #I get
   anno   loadPTG   loadPO4   loadNT  loadNH4  loadNO3 loadBOD5 
loadSiO2
  1 2002  3.399518  1.382235 390.6959 22.07992 256.2244
  492.9177 150.6505
  2 2003  1.559606 0.6271712 202.9181 6.198592 145.9498
  63.07578 68.08632
  3 2004  2.363862 0.9493779 292.0841 12.21207 200.2545
  141.0533 105.2409
  4 2005  1.64 0.6570313 217.2192  6.58045 155.7393
  66.44154  73.0394
  5 2006  1.827174 0.7290634 235.7914 7.858396 166.8327
  79.2474 80.92576
  6 2007  1.742629 0.6891045 228.0253 7.130082 162.6692
  71.91434 77.22507
  7 2008 0.8382246 0.3612176 110.9079 2.024197 86.60459
  25.46127  32.9733
 
  #which looks lika a data frame
  #And if I type
  is.data.frame(carichi.annui)
  # I get this:
  [1] TRUE
 
  #but if I try
  write.table(carichi.annui, carichi.annui.lav)
  #It doesn't work?!?!?!
  Does someone have an explanation?
  Thanks a lot for any help!!
  Laura
 
  Laura,
 
  What do you mean by It doesn't work?  Do you get error messages? Or, 
do
  you just not get what you want?  And if so, what is that you want?
 
  Dan
 
  Daniel Nordlund
  Bothell, WA USA
 
  __
  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@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.
 
 
 -- 
 Brian D. Ripley,  rip...@stats.ox.ac.uk
 Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
 University of Oxford, Tel:  +44 1865 272861 (self)
 1 South Parks Road, +44 1865 272866 (PA)
 Oxford OX1 3TG, UKFax:  +44 1865 272595
 
 __
 R-help@r-project.org mailing list

Re: [R] read.table : how to condition on error while opening file?

2009-02-19 Thread lauramorg...@bluewin.ch
Hello Stephane,
here is something you could try, 

filelist - c(file1.txt, file2.txt, file3.txt)
for (i in 1:3) {
tmpList-try(read.table(filelist[[i]]), silent=TRUE)
if(inherits(tmpList, try-error))
{print(paste(error opening file , filelist[[i]]))
} else {
tmp-read.table(filelist[[i]])-namelist[[i]]
   }
 }

There is though a problem that I didnt manage to fix, that is: if , suppose, 
file1.txt exists, file2 doesn't exist 
and file 3 exists,
the dataframe in file 1 will at first be called tmp, but then it will be 
substituted by the data.frame in file 3...
It is as if you would do:
c(1,2,3,4)-tmp
and then do
c(1,6,7,8)-tmp
the second tmp will substitute the first one...

Hope this helps
Laura
Messaggio originale
Da: e.vettora...@uke.uni-hamburg.de
Data: 19.02.2009 17.23
A: Stephane Bourgeoiss...@sanger.ac.uk
Copia: r-help@r-project.org
Oggetto: Re: [R] read.table : how to condition on error while opening file?

Hi Stephane,
see ?try
hth.


Stephane Bourgeois schrieb:
 Hi,

  

 I'm using read.table in a loop, to read in multiple files. The problem
 is that when a file is missing there is an error message and the loop is
 broken; what I'd like to do is to test for the error and simply do
 next instead of breaking the loop. Anybody knows how to do that?

  

 Example: 

  

 filelist - c(file1.txt, file2.txt, file3.txt)

  

 for (i in 1:3) {

   if (read.table(filelist[i]) == ERROR LOADING FILE) {
 # this is where I do not know how to write the condition

 print(paste(error opening file , filelist[i], sep=))

 next

   } else {

 tmp - read.table(filelist[i])

   }

 }

  

  

 Cheers,

  

 Stephane




   

-- 
Eik Vettorazzi
Institut für Medizinische Biometrie und Epidemiologie
Universitätsklinikum Hamburg-Eppendorf

Martinistr. 52
20246 Hamburg

T ++49/40/42803-8243
F ++49/40/42803-7790

__
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@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] write.table

2009-02-19 Thread lauramorg...@bluewin.ch
Hello,
I tried to turn lists into vectors and then bind them together in order to 
create a dataframe but if, after this, I 
try to use the function write.table I get the following error message:
Error in write.table(x, file, nrow(x), p, rnames, sep, eol, na, dec, 
as.integer(quote),  : 
   'list' type not implemented in 'EncodeElement'
 
Here is what I've done:
as.vector(c(unique(portate$Anno)))-anno
as.vector(loadListPO4)-loadPO4
as.vector(loadListPTG)-loadPTG
as.vector(loadListNT)-loadNT
as.vector(loadListNH4)-loadNH4
as.vector(loadListNO3)-loadNO3
as.vector(loadListBOD5)-loadBOD5
as.vector(loadListSiO2)-loadSiO2
cbind(anno,loadPTG,loadPO4,loadNT,loadNH4,loadNO3,loadBOD5,loadSiO2)-carichi
as.data.frame(carichi)-carichi.annui

#if I type  
carichi.annui
#I get
 anno   loadPTG   loadPO4   loadNT  loadNH4  loadNO3 loadBOD5 loadSiO2
1 2002  3.399518  1.382235 390.6959 22.07992 256.2244 492.9177 150.6505
2 2003  1.559606 0.6271712 202.9181 6.198592 145.9498 63.07578 68.08632
3 2004  2.363862 0.9493779 292.0841 12.21207 200.2545 141.0533 105.2409
4 2005  1.64 0.6570313 217.2192  6.58045 155.7393 66.44154  73.0394
5 2006  1.827174 0.7290634 235.7914 7.858396 166.8327  79.2474 80.92576
6 2007  1.742629 0.6891045 228.0253 7.130082 162.6692 71.91434 77.22507
7 2008 0.8382246 0.3612176 110.9079 2.024197 86.60459 25.46127  32.9733

#which looks lika a data frame
#And if I type
is.data.frame(carichi.annui)
# I get this:
[1] TRUE

#but if I try 
write.table(carichi.annui, carichi.annui.lav)
#It doesn't work?!?!?!
Does someone have an explanation?
Thanks a lot for any help!!
Laura

__
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] summary of a list

2009-02-13 Thread lauramorg...@bluewin.ch

Hello Dieter and everyone,
Thank you for your advice... but I didn't manage to solve my problem... :-(
I actually like the fact that R tells me which ones of my regressions didn't 
achieve convergence and gives me a 
warning saying that there was a false or singular convergence...
the problem is that out of 35 regressions I do with my loop, only 5 don't 
achieve convergence and I would like to be 
able to get the summaries(or sigma, or coef, ...)
of the 30 regressions that did achieve convergence... (I know from the start 
that some regression functions will work 
for some variable and not necessarily for others)
At the moment I check which ones reach convergence by doing 

print(resultList)

and then I extract the summary by doing the following loop:

summaryList-list()
for (i in c(1:8, 11:14, 16:24, 26:34))
{
summaryList[[i]]-summary(resultList[[i]])
}

But this is quite tedious, since I will have to repeat it for other rivers and 
also in the next years... 
Is there a way to tell R to show me the summary of all the results anyway? 
Cause right now if I do:
summaryList-list()
for (i in length(resultList))
{
summaryList[[i]]-summary(resultList[[i]])
}
R only shows me the first 8 summaries and then stops because the ninth didn't 
reach convergence...
Any suggestion?
P.S. I tried to use nls2 but I have the same problems...


lauramorgana at bluewin.ch lauramorgana at bluewin.ch writes:

 
 Hello,
 I'm using the following for loop to find regression curves using a list of
functions (formList), a list of

.. long non-reproducible code removed

 And I got the following error message:
 Error in chol2inv(object$m$Rmat()) : l'elemento (3, 3) è zero, quindi
l'inversa non può essere calcolata
 Which translated should be: Error in chol2inv(object$m$Rmat()): the element
(3, 3) is zero (NULL?),
 that's why the 
 inverse (inverse function?) can not be computed 

nls is rather nasty or nice in telling you when the result should not 
be trusted. Other software gives nonsense result without blinking.

In package nlme, there is a function nlsList which does directly what you
want, but there is little help besides revising the model for the cases
without convergence. 

Also try check Gabor Grothendieks package nls2 which could help you 
finding better start values.

Dieter

__
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@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] odfWeave prettyR

2009-02-13 Thread lauramorg...@bluewin.ch
Hello,
I've been trying to use odfWeave and prettyR packages to create documents with 
both text and graphs, but so far I 
haven't been very lucky...
With the function R2html () in prettyR package, when I try to use a source 
file, which works perfectly if I run it 
directly form R using source(file), 
it works for the first part and then it makes a mess, i guess something might 
be wrong in my formatting or the 
function R2html doesnt recognize the function if()
because i get errors like this one
Error in parse(cmdcon) : unexpected 'if' at
1: for (i in 1:length(resultList)){tempSummary - 
try(summary(resultList[[i]]), silent = TRUE)if

everytime there is a if.

I tried with the function htmlize and it works for the text (like summaries) 
but I didn't manage to get the plots on 
the html file
I tried to use the funcion HTMLgraph but I couldn't understand what it does and 
what its arguments are (listfile, 
graphfile)

With odfWeave I tried to download it, and saw that it depends on XML package. 
If I try to load the odfWeave package 
with
library(odfWeave) I get this message:

Loading: XML
Error: package 'XML' not loaded
Warning message:
In library(pkg, character.only = TRUE, logical.return = TRUE, lib.loc = 
lib.loc) :
 there is no 'XML' package

The problem is that on the CRAN web site the XML package is not available for 
windows...
Any suggestions?
Thanks and sorry for bothering you all again with this subject...

__
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] plotting the result of a nonlinear regression

2009-02-12 Thread lauramorg...@bluewin.ch
Thank you for the suggestion of using predict!
If someone is interested here is the loop I used:

seq(0,max(subset(dati, Fiume==Laveggio)$Portata), length=100)-seqQ

for(i in 1:35)
{
  jpeg(paste(result,i,.jpg))
  
plot(subset(dati,Fiume==Laveggio)$PTG.P~subset(dati,Fiume==Laveggio)$Portata,
 
   main=print(formList[[i]]), sub=print(summary(resultList[[i]])$sigma))
  lines(seqQ, predict(resultList[[i]], list(Portata = seqQ)))
  dev.off()
  }

Messaggio originale
Da: ssef...@gmail.com
Data: 10.02.2009 18.15
A: lauramorg...@bluewin.ch
Copia: r-help@r-project.org
Oggetto: Re: [R] plotting the result of a nonlinear regression

?predict

On Tue, Feb 10, 2009 at 11:06 AM, lauramorg...@bluewin.ch
lauramorg...@bluewin.ch wrote:
 Hello,
 to plot the result of a singular non linear regression (using nls) I usually 
 use the function plotfit, for 
example:

 r.PTG.V-nls(PTG.P~ fz1(Portata, a,b), data=dati, start=list(a=10, b=10), 
 nls.control(maxiter=200), 
algorithm='port',
 trace=TRUE, na.action=na.omit, lower=list(a=0, b=10), upper=list(a=100, 
 b=100))
 plotfit(r.PTG.V)

 I tried to use the function plotfit on the result of the following for loop 
 but I got an error message:
 for (i in 1:length(formList))
 {
resultList[[i]] - nls(formList[[i]], data=subset(dati, 
 Fiume==Laveggio), start=startList7[[i]],
nls.control(maxiter=1000, warnOnly=TRUE), algorithm='port', 
 na.action=na.omit,lower=lowerList7[[i]],
upper=upperList7[[i]])
 }
 plotfit(resultList[[1]]

 Error in diff(as.numeric(y[ord])) :
  (list) object cannot be coerced to type 'double'

 Could somebody tell me what this error means? how can I fix this problem?
 Any suggestion appreciated!!
 Thanks,
 Laura

 __
 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.




-- 
Stephen Sefick

Let's not spend our time and resources thinking about things that are
so little or so large that all they really do for us is puff us up and
make us feel like gods.  We are mammals, and have not exhausted the
annoying little problems of being mammals.

-K. Mullis

__
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] plotting the result of a nonlinear regression

2009-02-10 Thread lauramorg...@bluewin.ch
Hello,
to plot the result of a singular non linear regression (using nls) I usually 
use the function plotfit, for example:

r.PTG.V-nls(PTG.P~ fz1(Portata, a,b), data=dati, start=list(a=10, b=10), 
nls.control(maxiter=200), algorithm='port', 
trace=TRUE, na.action=na.omit, lower=list(a=0, b=10), upper=list(a=100, b=100))
plotfit(r.PTG.V)

I tried to use the function plotfit on the result of the following for loop but 
I got an error message:
for (i in 1:length(formList))
{
resultList[[i]] - nls(formList[[i]], data=subset(dati, Fiume==Laveggio), 
start=startList7[[i]], 
nls.control(maxiter=1000, warnOnly=TRUE), algorithm='port', 
na.action=na.omit,lower=lowerList7[[i]], 
upper=upperList7[[i]])
}
plotfit(resultList[[1]]

Error in diff(as.numeric(y[ord])) : 
  (list) object cannot be coerced to type 'double'

Could somebody tell me what this error means? how can I fix this problem? 
Any suggestion appreciated!!
Thanks,
Laura

__
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] summary of a list

2009-02-10 Thread lauramorg...@bluewin.ch
Hello,
I'm using the following for loop to find regression curves using a list of 
functions (formList), a list of starting 
values (startList), uppervalues (upperList) and lower values (lowerList).
A sample of the list of function I use in the loop is the following:

FormList - list(PTG.P ~ fz1(Portata, a, b), PTG.P ~ fz2(Portata, a, b), PTG.P 
~ fz3(Portata,a, b, d, e),
PTG.P ~ fz4(Portata, a, b), PTG.P ~ fz5(Portata, a, b, d), PO4.P ~ fz1(Portata, 
a, b),
PO4.P ~ fz2(Portata, a, b), ...

And the loop I use is:
resultList - list()
for (i in 1:length(formList))
{
resultList[[i]] - nls(formList[[i]], data=subset(dati, Fiume==Laveggio), 
start=startList[[i]], 
nls.control(maxiter=1000, warnOnly=TRUE), algorithm='port', 
na.action=na.omit,lower=lowerList[[i]], 
upper=upperList[[i]])
}
When the computation ends I get 5 warning messages (one of false convergence, 4 
of singular convergence:
In nls(formList[[i]], data = subset(dati, Fiume == Laveggio),  : Convergence 
failure: false convergence (8)
2: In nls(formList[[i]], data = subset(dati, Fiume == Laveggio),  : 
Convergence failure: singular convergence (7)

If I want to get the summary of the first object of the resultList I do:
summary(resultList[[1]])
And I get a result with no problem:
Formula: PTG.P ~ fz1(Portata, a, b)

Parameters:
  Estimate Std. Error t value Pr(|t|)   
a   61.158 18.591   3.290  0.00140 **
b7.616  8.720   0.873  0.38464   
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

Residual standard error: 91.32 on 96 degrees of freedom

Algorithm port, convergence message: both X-convergence and relative 
convergence (5)

It also works if I try to get the summary of the first 5 object of the 
resultList with:
summaryList-list()
for (i in 1:5)
{
summaryList[[i]]-summary(resultList[[i]])
}

But if I try to get the summary of all the objects of the resultList (there are 
35 objects) it doesn't work...
I tried:
summaryList-list()
 for (i in 1:length(resultList))
+ {
+ summaryList[[i]]-summary(resultList[[i]])
+ }
And I got the following error message:
Error in chol2inv(object$m$Rmat()) : l'elemento (3, 3) è zero, quindi l'inversa 
non può essere calcolata
Which translated should be: Error in chol2inv(object$m$Rmat()): the element (3, 
3) is zero (NULL?), that's why the 
inverse (inverse function?) can not be computed 

Does somebody have an idea on how to fix this? 

Thanks
Laura

__
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] non linear regression with nls

2009-02-06 Thread lauramorg...@bluewin.ch
Thank you! It worked perfectly, also for the other variables!

Messaggio originale
Da: r...@life.ku.dk
Data: 06.02.2009 13.29
A: lauramorg...@bluewin.ch
Oggetto: Re: [R] non linear regression with nls

Hi Laura,

I think you have to make a list formulas:


formList - list(NT.N ~ fz1(Portata, a, b), NT.N ~ fz2(Portata, a, b), NT.N ~ 
fz3(Portata,
a, b, d, e), NT.N ~ fz4(Portata, a, b), NT.N ~ fz5(Portata, a, b, d))


and then in the loop:

resultList[[i]] - nls(formList[[i]], ...



Christian

__
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] non linear regression with nls

2009-02-05 Thread lauramorg...@bluewin.ch
Thank you a lot Mr. Ritz!
I've tried the loop you suggested and I added a list for lower and upper limits 
of the parameters, but there is still 
a problem... 

I have a list of functions, which works...
fz1-function(Portata, a, b){a+(b/Portata)} 
fz2-function(Portata, a, b){a*exp(b*Portata)}
fz3-function(Portata, a, b, d, e){a+(b/Portata)+d*(Portata^e)}
fz4-function(Portata, a, b){a*Portata^b}
fz5-function(Portata, a, b, d){a+b*(Portata^d)}
fctList - list(fz1, fz2, fz3, fz4, fz5)

as well as lists for starting values, upper and lower values, which work as 
well:
startList - list(list(a=10, b=10), list(a=10, b=1), list(a=10, b=10, d=10, 
e=1), list(a=10, b=1), list(a=10, b=10, 
d=1))
lowerList-list(list(a=0,b=0),list(a=0,b=0), list(a=0,b=0,d=0,e=-50),list(a=0, 
b=-50), list(a=0, b=0, d=-50))
upperList-list(list(a=1000, b=1000), list(a=1000, b=1000), 
list(a=1000,b=1000,d=1000,e=50), list(a=1000,b=50), list
(a=1000, b=1000, d=50))

but if I try to run this for loop


resultList - list()
for (i in 1:5)
{
resultList[[i]] - nls(NT.N ~ fctList[[i]](Portata, a,b), data=subset(dati, 
Fiume==Laveggio), start=startList[[i]], 
nls.control(maxiter=200), algorithm='port', trace=TRUE, na.action=na.omit, 
upper=upperList[[i]], lower=lowerList[[i]])
}
 I get the following error message:
Error in fctList[[i]](Portata, a, b) : element 1 is empty;
   the part of the args list of '*' being evaluated was:
   (d, (Portata^e))

I realized that the problem is the element after the function, i.e. (Portata, 
a, b), since fct 3 and 5 have more 
parameters (Portata,a,b,d,e).
So I tried to make a list (parList) for this too, i tried 2 versions:
#version1
list(Portata,a, b)-pf1.2.4
list(Portata,a,b,d,e)-pf3
list(Portata,a,b,d)-pf5
parList-list(pf1.2.4, pf1.2.4,pf3,pf1.2.4,pf5)

#version 2
parList-list(Portata,a,b,Portata,a,b,Portata,a,b,d,e, 
Portata,a,b,Portata,a,b,d)

and then I tried them (one at a time) in the loop:

resultList - list()
for (i in 1:5)
{
resultList[[i]] - nls(NT.N ~ fctList[[i]](parList[[i]]), data=subset(dati, 
Fiume==Laveggio), start=startList
[[i]], nls.control(maxiter=200), algorithm='port', trace=TRUE, 
na.action=na.omit,upper=upperList[[i]], lower=lowerList
[[i]])
}
but I got this error message:  Error in fctList[[i]](parList[i]) : element 1 
is empty;
   the part of the args list of '+' being evaluated was:
   (a, (b/Portata))

What can I do to fix it?
I'm also wondering which kind of function (maybe another loop?) I could use to 
automize the regression not only for 
the variable NT.N but for every variable (PTG.P, PO4.P,. ..)

My dataframe look like this (a sample):

Fiume giorno mese anno Portata  PTG.P   PO4.P   NT.N  NH4.N NO3.N   
BOD5 SiO2  data
1Vedeggio 101 1995   0.981 218.40 118.000  9.196 6.5700  2.06  
6.080 4.33 34709
2Vedeggio  72 1995   0.965 125.84  54.000  8.701 5.2600  2.31 
16.480 4.43 34737
3Vedeggio  73 1995   1.536  37.44  12.000  7.271 5.5600  1.88  
5.240 4.15 34765
...
190 Cassarate 299 2008   1.240  26.00  20.000  2.480 0.1200  1.79  
1.700 4.03 39720
191 Cassarate 13   10 2008   0.860  23.00  16.000  2.720 0.0200  2.13  
1.780 3.71 39734
192 Cassarate 10   11 2008   8.840  26.00  14.000  2.900 0.0500NA  
1.400 3.62 39762
193 Cassarate  9   12 2008   2.030  35.00  23.000  2.190 0.0700  1.79  
1.950 3.74 39791
...
279  Laveggio 151 2002   0.347  77.00  30.000  9.690 0.4300  7.23  
1.950 4.17 37271
280  Laveggio 112 2002   0.527  54.00  17.000  7.520 0.8800  5.87  
2.410 3.58 37298
281  Laveggio 133 2002   0.900  34.00  15.000  7.520 0.7100  6.17  
6.550 3.03 37328
...
Thanks to anyone that could give me any hint!!
Laura




## a for loop
resultList - list()
for (i in 1:5)
{
## storing the result as the i'th list component
## notice that the i'th list components in fctList and startList
## are used for the i'th fit
resultList[[i]] - nls(NT.N ~ fctList[[i]](parList[[i]]), data=subset(dati, 
Fiume==Laveggio), start=startList
[[i]], nls.control(maxiter=200), algorithm='port', trace=TRUE, 
na.action=na.omit)
}

Messaggio originale
Da: r...@life.ku.dk
Data: 03.02.2009 19.00
A: lauramorg...@bluewin.ch
Oggetto: Re: [R] non linear regression with nls

Hi Laura,

I've the following suggestion for you using several lists and a for loop:


fz1-function(Portata, a, b){a+b/Portata}
fz2-function(Portata, a, b){a*exp(b*Portata)}
fz3-function(Portata, a, b, d, e){a+b/Portata+d*(Portata^e)}
fz4-function(Portata, b, d){b*Portata^d}
fz5-function(Portata, a, b, d){a+b*(Portata^d)}
fctList - list(fz1, fz2, fz3, fz4, fz5)

startList - list(list(a=10, b=10), list(a=10, b=1), start=list(a=10, b=10, 
d=10, e=10),
list(a=10, b=1), list(a=10, b=10, d=1))

## a for loop
resultList - list()
for (i in 1:5)
{
## storing the result as the i'th list component
## notice that the i'th list components in fctList and startList
## are used for the i'th fit
resultList[[i]] - nls(NT.N

Re: [R] non linear regression with nls

2009-02-05 Thread lauramorg...@bluewin.ch
Hello, thanks for the advice of nlsList!
I tried to look at the help page of nlsList, but I didnt understand how to use 
the subset argument of the function 
and it's not clear to 
me if this only allows you to choose one subset or if it run the regression for 
every given subset, in this case how 
can someone specify the groups/subset?
Thanks a lot!
Laura


Messaggio originale
Da: kfr...@wisc.edu
Data: 03.02.2009 15.36
A: lauramorg...@bluewin.ch
Oggetto: Re: [R] non linear regression with nls

Hi, Laura-

You might have a look at ?nlsList().

Ken


- Original Message -
From: lauramorg...@bluewin.ch lauramorg...@bluewin.ch
Date: Tuesday, February 3, 2009 4:38 am
Subject: [R] non linear regression with nls
To: r-help@r-project.org


 Hello,
 I'm a beginner with R and it's the first time I'm using the R-help 
 list... I hope I'm in the right place, if not: 
 Sorry!!
 
 I need to do non linear regressions on a data set which columns are:
 river.namePortata  PTG.P   PO4.P   NT.NNH4.N   
 NO3.N   BOD5SiO2   
 I need to predict every variable (PTG, PO4, NT, ..., which are 
 concentration of substances in water) starting from 
 the Portata variable (which is the water flow)
 The functions that I'm using are:
  fz1-function(Portata, a, b){a+b/Portata}
  fz2-function(Portata, a, b){a*exp(b*Portata)}
  fz3-function(Portata, a, b, d, e){a+b/Portata+d*(Portata^e)}
  fz4-function(Portata, b, d){b*Portata^d}
  fz5-function(Portata, a, b, d){a+b*(Portata^d)}
 I've made a list of the functions with list(fz1, fz2, fz3, fz4, fz5)
 
 and the starting , lower and upper parameters for each function are:
 fz1: start=list(a=10, b=10), lower=list(a=0, b=0), upper=list(a=1000, 
 b=1000) 
 fz2: start=list(a=10, b=1), lower=list(a=0, b=0), upper=list(a=1000, b=1000)
 fz3: start=list(a=10, b=10, d=10, e=10), lower=list(a=0, b=0, d=0, 
 e=-50), upper=list(a=1000, b=1000, d=1000, e=50) 
 fz4: start=list (a=10, b=1), lower=list(a=0, b=-50), 
 upper=list(a=1000, b=50)
 fz5: start=list(a=10, b=10, d=1), lower=list(a=0, b=0, d=-50), 
 upper=list(a=1000, b=1000, d=50)
 
 so far i manage to do non linear regression one at a time that is, 
 using one function for one river) using nls(), for 
 example:
 r.NT.lav-nls(NT.N~ fz1(Portata, a,b), 
 data=subset(dati,river.name==Laveggio), start=list(a=10, b=10), nls.control
 (maxiter=200), algorithm='port', trace=TRUE, na.action=na.omit, 
 lower=list(a=0, b=0), upper=list(a=1000, b=1000))
 and then I get the results with summary() and the graph using plotfit()
 
 but this will get extremly long since I have 12 rivers to analize for 
 every variable and then compare the results, so 
 I'd like to use a loop (cycle for??) but I can't figure out how it 
 works. I've tried to look on the help page on ?
 Control (control flow) but I didn't understand it... 
 Can somebody help me (give me a hint or an example of a loop) to 
 automize the regression and save the results
 Please consider that my knowledge of computer programming is 
 practically non-existent!!
 Thanks a lot!
 Laura  F.
 
 __
 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@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] non linear regression with nls

2009-02-03 Thread lauramorg...@bluewin.ch
Hello,
I'm a beginner with R and it's the first time I'm using the R-help list... I 
hope I'm in the right place, if not: 
Sorry!!

I need to do non linear regressions on a data set which columns are:
river.namePortata  PTG.P   PO4.P   NT.NNH4.N   NO3.N   
BOD5SiO2   
I need to predict every variable (PTG, PO4, NT, ..., which are concentration of 
substances in water) starting from 
the Portata variable (which is the water flow)
The functions that I'm using are:
 fz1-function(Portata, a, b){a+b/Portata}
 fz2-function(Portata, a, b){a*exp(b*Portata)}
 fz3-function(Portata, a, b, d, e){a+b/Portata+d*(Portata^e)}
 fz4-function(Portata, b, d){b*Portata^d}
 fz5-function(Portata, a, b, d){a+b*(Portata^d)}
I've made a list of the functions with list(fz1, fz2, fz3, fz4, fz5)

and the starting , lower and upper parameters for each function are:
fz1: start=list(a=10, b=10), lower=list(a=0, b=0), upper=list(a=1000, b=1000) 
fz2: start=list(a=10, b=1), lower=list(a=0, b=0), upper=list(a=1000, b=1000)
fz3: start=list(a=10, b=10, d=10, e=10), lower=list(a=0, b=0, d=0, e=-50), 
upper=list(a=1000, b=1000, d=1000, e=50) 
fz4: start=list (a=10, b=1), lower=list(a=0, b=-50), upper=list(a=1000, b=50)
fz5: start=list(a=10, b=10, d=1), lower=list(a=0, b=0, d=-50), 
upper=list(a=1000, b=1000, d=50)

so far i manage to do non linear regression one at a time that is, using one 
function for one river) using nls(), for 
example:
r.NT.lav-nls(NT.N~ fz1(Portata, a,b), 
data=subset(dati,river.name==Laveggio), start=list(a=10, b=10), nls.control
(maxiter=200), algorithm='port', trace=TRUE, na.action=na.omit, lower=list(a=0, 
b=0), upper=list(a=1000, b=1000))
and then I get the results with summary() and the graph using plotfit()

but this will get extremly long since I have 12 rivers to analize for every 
variable and then compare the results, so 
I'd like to use a loop (cycle for??) but I can't figure out how it works. I've 
tried to look on the help page on ?
Control (control flow) but I didn't understand it... 
Can somebody help me (give me a hint or an example of a loop) to automize the 
regression and save the results
Please consider that my knowledge of computer programming is practically 
non-existent!!
Thanks a lot!
Laura  F.

__
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.