[R] For loop question

2012-11-10 Thread dae
I have this code:
IEF <- to.monthly(IEF, indexAt="endof")
SPY <- to.monthly(SPY, indexAt="endof")

I would like to use a for loop instead of separate entries,
so the only code that needs to be modified is the list
of symbols.

symbols <- c("IEF", "SPY")
for(symbol in symbols) {
  symbol <- to.monthly(symbol, indexAt="endof")
}
This for loop doesn't work. It puts each output into  
*symbol *not into *IEF *and *SPY*.
How do I put the output into the existing objects using
a for loop?

Note: to.monthly() is an xts function



--
View this message in context: 
http://r.789695.n4.nabble.com/For-loop-question-tp4649215.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] For loop question

2012-11-10 Thread dae
Thanks.  That got me the answer.  This works:

symbols = c("IEF","SPY")

getSymbols(symbols)

for(symbol in symbols) { 
assign(symbol, to.monthly(get(symbol), indexAt="endof")) 
   } 
 
#end



--
View this message in context: 
http://r.789695.n4.nabble.com/For-loop-question-tp4649215p4649227.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] Lasso for k-subset regression

2011-06-05 Thread Dae-Jin Lee
Dear R-users

I'm trying to use lasso in lars package for subset regression,  I have a
large matrix of size 1000x100 and my aim is to select a subset k of the 100
variables.

Is there any way in lars to fix the number k (i.e. to select the best 10
variables)

library(lars)

aa=lars(X,Y,type="lasso",max.steps=200)

plot(aa,plottype="Cp")
aa$RSS
which.min(aa$RSS)
round(aa$beta,2)

aa$beta[which.min(aa$RSS),]#  find which coefficients minimizes the RSS

lasso.ind=which((as.vector((aa$beta[which.min(aa$RSS),])))>0)# index of
variables

print(lasso.ind)   # this usually gives more than 10 variables (also depends
on the max.steps in lars)


Thanks in advance

[[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] optim with simulated annealing SANN for combinatorial optimization

2011-12-15 Thread Dae-Jin Lee
Hi all

I am trying to solve a combinatorial optimization problem. Basically, I can
reduce my problem into the next problem:

1.- Given a NxN grid of points, with some values in each cell
2.- Find the combination of K points on the grid such that, the maximum
mean value is obtained


 I took the Travel SalesMan problem example in ?optim documentation. I am
not sure if I have understood correctly the SANN implementation in optim,
as I do not see how the acceptance probability comes out. And it looks like
I am only evaluating the criteria several times and keep the maximum at the
end.

Thanks in advance


Here is some example code in R

### toy example
N=5
K=6

new.points = expand.grid(1:N,1:N)  # grid points

set.seed(1234)

resp=rnorm(N^2)  # random values on each grid cell

###  function to generate the sequence of candidates
generate<-function(ind){

idx <- seq(1, nrow(new.points), by=1)   # index of 1 to N^2 grid cells
swap.points <- c(sample(ind,1),sample(idx[-c(ind)], size=1))  # swap
between points of the initial set and other candidate
ind[which(ind==swap.points[1])]<-idx[which(idx==swap.points[2])]

cat("set  =",ind,"\n")
cat("crit =",media(ind),"\n")
cat("swap =",swap.points,"\n")

plot(new.points[,1:2],col='black',xlim=c(range(new.points[,1])),ylim=c(range(new.points[,2])))
points(new.points[ind,1:2],col='blue',pch=19,xlim=c(range(new.points[,1])),ylim=c(range(new.points[,2])))
return(as.numeric(ind))
}

###  function to calculate the mean value of the points on the grid
media=function(sq){
med=mean(resp[sq])
return(as.numeric(med))
}

###  initial set of K candidates
init=sample(1:K,K)

###  run SANN
res <- optim(init, media ,generate, method="SANN",control =
list(maxit=2, temp=100,tmax=1000, trace=TRUE, REPORT=1, fnscale=-1))
new.points[sort(res$par),]
plot(new.points,cex=.1)
points(new.points[res$par,],col=3,lwd=3,cex=1.5)

[[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] Conditional Autoregressive (CAR) model simulation

2008-02-15 Thread Dae-Jin Lee
Hi all !

I would like to simulate spatial lattice/areal data with a conditional
autoregressive (CAR) structure, for a given neighbouring matrix and for a
autocorrelation "rho".

Is there any package or function in R to perform it ?

I found the function "CARsimu" in the hdeco library, but this is not what
I'm looking for

Thanks in advance

Dae-Jin

-- 
______

Dae-Jin Lee

Office/Despacho: 7.3.J04
Phone/Tlfno:+34 91 624 9175
Fax: +34 91 624 9430

Departamento de Estadística
Av. Universidad 30, Ed. Juan Benet
28911 Leganés (Madrid), SPAIN
Universidad Carlos III de Madrid

e-mail: [EMAIL PROTECTED]
   web: http://www.est.uc3m.es/daejin
__

[[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] Conditional Autoregressive (CAR) model simulation

2008-02-19 Thread Dae-Jin Lee
Thanks for your suggestion, Henrique

Here I attach a link to some notes by Robert Bivand, with a SAR model, see
page 30

www.bias-project.org.uk/ASDARcourse/unit6_slides.pdf

The difference with CAR is in the covariance structure, I run and example:

# ===
library(spdep)
set.seed(987654)

# Columbus example in spdep library
example(columbus)
n <- length(col.gal.nb)
coords <- coordinates(columbus)
cards <- card(col.gal.nb)
col.w <- nb2listw(col.gal.nb)

uncorr_x <- rnorm(n)# uncorrelated random variable

cor(uncorr_x, lag(col.w,uncorr_x)) # correlation

# ==
 rho <- .5 # spatial autocorrelation

# Case1: SAR

 IrW <- diag(n) - rho*listw2mat(col.w) # I - rho * W

 SARcov <- solve(t(IrW) %*% IrW)
 SARcovL <- chol((SARcov + t(SARcov))/2)

autocorr_x <- t(SARcovL)%*%uncorr_x

cor(autocorr_x, lag(col.w,autocorr_x))   #
   [,1]
[1,] 0.3876456
# ==

I've did the same but with CAR structure

   CARcov = solve(IrW) #  from  (I-rhoW)^ -1
   CARcovL = chol(CARcov)   # Cholesky Decomposition

a_x = t(CARcovL)%*%uncorr_x #

cor(a_x,lag(col.W,a_x))

   [,1]
[1,] 0.06929324
# ==

I don't know if I'm simulating correctly the CAR,

any help?

Thanks in advance



2008/2/15, Henrique Dallazuanna <[EMAIL PROTECTED]>:
>
> See spautolm function in spdep package
>
> On 15/02/2008, Dae-Jin Lee <[EMAIL PROTECTED]> wrote:
> > Hi all !
> >
> >  I would like to simulate spatial lattice/areal data with a conditional
> >  autoregressive (CAR) structure, for a given neighbouring matrix and for
> a
> >  autocorrelation "rho".
> >
> >  Is there any package or function in R to perform it ?
> >
> >  I found the function "CARsimu" in the hdeco library, but this is not
> what
> >  I'm looking for
> >
> >  Thanks in advance
> >
> >  Dae-Jin
> >
> >  --
> >  __
> >
> >  Dae-Jin Lee
> >
> >  Office/Despacho: 7.3.J04
> >  Phone/Tlfno:+34 91 624 9175
> >  Fax: +34 91 624 9430
> >
> >  Departamento de Estadística
> >  Av. Universidad 30, Ed. Juan Benet
> >  28911 Leganés (Madrid), SPAIN
> >  Universidad Carlos III de Madrid
> >
> >  e-mail: [EMAIL PROTECTED]
> >web: http://www.est.uc3m.es/daejin
> >  __
> >
> > [[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.
> >
> >
>
>
> --
> Henrique Dallazuanna
> Curitiba-Paraná-Brasil
> 25° 25' 40" S 49° 16' 22" O
>



-- 
__

Dae-Jin Lee

Office/Despacho: 7.3.J04
Phone/Tlfno:+34 91 624 9175
Fax: +34 91 624 9430

Departamento de Estadística
Av. Universidad 30, Ed. Juan Benet
28911 Leganés (Madrid), SPAIN
Universidad Carlos III de Madrid

e-mail: [EMAIL PROTECTED]
   web: http://www.est.uc3m.es/daejin
__

[[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] mcv package gamm function Error in chol(XVX + S)

2007-10-02 Thread Dae-Jin Lee
Hi all R users !

I'm using gamm function from Simon Wood's mgcv package, to fit a spatial
regression Generalized Additive Mixed Model, as covariates I have the
geographical longitude and latitude locations of indexed data. I include a
random effect for each district (dist) so the code is

fit <- gamm(y~s(lon,lat,bs="tp", m=2)+offset(log(exp.)),
random=list(dist=~1), family="poisson", niterPQL=30)

when I run the gamm function I obtain the next error message:

%%%
 Maximum number of PQL iterations:  30
iteration 1
iteration 2
...
iteration 8
iteration 9
iteration 10
Error in chol(XVX + S) : the leading minor of order 29 is not positive
definite
%%%

Could be any problem in gamm() ???

--

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