Re: [R] problem with looping formula through table

2011-03-14 Thread Scott Chamberlain
library(tseries)

sie - get.hist.quote(instrument=SIE.DE, start=2010-01-01, quote=AdjClose)
vow - get.hist.quote(instrument=VOW.DE, start=2010-01-01, quote=AdjClose)
lin - get.hist.quote(instrument=LIN.DE, start=2010-01-01, quote=AdjClose)
dax - get.hist.quote(instrument=^GDAXI, start=2010-01-01, quote=AdjClose)

returns.table - diff(log(na.omit(merge(dax, lin, sie, vow

# My function to get the betas pval is:

B - function(share,bench){
beta - summary(lm(share~bench))$coef[2]
pval - summary(lm(share~bench))$coef[8]
coefs - t(as.matrix(c(beta, pval), ncol=2, byrow=TRUE))
coefs
}

# The function B works fine:
B(returns.table[,2], returns.table[,1])

# this loop should work
result - matrix(NA, length(names(returns.table))-1, 2)
for(i in 2:4){
result[i-1,] - B(returns.table[,i],returns.table[,1])
}
result

On Sunday, March 13, 2011 at 8:03 AM, herr dittmann wrote: 
 Dear useRs,
 
 I am stuck with a piece of code and hope you could give me some pointers.
 
 My aim is to calculate the lm-regression coefficients of individual stocks 
 against an index. I am interested in both the coefficient and the pval. While 
 I could do this manually for a select hand full, I hope to scale this up say 
 for 30+ stocks (DAX-30, FTSE-100 etc.) to eventually have a matrix of 
 coefficients and p-values for each individual stock.
 
 First, let's get share prices:
 
 library(tseries)
 
 sie - get.hist.quote(instrument=SIE.DE, start=2010-01-01, 
 quote=AdjClose)
 vow - get.hist.quote(instrument=VOW.DE, start=2010-01-01, 
 quote=AdjClose)
 lin - get.hist.quote(instrument=LIN.DE, start=2010-01-01, 
 quote=AdjClose)
 dax - get.hist.quote(instrument=^GDAXI, start=2010-01-01, 
 quote=AdjClose)
 
 returns.table - diff(log(na.omit(merge(dax, lin, sie, vow
 
 My function to get the betas pval is:
 
 B - function(share,bench){
 beta - summary(lm(share~bench))$coef[2]
 pval - summary(lm(share~bench))$coef[8]
 coefs - t(as.matrix(c(beta, pval), ncol=2, byrow=TRUE))
 coefs
 }
 
 The function B works fine:
 
 B(returns[,2],returns[,1])
 
  B(returns[,2],returns[,1])
  [,1]  [,2]
 [1,] 0.7568787 9.740043e-47
 
 
 Now, at the following step I am stuck. I am trying to loop through my 
 returns.table:
 
 Attempt 1:
 
 for(i in 2:4){result[i] - B(returns[,i],returns[,1]); result}
 Error in result[i] - B(returns[, i], returns[, 1]) : 
  object 'result' not found
 
 
 Attempt 2:
 
 for(i in 2:4){print(B(returns[,i],returns[,1]))}
  [,1]  [,2]
 [1,] 0.7568787 9.740043e-47
  [,1]  [,2]
 [1,] 1.311835 2.924594e-86
  [,1]  [,2]
 [1,] 1.023310 1.078007e-30
 
 Attempt 2 gets me a little closer to the desired matrix of coefficient and 
 pval by each share.
 
 
 What am I doing wrong here?
 
 Any pointers most welcome.
 
 Many thanks in advance!
 
 Regards,
 
 Bernd
 
 
 
 
 
 __
 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.
 

[[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] problem with looping formula through table

2011-03-13 Thread herr dittmann
Dear useRs,

I am stuck with a piece of code and hope you could give me some pointers.

My aim is to calculate the lm-regression coefficients of individual stocks 
against an index. I am interested in both the coefficient and the pval. While I 
could do this manually for a select hand full, I hope to scale this up say for 
30+ stocks (DAX-30, FTSE-100 etc.) to eventually have a matrix of coefficients 
and p-values for each individual stock.

First, let's get share prices:

library(tseries)

sie - get.hist.quote(instrument=SIE.DE, start=2010-01-01, quote=AdjClose)
vow - get.hist.quote(instrument=VOW.DE, start=2010-01-01, quote=AdjClose)
lin - get.hist.quote(instrument=LIN.DE, start=2010-01-01, quote=AdjClose)
dax - get.hist.quote(instrument=^GDAXI, start=2010-01-01, quote=AdjClose)

returns.table - diff(log(na.omit(merge(dax, lin, sie, vow

My function to get the betas pval is:

B - function(share,bench){
beta - summary(lm(share~bench))$coef[2]
pval - summary(lm(share~bench))$coef[8]
coefs - t(as.matrix(c(beta, pval), ncol=2, byrow=TRUE))
coefs
}

The function B works fine:

B(returns[,2],returns[,1])

 B(returns[,2],returns[,1])
  [,1] [,2]
[1,] 0.7568787 9.740043e-47


Now, at the following step I am stuck. I am trying to loop through my 
returns.table:

Attempt 1:

for(i in 2:4){result[i] - B(returns[,i],returns[,1]); result}
Error in result[i] - B(returns[, i], returns[, 1]) : 
  object 'result' not found


Attempt 2:

for(i in 2:4){print(B(returns[,i],returns[,1]))}
  [,1] [,2]
[1,] 0.7568787 9.740043e-47
 [,1] [,2]
[1,] 1.311835 2.924594e-86
 [,1] [,2]
[1,] 1.023310 1.078007e-30

Attempt 2 gets me a little closer to the desired matrix of coefficient and pval 
by each share.


What am I doing wrong here?

Any pointers most welcome.

Many thanks in advance!

Regards,

Bernd





__
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] problem with looping on sqlSave()

2010-02-25 Thread Ivan Calandra

Hi!

I've tried it, but the problem is that each element has a different 
size, with makes rbind() useless.
Is there then a function/package that allows appending data in an Excel 
sheet?
I've searched already for a few packages, but none (except, in theory, 
RODBC) can append.


Any suggestion?
Regards,
Ivan

Le 2/24/2010 19:38, Dieter Menne a écrit :


Ivan Calandra wrote:
   

...
for (i in 1:4){
sqlSave(xlsFile, as.data.frame(test[[i]]), tablename=test, rownames=F,
addPK=T, append=T)
}
Error in odbcUpdate(channel, query, mydata, coldata[m, ], test = test,
:  missing columns in 'data'
odbcCloseAll()


 

I have never managed to write to Excel with ODBC with append=TRUE. If you
do not want to Access instead where this works, best is to collect (rbind)
the data in an R data frame, and write everything in one chunk.

Dieter



   


--
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Institut und Museum
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calan...@uni-hamburg.de

**
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php

__
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] problem with looping on sqlSave()

2010-02-25 Thread Dieter Menne


Ivan Calandra wrote:
 
 
 I've tried it, but the problem is that each element has a different 
 size, with makes rbind() useless.
 

Assuming that size means different number of columns, then appending is
not valid, because it would change the structure of the table, and with ODBC
we are in the relational database world. In that case, you should assemble
your data frame with all possible columns and leave the fields empty.

If size means different number of rows, where append DOES make sense, 
rbind is useful.

Dieter


-- 
View this message in context: 
http://n4.nabble.com/problem-with-looping-on-sqlSave-tp1567601p1568715.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.


Re: [R] problem with looping on sqlSave()

2010-02-25 Thread Ivan Calandra

What if size means both columns and rows?

Here is one of such lists:
test - structure(list(m = structure(c(0.090909090909091, 
0.181818181818182,

0.272727272727273, 0.363636363636364, 0.454545454545455, 0.545454545454545,
0.636363636363636, 0.727272727272727, 0.818181818181818, 0.909090909090909,
1, NA, NA, NA, NA, NA, NA, -14.571209, -13.829402, -22.1283685,
-22.717841, -34.8053215, 12.988246, 10.2528335, 9.47791, 24.585605,
NA, NA, NA, NA, NA, NA, NA), .Dim = c(11L, 3L), .Dimnames = list(
NULL, c(qhat, lower, upper))), crit = 0.611274541966673,
numsig = 0L, pc = NA), .Names = c(m, crit, numsig,
pc))

I've managed to do what I want using write.csv() like:
for (i in 1:4){
 write.csv(test[[i]], file=test.csv, append=T, quote=F, row.names=F)
}

I would like to export directly into xls for 2 reasons:
- I won't have to save each file again in xls
- I can take advantage of the use of several sheets, which would reduce 
the number of files.


As I said in my earlier emails, what I want to export are test outputs 
from functions, so I cannot change it; I just have to deal with it.


Thanks again
Ivan


Le 2/25/2010 10:24, Dieter Menne a écrit :


Ivan Calandra wrote:
   


I've tried it, but the problem is that each element has a different
size, with makes rbind() useless.

 

Assuming that size means different number of columns, then appending is
not valid, because it would change the structure of the table, and with ODBC
we are in the relational database world. In that case, you should assemble
your data frame with all possible columns and leave the fields empty.

If size means different number of rows, where append DOES make sense,
rbind is useful.

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.


Re: [R] problem with looping on sqlSave()

2010-02-25 Thread Dieter Menne


Ivan Calandra wrote:
 
 What if size means both columns and rows?
 
 
Then you are not longer in the database world, and should use one of the
half dozen other methods to write to Excel, either native, via Perl
(portable) or RCOM. Search R-search for write Excel.

Dieter


-- 
View this message in context: 
http://n4.nabble.com/problem-with-looping-on-sqlSave-tp1567601p1568745.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.


Re: [R] problem with looping on sqlSave()

2010-02-25 Thread Ivan Calandra
As I said earlier, none of the packages I have found (dataframes2xls, 
WriteXLS, xlsReadWrite, xlsx) to export to xls allow appending on the 
same sheet.

So at the end, write.csv() is more flexible for my use.

Do you know if the RExcel add-on would be useful in my case? I took a 
look at the manual, but it doesn't really describe well the possibilites.


Thank you anyway
Ivan

Le 2/25/2010 10:53, Dieter Menne a écrit :


Ivan Calandra wrote:
   

What if size means both columns and rows?


 

Then you are not longer in the database world, and should use one of the
half dozen other methods to write to Excel, either native, via Perl
(portable) or RCOM. Search R-search for write Excel.

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.


Re: [R] problem with looping on sqlSave()

2010-02-25 Thread Dieter Menne


Ivan Calandra wrote:
 
 As I said earlier, none of the packages I have found (dataframes2xls, 
 WriteXLS, xlsReadWrite, xlsx) to export to xls allow appending on the 
 same sheet.
 

Please check again. WriteXLS has a parameter where to start writing.

D

-- 
View this message in context: 
http://n4.nabble.com/problem-with-looping-on-sqlSave-tp1567601p1568831.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.


Re: [R] problem with looping on sqlSave()

2010-02-25 Thread Ivan Calandra
I've checked in the package WriteXLS and couldn't find such parameter (I 
hope I didn't overlook anything!).

However write.xls() in xlsReadWrite package has such parameter.
I've tried it and the problem is that, even though I can make the 
writing start at the last line, it will overwrite the whole file. Even 
if I try to append on a new sheet, the xls file is overwritten.

One by one, I'm crossing off the possibilities...
Thanks!
Ivan

Le 2/25/2010 11:58, Dieter Menne a écrit :


Ivan Calandra wrote:
   

As I said earlier, none of the packages I have found (dataframes2xls,
WriteXLS, xlsReadWrite, xlsx) to export to xls allow appending on the
same sheet.

 

Please check again. WriteXLS has a parameter where to start writing.

D




__
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] problem with looping on sqlSave()

2010-02-25 Thread Marc Schwartz
Just to confirm, WriteXLS() does not have such a parameter, but write.xls() in 
the xlsReadWRite package does. As you have noted however, none of these 
approaches are really designed to enable flexible appending to an existing 
Excel file.

As I believe Dieter noted in an earlier post, your best option may be to look 
at the RCOM/RExcel based solution, which gives you more flexibility in writing 
to Excel files. More info at http://rcom.univie.ac.at/.

HTH,

Marc Schwartz

On Feb 25, 2010, at 8:02 AM, Ivan Calandra wrote:

 I've checked in the package WriteXLS and couldn't find such parameter (I hope 
 I didn't overlook anything!).
 However write.xls() in xlsReadWrite package has such parameter.
 I've tried it and the problem is that, even though I can make the writing 
 start at the last line, it will overwrite the whole file. Even if I try to 
 append on a new sheet, the xls file is overwritten.
 One by one, I'm crossing off the possibilities...
 Thanks!
 Ivan
 
 Le 2/25/2010 11:58, Dieter Menne a écrit :
 
 Ivan Calandra wrote:
   
 As I said earlier, none of the packages I have found (dataframes2xls,
 WriteXLS, xlsReadWrite, xlsx) to export to xls allow appending on the
 same sheet.
 
 
 Please check again. WriteXLS has a parameter where to start writing.
 
 D

__
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] problem with looping on sqlSave()

2010-02-24 Thread Ivan Calandra

Dear R users,

I have a follow-up question on sqlSave().
Since most of the output from the tests I use are lists, I would like to 
loop to export each element of the list and append it to the sheet.


Here is what I do:
 library(RODBC)
 test - structure(list(m = structure(c(0.090909090909091, 
0.181818181818182,

0.272727272727273, 0.363636363636364, 0.454545454545455, 0.545454545454545,
0.636363636363636, 0.727272727272727, 0.818181818181818, 0.909090909090909,
1, NA, NA, NA, NA, NA, NA, -14.571209, -13.829402, -22.1283685,
-22.717841, -34.8053215, 12.988246, 10.2528335, 9.47791, 24.585605,
NA, NA, NA, NA, NA, NA, NA), .Dim = c(11L, 3L), .Dimnames = list(
NULL, c(qhat, lower, upper))), crit = 0.611274541966673,
numsig = 0L, pc = NA), .Names = c(m, crit, numsig,
pc))
 xlsFile - odbcConnectExcel(file.xls, readOnly=F)
 for (i in 1:4){
sqlSave(xlsFile, as.data.frame(test[[i]]), tablename=test, rownames=F, 
addPK=T, append=T)

}
Error in odbcUpdate(channel, query, mydata, coldata[m, ], test = test,  
:  missing columns in 'data'

 odbcCloseAll()

I've tried different combination and the 1st element is always exported 
but the next ones never. Elements 2 and 3 have the same number of 
columns; the problem persists in that case.


Thanks a lot for your help
Ivan

__
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] problem with looping on sqlSave()

2010-02-24 Thread Dieter Menne


Ivan Calandra wrote:
 
 ...
   for (i in 1:4){
 sqlSave(xlsFile, as.data.frame(test[[i]]), tablename=test, rownames=F, 
 addPK=T, append=T)
 }
 Error in odbcUpdate(channel, query, mydata, coldata[m, ], test = test,  
 :  missing columns in 'data'
   odbcCloseAll()
 
 

I have never managed to write to Excel with ODBC with append=TRUE. If you
do not want to Access instead where this works, best is to collect (rbind)
the data in an R data frame, and write everything in one chunk.

Dieter



-- 
View this message in context: 
http://n4.nabble.com/problem-with-looping-on-sqlSave-tp1567601p1567872.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] :Problem with Looping

2009-11-17 Thread Bruno Giovannetti

Hello,

Sometimes the looping (using for) seems to skip some iterations.

An example:

arg - matrix(NA,length(seq(.30,.5,.01)),1)
for (i in seq(.30,.5,.01)) {
arg[i*100-29] - i
}
arg

What is the problem with this coding, please?

Thank you so much,
--
Bruno Cara Giovannetti, PhD Candidate
Economics Department
Columbia University
Personal Webpage: www.columbia.edu/~bcg2108

__
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] :Problem with Looping

2009-11-17 Thread Benilton Carvalho

it doesn't skip...

think about why:

seq(.30,.5,.01) * 100 - 29 == 1:21

isn't always TRUE.

b

On Nov 17, 2009, at 9:17 PM, Bruno Giovannetti wrote:


Hello,

Sometimes the looping (using for) seems to skip some iterations.

An example:

arg - matrix(NA,length(seq(.30,.5,.01)),1)
for (i in seq(.30,.5,.01)) {
arg[i*100-29] - i
}
arg

What is the problem with this coding, please?

Thank you so much,
--
Bruno Cara Giovannetti, PhD Candidate
Economics Department
Columbia University
Personal Webpage: www.columbia.edu/~bcg2108

__
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] :Problem with Looping

2009-11-17 Thread Nordlund, Dan (DSHS/RDA)
 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On
 Behalf Of Bruno Giovannetti
 Sent: Tuesday, November 17, 2009 3:18 PM
 To: r-help@r-project.org
 Subject: [R] :Problem with Looping
 
 Hello,
 
 Sometimes the looping (using for) seems to skip some iterations.
 
 An example:
 
 arg - matrix(NA,length(seq(.30,.5,.01)),1)
 for (i in seq(.30,.5,.01)) {
 arg[i*100-29] - i
 }
 arg
 
 What is the problem with this coding, please?
 
 Thank you so much,
 --
 Bruno Cara Giovannetti, PhD Candidate
 Economics Department
 Columbia University
 Personal Webpage: www.columbia.edu/~bcg2108

Don't use floating point operations in your indexing.  You may also want to 
change how you generate your sequence.  This is a floating point representation 
problem. See FAQ 7.31.  Try something like this

arg - matrix(NA,length(seq(30,50,1)),1)
for (i in seq(30,50,1)) {arg[i-29] - i/100 }

Hope this is helpful,

Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA  98504-5204

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