Re: [R] [FORGED] Re: rmgarch: source package installation problem

2020-01-02 Thread Pietro Coretto

Dear All

I could solve the problem: I removed my .Rprofile and I could install 
the package via the usual


install.packages("rmgarch")

My .Rprofile contains few settings, therefore, it was not too difficult 
to understand what was conflicting with the installation.  The issue in 
.Rprofile was the following


q <-  function (save="no", ...) {
   quit(save=save, ...)
}


Regards
Pietro Coretto





On 02/01/2020 23.54, Rolf Turner wrote:


On 2/01/20 9:51 pm, Pietro Coretto wrote:



No problem Rolf. Thanks for you interest. But the problem is still 
unsolved!


I experimented and found that I too could not install rmgarch.  However 
the string of error messages that I got was quite different from yours.


I did some scrounging around and after a bit of trial and error found 
that I needed to do:


sudo apt-get install libgmp3-dev
sudo apt-get install libmpfr-dev

Then the R command

    install.packages("rmgarch",lib="~/Rlib")

worked.  (Note that "~/Rlib" is where I keep my "contributed" packages.)

This is under Ubuntu 18.04.  I don't know if this will work for you 
since, as I said, the error messages that I initially got were different 
from those that you got.


*Don't* ask me about what to do under Mac OSX or (God save us!) under 
Windoze!!! :-)


cheers,

Rolf

P.S. I have taken the liberty of CC-ing this to the r-help list in case 
it is of interest to others or in case others may have useful 
contributions to add.


R.



__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] rmgarch: source package installation problem

2019-12-30 Thread Pietro Coretto

On 30/12/2019 12.11, Duncan Murdoch wrote:

On 30/12/2019 5:59 a.m., Pietro Coretto wrote:

[...]

You didn't show us the command you used to install it.

Duncan Murdoch



Sorry for this,


I used the following:

install.packages("rmgarch")

from the linux command line, using both a regular user account and root. 
Same issue anyway


Thanks
Pietro

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] rmgarch: source package installation problem

2019-12-30 Thread Pietro Coretto




Dear Users

I tried to install the package "rmgarch". While installing binaries for 
Windows and MacOS didn't cause any issue. The source package 
installation doesn't work.


I tried it on three different OS: Linux Ubuntu 18.04 (64bit), Windows 10 
Pro (64bit) and MacOS Catalina. In all three cases I have worked with a 
fresh R 3.6.2 install. I contacted the maintainer of the package who 
said that it can't reproduce the same issue and that CRAN checks are fine.


Below I report the console output when installing "rmgarch" on my Ubuntu 
18.04 LTS (64bit). I skip the messages regarding installation of the 
required packages (they all installed without issues):



https://pastebin.com/J6XHaP28

I have installed many other packages from source (unsing C, C++, RCpp, 
fortran code, etc) without any issue.



Here the output of  sessionInfo() at the end.

https://pastebin.com/z7ZwU9iR


I hope somebody can tell me what may cause this.

Regards
Pietro

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] No transactions/positions to chart in Quantstrat

2018-07-14 Thread Pietro Fabbro via R-help
Hello.
I get the error message that there are no transactions/positions to chart 
despite the signals and rules that I inserted.
Can someone please help?

rm(list = ls(.blotter), envir = .blotter)
initdate <- "2010-01-01"
from <- "2012-01-01" #start of backtest
to <- "2017-31-12" #end of backtest

Sys.setenv(TZ= "EST") #Set up environment for timestamps

currency("USD") #Set up environment for currency to be used

symbols <- c("RUT") #symbols used in our backtest
getSymbols("^RUT",src="yahoo", from="2012-01-01", to="2017-12-31", 
periodicity="daily")

stock(symbols, currency = "USD", multiplier = 1) #tells quanstrat what 
instruments present and what currency to use

n <- 30

wma <-  WMA(Cl(RUT), n=4, wts=c(1:4))
wmamaxt <-  rollmaxr(wma, n, fill = NA)
wmamint <- - rollmaxr(- wma, n, fill = NA)
CNOwma <- function (RUT) {(wma - wmamint) / (wmamaxt - wmamint)}

tradesize <-1 #default trade size
initeq <- 10 #default initial equity in our portfolio

strategy.st <- portfolio.st <- account.st <- "firststrat" #naming strategy, 
portfolio and account

#removes old portfolio and strategy from environment
rm.strat(portfolio.st)
rm.strat(strategy.st) 

#initialize portfolio, account, orders and strategy objects
initPortf(portfolio.st, symbols = symbols, initDate = initdate, currency = 
"USD")

initAcct(account.st, portfolios = portfolio.st, initDate = initdate, currency = 
"USD", initEq = initeq)

initOrders(portfolio.st, initDate = initdate)
strategy(strategy.st, store=TRUE)

add.indicator(strategy = strategy.st,
name = 'CNOwma',
arguments = list(x = quote(Cl(mktdata)), n=4),
label = 'CNOwma4')





add.signal(strategy.st, name = "sigThreshold",
arguments = list(column = "CNOwma4", threshold = 0.6,
relationship = "gt", cross = TRUE),
label = "longthreshold")


add.signal(strategy.st, name = "sigThreshold",
arguments = list(column = "CNOwma4", threshold = 0.6,
relationship = "lt", cross = TRUE),
label = "shortthreshold")




add.rule(strategy.st, name = "ruleSignal",
arguments = list(sigcol = "longthreshold", sigval = TRUE,
orderqty = "all", ordertype = "market",
orderside = "long", replace = FALSE,
prefer = "Open"), osFUN = IKTrading::osMaxDollar,
tradeSize = tradesize, maxSize = tradesize, type = "enter")


add.rule(strategy.st, name = "ruleSignal",
arguments = list(sigcol = "shortthreshold", sigval = TRUE,
orderqty = "all", ordertype = "market",
orderside = "long", replace = FALSE,
prefer = "Open"),
type = "exit")

add.rule(strategy.st, name = "ruleSignal",
arguments = list(sigcol = "shortthreshold", sigval = TRUE,
orderqty = "all", ordertype = "market",
orderside = "short", replace = FALSE,
prefer = "Open"),osFUN = IKTrading::osMaxDollar, 
tradeSize = tradesize, maxSize = tradesize, type = "enter")

add.rule(strategy.st, name = "ruleSignal",
arguments = list(sigcol = "longthreshold", sigval = TRUE,
orderqty = "all", ordertype = "market",
orderside = "short", replace = FALSE,
prefer = "Open"),
type = "exit")



out <- applyStrategy(strategy = strategy.st, portfolios = portfolio.st)
updatePortf(portfolio.st)
daterange <- time(getPortfolio(portfolio.st)$summary)[-1]

updateAcct(account.st, daterange)
updateEndEq(account.st)


for(symbol in symbols){

chart.Posn(Portfolio = portfolio.st, Symbol = symbol, 
TA= c("add_SMA(n=50, col='blue')", "add_SMA(n=200, col='red')"))
}

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] Quantstrat custom indicator colnames error

2018-07-14 Thread Pietro Fabbro via R-help
I apologize if the data I will insert will not be enough.

So, I am trying to run a strategy through the package Quantstrat.

install.packages("quantstrat")


My problem is that I get the following error
Error incolnames<-(tmp, value = seq(ncol(tmp_val))) : 
attempt to set 'colnames' on an object with less than two dimensions

when I try to run the following command:

> out <- applyStrategy(strategy=strategy.st,portfolios=portfolio.st)
I do not have this problem if I use, as indicator, one or more indicators, 
which are already defined by the package TTR.

I have this error only when I try to use a custom indicator. Here is the code 
for the custom indicator that I use:

wma <-  WMA(Cl(mktdata), 4, wts=c(1:4)) 
wmamaxt <- rollmaxr(wma, 30, fill = NA)
wmamint <- - rollmaxr(- wma, 30, fill = NA)
CNOwma <- function (mktdata=quote(mktdata),x) {(wma - wmamint) / (wmamaxt - 
wmamint)}
Please refer to the following code:

library(devtools)
library(quantmod)
library(quantstrat)
library(TTR)
library(png)
library(IKTrading)

wma <-  WMA(Cl(mktdata), 4, wts=c(1:4)) 
wmamaxt <- rollmaxr(wma, 30, fill = NA)
wmamint <- - rollmaxr(- wma, 30, fill = NA)
CNOwma <- function (mktdata=quote(mktdata),x) {(wma - wmamint) / (wmamaxt - 
wmamint)}
initdate <- "2010-01-01"
from <- "2012-01-01" #start of backtest
to <- "2017-31-12" #end of backtest

Sys.setenv(TZ= "EST") #Set up environment for timestamps

currency("USD") #Set up environment for currency to be used

symbols <- c("RUT", "IXIC") #symbols used in our backtest
getSymbols(Symbols = symbols, src = "google", from=from, to=to, adjust = TRUE) 
#receive data from google finance,  adjusted for splits/dividends

stock(symbols, currency = "USD", multiplier = 1) #tells quanstrat what 
instruments present and what currency to use

tradesize <-1 #default trade size
initeq <- 10 #default initial equity in our portfolio

strategy.st <- portfolio.st <- account.st <- "firststrat" #naming strategy, 
portfolio and account

#removes old portfolio and strategy from environment
rm.strat(portfolio.st)
rm.strat(strategy.st) 

#initialize portfolio, account, orders and strategy objects
initPortf(portfolio.st, symbols = symbols, initDate = initdate, currency = 
"USD")

initAcct(account.st, portfolios = portfolio.st, initDate = initdate, currency = 
"USD", initEq = initeq)

initOrders(portfolio.st, initDate = initdate)
strategy(strategy.st, store=TRUE)

add.indicator(strategy = strategy.st,
name = 'CNOwma',
arguments = list(x = quote(Cl(mktdata)), n=4),
label = 'CNOwma4')





add.signal(strategy.st, name = "sigThreshold",
arguments = list(column = "CNOwma4", threshold = 0.6,
relationship = "gt", cross = TRUE),
label = "longthreshold")


add.signal(strategy.st, name = "sigThreshold",
arguments = list(column = "CNOwma4", threshold = 0.6,
relationship = "lt", cross = TRUE),
label = "shortthreshold")




add.rule(strategy.st, name = "ruleSignal",
arguments = list(sigcol = "longthreshold", sigval = TRUE,
orderqty = "all", ordertype = "market",
orderside = "long", replace = FALSE,
prefer = "Open"),
type = "enter")


add.rule(strategy.st, name = "ruleSignal",
arguments = list(sigcol = "shortthreshold", sigval = TRUE,
orderqty = "all", ordertype = "market",
orderside = "long", replace = FALSE,
prefer = "Open"),
type = "exit")

add.rule(strategy.st, name = "ruleSignal",
arguments = list(sigcol = "shortthreshold", sigval = TRUE,
orderqty = "all", ordertype = "market",
orderside = "short", replace = FALSE,
prefer = "Open"),
type = "enter")

add.rule(strategy.st, name = "ruleSignal",
arguments = list(sigcol = "longthreshold", sigval = TRUE,
orderqty = "all", ordertype = "market",
orderside = "short", replace = FALSE,
prefer = "Open"),
type = "exit")



out <- applyStrategy(strategy = strategy.st, portfolios = portfolio.st)


When I run the traceback() of the error, this is what I get:
> traceback()
4: stop("attempt to set 'colnames' on an object with less than two dimensions")
3: `colnames<-`(`*tmp*`, value = seq(ncol(tmp_val)))
2: applyIndicators(strategy = strategy, mktdata = mktdata, parameters = 
parameters, 
...)
1: applyStrategy(strategy = strategy.st, portfolios = portfolio.st

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] Error custom indicator Quantstrat colnames

2018-07-14 Thread Pietro Fabbro via R-help
I will try to be as clear as possible as I have been rebuked by some users. I 
deleted the last questions and I will try to be sufficiently explicative in 
this one. I apologize if the data I will insert will not be enough.

So, I am trying to run a strategy through the package Quantstrat.

install.packages("quantstrat")
My problem is that I get the following error

Error incolnames<-(tmp, value = seq(ncol(tmp_val))) : 
attempt to set 'colnames' on an object with less than two dimensions

when I try to run the following command:

> out <- applyStrategy(strategy=strategy.st,portfolios=portfolio.st)
I do not have this problem if I use, as indicator, one or more indicators, 
which are already defined by the package TTR.

I have this error only when I try to use a custom indicator. Here is the code 
for the custom indicator that I use:

wma <-  WMA(Cl(mktdata), 4, wts=c(1:4)) 
wmamaxt <- rollmaxr(wma, 30, fill = NA)
wmamint <- - rollmaxr(- wma, 30, fill = NA)
CNOwma <- function (mktdata=quote(mktdata),x) {(wma - wmamint) / (wmamaxt - 
wmamint)}
Please refer to the following code:

library(devtools)
library(quantmod)
library(quantstrat)
library(TTR)
library(png)
library(IKTrading)

wma <-  WMA(Cl(mktdata), 4, wts=c(1:4)) 
wmamaxt <- rollmaxr(wma, 30, fill = NA)
wmamint <- - rollmaxr(- wma, 30, fill = NA)
CNOwma <- function (mktdata=quote(mktdata),x) {(wma - wmamint) / (wmamaxt - 
wmamint)}
initdate <- "2010-01-01"
from <- "2012-01-01" #start of backtest
to <- "2017-31-12" #end of backtest

Sys.setenv(TZ= "EST") #Set up environment for timestamps

currency("USD") #Set up environment for currency to be used

symbols <- c("RUT", "IXIC") #symbols used in our backtest
getSymbols(Symbols = symbols, src = "google", from=from, to=to, adjust = TRUE) 
#receive data from google finance,  adjusted for splits/dividends

stock(symbols, currency = "USD", multiplier = 1) #tells quanstrat what 
instruments present and what currency to use

tradesize <-1 #default trade size
initeq <- 10 #default initial equity in our portfolio

strategy.st <- portfolio.st <- account.st <- "firststrat" #naming strategy, 
portfolio and account

#removes old portfolio and strategy from environment
rm.strat(portfolio.st)
rm.strat(strategy.st) 

#initialize portfolio, account, orders and strategy objects
initPortf(portfolio.st, symbols = symbols, initDate = initdate, currency = 
"USD")

initAcct(account.st, portfolios = portfolio.st, initDate = initdate, currency = 
"USD", initEq = initeq)

initOrders(portfolio.st, initDate = initdate)
strategy(strategy.st, store=TRUE)

add.indicator(strategy = strategy.st,
name = 'CNOwma',
arguments = list(x = quote(Cl(mktdata)), n=4),
label = 'CNOwma4')





add.signal(strategy.st, name = "sigThreshold",
arguments = list(column = "CNOwma4", threshold = 0.6,
relationship = "gt", cross = TRUE),
label = "longthreshold")


add.signal(strategy.st, name = "sigThreshold",
arguments = list(column = "CNOwma4", threshold = 0.6,
relationship = "lt", cross = TRUE),
label = "shortthreshold")




add.rule(strategy.st, name = "ruleSignal",
arguments = list(sigcol = "longthreshold", sigval = TRUE,
orderqty = "all", ordertype = "market",
orderside = "long", replace = FALSE,
prefer = "Open"),
type = "enter")


add.rule(strategy.st, name = "ruleSignal",
arguments = list(sigcol = "shortthreshold", sigval = TRUE,
orderqty = "all", ordertype = "market",
orderside = "long", replace = FALSE,
prefer = "Open"),
type = "exit")

add.rule(strategy.st, name = "ruleSignal",
arguments = list(sigcol = "shortthreshold", sigval = TRUE,
orderqty = "all", ordertype = "market",
orderside = "short", replace = FALSE,
prefer = "Open"),
type = "enter")

add.rule(strategy.st, name = "ruleSignal",
arguments = list(sigcol = "longthreshold", sigval = TRUE,
orderqty = "all", ordertype = "market",
orderside = "short", replace = FALSE,
prefer = "Open"),
type = "exit")



out <- applyStrategy(strategy = strategy.st, portfolios = portfolio.st)
When I run the traceback() of the error, this is what I get:

> traceback()
4: stop("attempt to set 'colnames' on an object with less than two dimensions")
3: `colnames<-`(`*tmp*`, value = seq(ncol(tmp_val)))
2: applyIndicators(strategy = strategy, mktdata = mktdata, parameters = 
parameters, 
...)
1: applyStrategy(strategy = strategy.st, portfolios = portfolio.st)

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] SARIMA in rpy2

2015-10-22 Thread pietro chen
Hello,

Am trying to estimate a seasonal Arima by calling the R forecast package in
Rpy2:

fit = forecast.Arima(x = h02, order = order, seasonal = seasonal)

Strangely I get the estimates of the non-seasonal part, only, even if the
model is specified as (3,0,1)x(0,1,2). Can anyone tell me where the issue
is?

Regards,

P.

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] help with xts

2014-05-19 Thread Pietro

Thanks to all for your ideas and inspirations

Il 18/05/2014 18.35, Joshua Ulrich ha scritto:

Using subset assignment with an array usually doesn't work well with
xts/zoo objects.  Your case wouldn't even work with a matrix because
you have NA in your array.

In this case, you can achieve the same result using multiplication:
pos.neg.1 - ((cond1  cond2)*-2+1)
test - test * pos.neg.1

Best,
--
Joshua Ulrich  |  about.me/joshuaulrich
FOSS Trading  |  www.fosstrading.com


On Sun, May 18, 2014 at 9:25 AM, Pete freeri...@gmail.com wrote:

I have 3 xts objects: test, cond1, cond2
You can download here:

https://dl.dropboxusercontent.com/u/102669/obj.rar

My problem is very simple.

test [ cond1  cond2] = NA   THIS WORKS

test [ cond1  cond2] = -test [ cond1  cond2]   THIS DOESN'T WORKS

Why?

My objective is to substitute all values in test (when cond1  cond2) with
the corresponding values of test but with negative sign

__
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] Help with multiple use of quantile

2013-06-08 Thread Parodi, Pietro
Hello

I have a matrix aaa like this:

   aa1   aa2   aa3
[1,]  8371.417  27613.57  1170.466
[2,] 14317.999  42421.82  3423.934
[3,] 22026.466  59874.14  8103.084
[4,] 33884.986  84506.34 19176.764
[5,] 57954.968 129824.30 56097.310

where each column represents an empirical distribution of random variates 
(normally the number of rows would be, say, 10,000 or 100,000) and a matrix bbb 
of percentiles like this:

bbb

   [,1]  [,2]  [,3]
[1,] 0.70092980 0.8144194 0.6200732
[2,] 0.77968803 0.5804948 0.5463661
[3,] 0.01509415 0.9313509 0.8611973
[4,] 0.22654757 0.6183386 0.4962867
[5,] 0.36548835 0.6608696 0.3062784
What I'd like to do is to apply the quantiles in the three columns of bbb to 
the columns of aaa independently, so as to obtain a matrix ccc such that, for 
example, ccc[3,1]=quantile(aaa[,1],bbb[3,1]). THe complete matrix is:


ccc =quantile(aaa[,1],bbb[1,1]), quantile(aaa[,2],bbb[1,2]), 
quantile(aaa[,3],bbb[1,3])
quantile(aaa[,1],bbb[2,1]), quantile(aaa[,2],bbb[2,2]), 
quantile(aaa[,3],bbb[2,3])
quantile(aaa[,1],bbb[3,1]), quantile(aaa[,2],bbb[3,2]), 
quantile(aaa[,3],bbb[3,3])
quantile(aaa[,1],bbb[4,1]), quantile(aaa[,2],bbb[4,2]), 
quantile(aaa[,3],bbb[4,3])
quantile(aaa[,1],bbb[5,1]), quantile(aaa[,2],bbb[5,2]), 
quantile(aaa[,3],bbb[5,3])



Now if I only two vectors, it would be enough for me to define 
ccc=quantile(aaa,bbb). However, if I do this when aaa and bbb are matrix, 
quantile(aaa,bbb) does something else -- it gives the percentiles bbb of the 
whole set of matrix elements rather than considering separately the quantiles 
of the different columns.

I suspect I'll need to use apply in some form but have been able to come up 
with the correct form.

As you might have imagined, this arises in the  context of simulating random 
variables from different distribution in empirical form that are correlated 
through a copula.

Thanks in advance for your help!

Pietro
_

__

For information pertaining to Willis' email confidentiality and monitoring 
policy, usage restrictions, or for specific company registration and regulatory 
status information, please visit http://www.willis.com/email_trailer.aspx

We are now able to offer our clients an encrypted email capability for secure 
communication purposes. If you wish to take advantage of this service or learn 
more about it, please let me know or contact your Client Advocate for full 
details. ~W67897
__

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


Re: [R] data.frame with NA

2013-03-20 Thread Pietro

Thank you David and thank you Petr

At 14.18 19/03/2013, David L Carlson wrote:

Try this instead:

 Foglio1[,2:ncol(Foglio1)] - na.locf(Foglio1[,2:ncol(Foglio1)],fromLast=T)
 str(Foglio1)
'data.frame':   1489 obs. of  15 variables:
 $ Date: Date, format: 2001-08-17 2001-08-20 ...
 $ a   : num  202 201 202 201 202 ...
 $ b   : num  231 230 230 230 232 ...
 $ c   : num  177 179 181 180 182 ...
 $ d   : num  277 277 276 276 275 ...
 $ e   : num  2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 ...
 $ f   : num  275 277 279 279 279 ...
 $ g   : num  91.7 90.7 90.8 91.1 91 ...
 $ h   : num  11446 11258 11280 11396 11127 ...
 $ i   : num  388 389 393 392 393 ...
 $ l   : num  93.2 94 92.4 93.4 93.1 ...
 $ m   : num  128 127 126 129 130 ...
 $ n   : num  103 103 103 103 103 ...
 $ o   : num  133 133 133 133 133 ...
 $ p   : num  107 107 107 107 107 ...

It appears that na.locf() converts the object to a matrix at some point (but
I haven't checked the source code). The first column (the Date variable) is
treated as character. As a result, everything gets converted to character.
This will skip the first column which does not have any missing values.

--
David L Carlson
Associate Professor of Anthropology
Texas AM University
College Station, TX 77843-4352


 -Original Message-
 From: Pietro [mailto:freeri...@gmail.com]
 Sent: Tuesday, March 19, 2013 6:10 AM
 To: dcarl...@tamu.edu; dcarl...@tamu.edu
 Cc: r-h...@stat.math.ethz.ch
 Subject: RE: [R] data.frame with NA

 Yes, colClasses is the solution. Thank you very much.
 However i found a very strange thing.

 If i use:
 Foglio1 - read.xlsx2(mydb.xlsx, 1, colClasses=c(Date,
 rep(numeric,14)))

 i get numeric dataframe, as you said.

 I also get NaN (and not NA).

 At this point i use the function:
 Foglio1 = na.locf(Foglio1,fromLast=T) and it works perfectly. All NaN
 's were replaced with the first numeric value, as expected.

 And now the enigma.

 After na.locf function, Foglio1 become all CHR again! It seems that
 na.locf  convert from num to chr. Even Date is converted in chr.
 I'm reading the help of this function but i can't find trace about
 the possibility of this conversion.

 It seems that i can't get in anyway a numeric dataframe without NA o
 NaN!
 Ok, i admit that i'm a newbie, but i'm trying every day to gain
 confidence with R

 Can i ask you the courtesy to use na.locf function to see if also on
 your computer this function convert all to CHR?

 Thank you



 At 21.37 18/03/2013, David L Carlson wrote:
 It appears that you MUST use the colClasses= argument with read.xlsx2:
 
 Foglio1 - read.xlsx2(mydb.xlsx, 1, colClasses=c(Date,
 rep(numeric,
 14)))
 
 However, e and n are converted to NaN not NA so you would need to
 convert
 those columns (at least, I didn't check for missing values in the
 other
 columns):
 
   Foglio1$e - ifelse(is.nan(Foglio1$e), NA, Foglio1$e)
   Foglio1$n - ifelse(is.nan(Foglio1$n), NA, Foglio1$n)
   str(Foglio1)
 'data.frame':   1489 obs. of  15 variables:
   $ Date: Date, format: 2001-08-17 2001-08-20 ...
   $ a   : num  202 201 202 201 202 ...
   $ b   : num  231 230 230 230 232 ...
   $ c   : num  177 179 181 180 182 ...
   $ d   : num  277 277 276 276 275 ...
   $ e   : num  NA NA NA NA NA NA NA NA NA NA ...
   $ f   : num  275 277 279 279 279 ...
   $ g   : num  91.7 90.7 90.8 91.1 91 ...
   $ h   : num  11446 11258 11280 11396 11127 ...
   $ i   : num  388 389 393 392 393 ...
   $ l   : num  93.2 94 92.4 93.4 93.1 ...
   $ m   : num  128 127 126 129 130 ...
   $ n   : num  NA NA NA NA NA NA NA NA NA NA ...
   $ o   : num  133 133 133 133 133 ...
   $ p   : num  107 107 107 107 107 ...
 
 ---
 David
 
 
   -Original Message-
   From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
   project.org] On Behalf Of David L Carlson
   Sent: Monday, March 18, 2013 3:22 PM
   To: 'Pietro'; 'Berend Hasselman'
   Cc: r-h...@stat.math.ethz.ch
   Subject: Re: [R] data.frame with NA
  
   Try this
  
   Open the spreadsheet in Excel. Select all of the data click Copy.
 Don't
   close Excel.
  
   Open R and type the following command:
  
Foglio1 - read.table(clipboard-128, header=TRUE, sep=\t)
  
   Now take a look at the structure of the data.frame
  
str(Foglio1)
   'data.frame':   1489 obs. of  15 variables:
$ Date: Factor w/ 1489 levels 1/10/2002,1/10/2003,..: 1275
 1291
   1295
   1299 1304 1309 1321 1325 1329 1337 ...
$ a   : num  202 201 202 201 202 ...
$ b   : num  231 230 230 230 232 ...
$ c   : num  177 179 181 180 182 ...
$ d   : num  277 277 276 276 275 ...
$ e   : num  NA NA NA NA NA NA NA NA NA NA ...
$ f   : num  275 277 279 279 279 ...
$ g   : num  91.7 90.7 90.8 91.1 91 ...
$ h   : num  11446 11258 11280 11396 11127 ...
$ i   : num  388 389 393 392 393 ...
$ l   : num  93.2 94 92.4 93.4 93.1 ...
$ m   : num  128 127 126 129 130 ...
$ n   : num  NA NA NA NA NA NA NA NA NA NA ...
$ o

Re: [R] data.frame with NA

2013-03-19 Thread Pietro

Yes, colClasses is the solution. Thank you very much.
However i found a very strange thing.

If i use:
Foglio1 - read.xlsx2(mydb.xlsx, 1, colClasses=c(Date, rep(numeric,14)))

i get numeric dataframe, as you said.

I also get NaN (and not NA).

At this point i use the function:
Foglio1 = na.locf(Foglio1,fromLast=T) and it works perfectly. All NaN 
's were replaced with the first numeric value, as expected.


And now the enigma.

After na.locf function, Foglio1 become all CHR again! It seems that 
na.locf  convert from num to chr. Even Date is converted in chr.
I'm reading the help of this function but i can't find trace about 
the possibility of this conversion.


It seems that i can't get in anyway a numeric dataframe without NA o NaN!
Ok, i admit that i'm a newbie, but i'm trying every day to gain 
confidence with R


Can i ask you the courtesy to use na.locf function to see if also on 
your computer this function convert all to CHR?


Thank you



At 21.37 18/03/2013, David L Carlson wrote:

It appears that you MUST use the colClasses= argument with read.xlsx2:

Foglio1 - read.xlsx2(mydb.xlsx, 1, colClasses=c(Date, rep(numeric,
14)))

However, e and n are converted to NaN not NA so you would need to convert
those columns (at least, I didn't check for missing values in the other
columns):

 Foglio1$e - ifelse(is.nan(Foglio1$e), NA, Foglio1$e)
 Foglio1$n - ifelse(is.nan(Foglio1$n), NA, Foglio1$n)
 str(Foglio1)
'data.frame':   1489 obs. of  15 variables:
 $ Date: Date, format: 2001-08-17 2001-08-20 ...
 $ a   : num  202 201 202 201 202 ...
 $ b   : num  231 230 230 230 232 ...
 $ c   : num  177 179 181 180 182 ...
 $ d   : num  277 277 276 276 275 ...
 $ e   : num  NA NA NA NA NA NA NA NA NA NA ...
 $ f   : num  275 277 279 279 279 ...
 $ g   : num  91.7 90.7 90.8 91.1 91 ...
 $ h   : num  11446 11258 11280 11396 11127 ...
 $ i   : num  388 389 393 392 393 ...
 $ l   : num  93.2 94 92.4 93.4 93.1 ...
 $ m   : num  128 127 126 129 130 ...
 $ n   : num  NA NA NA NA NA NA NA NA NA NA ...
 $ o   : num  133 133 133 133 133 ...
 $ p   : num  107 107 107 107 107 ...

---
David


 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
 project.org] On Behalf Of David L Carlson
 Sent: Monday, March 18, 2013 3:22 PM
 To: 'Pietro'; 'Berend Hasselman'
 Cc: r-h...@stat.math.ethz.ch
 Subject: Re: [R] data.frame with NA

 Try this

 Open the spreadsheet in Excel. Select all of the data click Copy. Don't
 close Excel.

 Open R and type the following command:

  Foglio1 - read.table(clipboard-128, header=TRUE, sep=\t)

 Now take a look at the structure of the data.frame

  str(Foglio1)
 'data.frame':   1489 obs. of  15 variables:
  $ Date: Factor w/ 1489 levels 1/10/2002,1/10/2003,..: 1275 1291
 1295
 1299 1304 1309 1321 1325 1329 1337 ...
  $ a   : num  202 201 202 201 202 ...
  $ b   : num  231 230 230 230 232 ...
  $ c   : num  177 179 181 180 182 ...
  $ d   : num  277 277 276 276 275 ...
  $ e   : num  NA NA NA NA NA NA NA NA NA NA ...
  $ f   : num  275 277 279 279 279 ...
  $ g   : num  91.7 90.7 90.8 91.1 91 ...
  $ h   : num  11446 11258 11280 11396 11127 ...
  $ i   : num  388 389 393 392 393 ...
  $ l   : num  93.2 94 92.4 93.4 93.1 ...
  $ m   : num  128 127 126 129 130 ...
  $ n   : num  NA NA NA NA NA NA NA NA NA NA ...
  $ o   : num  133 133 133 133 133 ...
  $ p   : num  107 107 107 107 107 ...

 --
 David L Carlson
 Associate Professor of Anthropology
 Texas AM University
 College Station, TX 77843-4352

  -Original Message-
  From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
  project.org] On Behalf Of Pietro
  Sent: Monday, March 18, 2013 1:57 PM
  To: Berend Hasselman
  Cc: r-h...@stat.math.ethz.ch
  Subject: Re: [R] data.frame with NA
 
  Yes, it's true Berend!
 
  What i do is simply use read.xlsx  function
 
  db - read.xlsx2(c:/mydb.xlsx,1,as.data.frame=T)
 
  This is excel file i use:
  http://dl.dropbox.com/u/102669/mydb.xlsx
 
  I can't find  a way to import as numeric.
  My objective is to be able to work (in R) with my NA's
 
 
  At 18.46 18/03/2013, Berend Hasselman wrote:
 
  On 18-03-2013, at 16:49, Pete freeri...@gmail.com wrote:
  
   
I have this little data.frame
   
http://dl.dropbox.com/u/102669/nanotna.rdata
   
Two column contains NA, so the best thing to do is use na.locf
   function (with
fromLast = T)
   
But locf function doesn't work because NA in my data.frame are
   not recognized as
real NA.
   
Is there a way to substitute fake NA with real NA? In this case
   na.locf function
should work
   
  
  Your data are all characters. Do
  
  str(db)
  
  to see that. What is probably supposed to be numeric is also
  character,
  Somehow you have managed to read in data that R thinks is all chr.
  Your NA are NA in reality: a character string NA.
  
  You will have to review the method you used to get the data into R.
  And make sure that what you

Re: [R] data.frame with NA

2013-03-18 Thread Pietro

Yes, it's true Berend!

What i do is simply use read.xlsx  function

db - read.xlsx2(c:/mydb.xlsx,1,as.data.frame=T)

This is excel file i use:
http://dl.dropbox.com/u/102669/mydb.xlsx

I can't find  a way to import as numeric.
My objective is to be able to work (in R) with my NA's


At 18.46 18/03/2013, Berend Hasselman wrote:


On 18-03-2013, at 16:49, Pete freeri...@gmail.com wrote:


 I have this little data.frame

 http://dl.dropbox.com/u/102669/nanotna.rdata

 Two column contains NA, so the best thing to do is use na.locf 
function (with

 fromLast = T)

 But locf function doesn't work because NA in my data.frame are 
not recognized as

 real NA.

 Is there a way to substitute fake NA with real NA? In this case 
na.locf function

 should work


Your data are all characters. Do

str(db)

to see that. What is probably supposed to be numeric is also character,
Somehow you have managed to read in data that R thinks is all chr.
Your NA are NA in reality: a character string NA.

You will have to review the method you used to get the data into R.
And make sure that what you want to be numeric is indeed numeric.
Then you can start to think about doing something about the NA's.

Berend


__
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] Counting number of common elements between the rows of two different matrices

2011-11-04 Thread Parodi, Pietro
 
Hello

I'm trying to solve this problem without using a for loop but I have so
far failed to find a solution.

I have two matrices of K columns each, e.g. (K=5), and with numbers of
row N_A and N_B respectively

A = (1 5 3 8 15;
 2 7 20 11 13;
 12 19 20 21 43)

B = (2 6 30 8 16;
 3 8 19 11 13)

(the actual matrices have hundreds of thousands of entry, that's why I'm
keen to avoid for loops)

And what I need to do is to apply a function which counts the number of
common elements between ANY row of A and ANY row of B, giving a result
like this:


A1 vs B1:  1  # (8 is a common element)
A1 vs B2:  1  # (8 is a common element)
A2 vs B1:  1  # (2 is a common element)
A2 vs B2:  1  # 11, 13 are common elements
Etc.

I've built a function that counts the number of common elements between
two vectors, based on the intersect function in the R manual

common_elements - function(x,y) length(y[match(x,y,nomatch=0)])

And a double loop who solves my problem would be something like
(pseudo-code)

For(i in 1:N_A){
for(j in 1:N_B){
ce(i,j)=common_elements(a(i),b(j))
}
} 

Is there an efficient, clean way to do the same job and give as an
output a matrix N_A x N_B such as that above?

Thanks a lot for your help

Regards

Pietro

__

For information pertaining to Willis' email confidentiality and monitoring 
policy, usage restrictions, or for specific company registration and regulatory 
status information, please visit http://www.willis.com/email_trailer.aspx

We are now able to offer our clients an encrypted email capability for secure 
communication purposes. If you wish to take advantage of this service or learn 
more about it, please let me know or contact your Client Advocate for full 
details. ~W67897

__
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] Counting number of common elements between the rows of two different matrices

2011-11-04 Thread Parodi, Pietro
Jim

I tried that and it works. Thank you very much for your help!

Regards

Pietro


-Original Message-
From: jim holtman [mailto:jholt...@gmail.com] 
Sent: 04 November 2011 13:38
To: Parodi, Pietro
Cc: r-help@r-project.org
Subject: Re: [R] Counting number of common elements between the rows of two 
different matrices

Try this:

# create dummy data
a - matrix(sample(20, 50, TRUE), ncol = 5)
b - matrix(sample(20, 50, TRUE), ncol = 5)
# create combinations to test
x - expand.grid(seq(nrow(a)), seq(nrow(b)))
# test
result - mapply(function(m1, m2) any(a[m1, ] %in% b[m2, ])
, x[, 1]
, x[, 2]
)
# create the output matrix
result.m - matrix(result, nrow = nrow(a), ncol = nrow(b))

On Fri, Nov 4, 2011 at 8:51 AM, Parodi, Pietro pietro.par...@willis.com wrote:

 Hello

 I'm trying to solve this problem without using a for loop but I have so
 far failed to find a solution.

 I have two matrices of K columns each, e.g. (K=5), and with numbers of
 row N_A and N_B respectively

 A =     (1 5 3 8 15;
         2 7 20 11 13;
         12 19 20 21 43)

 B =     (2 6 30 8 16;
         3 8 19 11 13)

 (the actual matrices have hundreds of thousands of entry, that's why I'm
 keen to avoid for loops)

 And what I need to do is to apply a function which counts the number of
 common elements between ANY row of A and ANY row of B, giving a result
 like this:


 A1 vs B1:  1  # (8 is a common element)
 A1 vs B2:  1  # (8 is a common element)
 A2 vs B1:  1  # (2 is a common element)
 A2 vs B2:  1  # 11, 13 are common elements
 Etc.

 I've built a function that counts the number of common elements between
 two vectors, based on the intersect function in the R manual

 common_elements - function(x,y) length(y[match(x,y,nomatch=0)])

 And a double loop who solves my problem would be something like
 (pseudo-code)

 For(i in 1:N_A){
        for(j in 1:N_B){
                ce(i,j)=common_elements(a(i),b(j))
                }
        }

 Is there an efficient, clean way to do the same job and give as an
 output a matrix N_A x N_B such as that above?

 Thanks a lot for your help

 Regards

 Pietro

 __

 For information pertaining to Willis' email confidentiality and monitoring 
 policy, usage restrictions, or for specific company registration and 
 regulatory status information, please visit 
 http://www.willis.com/email_trailer.aspx

 We are now able to offer our clients an encrypted email capability for secure 
 communication purposes. If you wish to take advantage of this service or 
 learn more about it, please let me know or contact your Client Advocate for 
 full details. ~W67897

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




-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

__
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] Converting from density to cumulative distribution

2011-04-21 Thread Parodi, Pietro
 
Hello
 
I'm trying to do the following vector operation:
 
given vector x = c(x1,x2,x3,x4...xn),  produce vector y =
c(x1,x1+x2,x1+x2+x3,...x1+...+xn). 
 
E.g.,  from x = c(1,3,2,2,5), produce y = c(1,4,6,8,13).
 
The underlying problem is finding the cumulative distribution function
given the empirical density distribution function.
 
I have done some research on this but the only relevant thing I've found
is the function integrate, which however gives me the sum of the whole
vector not the step-wise sum.
 
Thanks for your help
 
Pietro
 

__

For information pertaining to Willis' email confidentiality and monitoring 
policy, usage restrictions, or for specific company registration and regulatory 
status information, please visit http://www.willis.com/email_trailer.aspx

We are now able to offer our clients an encrypted email capability for secure 
communication purposes. If you wish to take advantage of this service or learn 
more about it, please let me know or contact your Client Advocate for full 
details. ~W67897
__

[[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] rcorrp.cens and U statistics

2009-09-08 Thread pietro bulian
I have two alternative Cox models with C-statistics 0.72 and 0.78. My question 
is if 0.78 is significantly greater than 0.72. I'm using rcorrp.cens. I cannot 
find the U statistics in the output of the function. This is the output of the 
help example:

 x1 - rnorm(400)
 x2 - x1 + rnorm(400)
 d.time - rexp(400) + (x1 - min(x1))
 cens   - runif(400,.5,2)
 death  - d.time = cens
 d.time - pmin(d.time, cens)
 rcorrp.cens(x1, x2, Surv(d.time, death))
   Dxy   S.D. x1 more concordant x2 more concordant 
 nmissing uncensored Relevant Pairs 
 -8.902077e-03   2.649712e-01   4.955490e-01   5.044510e-01 
  4.00e+02   0.00e+00   4.00e+00   1.348000e+03 
 Uncertain   C X1   C X2 Dxy X1 
Dxy X2 
  1.582520e+05   9.955490e-01   9.762611e-01   9.910979e-01 
  9.525223e-01 

Should I manually calculate the U statistics interpreting the fraction x1 more 
concordant and x2 more concordant of the Relevant Pairs as Uxy and Uyx? 

many thanks for any help or suggestion

Pietro Bulian




__
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] pivot table in R

2008-01-29 Thread pietro . parodi
Hello,

I'm struggling with an elementary problem with R. I have a simple data 
frame such as this one giving the number of accidents subdivided by sex, 
age and region.

sex age region  no_of_accidents

F   young   north   10
F   young   south   12
F   old north   5
F   old south   7
M   young   north   24
M   young   south   30
M   old north   12
M   old south   17

and I would like to build a pivot table, e.g. obtaining the sum of the 
number of accidents for each sex:

sex age region  no_of_accidents

F   (any)   (any)   34
M   (any)   (any)   83

but I can't seem to obtain this result simply in R, except by embarking in 
complicated for loops.

I have checked the documentation on such functions as table() and the 
documentation on An introduction to R but couldn't solve the problem. 
Could you please help me with this?

Cheers

Pete


PLEASE CONSIDER THE ENVIRONMENT BEFORE PRINTING THIS E-MAIL

For Aon’s standard conditions associated with this e-mail please visit 
http://www.aon.com/uk/en/email-footer/aon-limited.jsp
Aon Limited
Registered Office: 8 Devonshire Square, London EC2M 4PL
Registered in London No. 210725 . VAT Registration No. 480 8401 48

Aon Limited is authorised and regulated by the Financial Services Authority in 
respect of insurance mediation activities only.

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