Re: [R] creating dummy variables based on conditions

2013-07-14 Thread Anup Nandialath
hi Arun,

Thanks for this. This solution works great.

Knid Regards

Anup


On Sun, Jul 14, 2013 at 8:07 PM, arun  wrote:

> Hi,
> You could try this: (if I understand it correctly)
> dat1<- read.table(text="
> yearid var ans
>  2010  1  1  1
>  2010  2  0  0
>  2010  1  0  1
> 2010  1  0  1
>  2011  2  1  1
>  2011  2  0  1
>  2011  1  0  0
> 2011  1  0  0
> ",sep="",header=TRUE,stringsAsFactors=FALSE)
>
> dat1$newres<-with(dat1,ave(var,id,year,FUN=function(x) any(x==1)*1))
>  dat1
> #  year id var ans newres
> #1 2010  1   1   1  1
> #2 2010  2   0   0  0
> #3 2010  1   0   1  1
> #4 2010  1   0   1  1
> #5 2011  2   1   1  1
> #6 2011  2   0   1  1
> #7 2011  1   0   0      0
> #8 2011  1   0   0  0
>
> A.K.
>
> - Original Message -
> From: Anup Nandialath 
> To: r-help@r-project.org
> Cc:
> Sent: Sunday, July 14, 2013 7:30 AM
> Subject: [R] creating dummy variables based on conditions
>
> Hello everyone,
>
> I have a dataset which includes the first three variables from the demo
> data below (year, id and var). I need to create the new variable ans as
> follows
>
> If var=1, then for each year (where var=1), i need to create a new dummy
> ans which takes the value of 1 for all corresponding id's where an instance
> of one was recorded. Sample data with the output is shown below.
>
> yearid var ans
> [1,] 2010  1   1   1
> [2,] 2010  2   0   0
> [3,] 2010  1   0   1
> [4,] 2010  1   0   1
> [5,] 2011  2   1   1
> [6,] 2011  2   0   1
> [7,] 2011  1   0   0
> [8,] 2011  1   0   0
>
> Any help on how to achieve this is much appreciated.
>
> Thanks
> Anup
>
> [[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.
>
>

[[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] creating dummy variables based on conditions

2013-07-14 Thread Anup Nandialath
Hello everyone,

I have a dataset which includes the first three variables from the demo
data below (year, id and var). I need to create the new variable ans as
follows

If var=1, then for each year (where var=1), i need to create a new dummy
ans which takes the value of 1 for all corresponding id's where an instance
of one was recorded. Sample data with the output is shown below.

yearid var ans
[1,] 2010  1   1   1
[2,] 2010  2   0   0
[3,] 2010  1   0   1
[4,] 2010  1   0   1
[5,] 2011  2   1   1
[6,] 2011  2   0   1
[7,] 2011  1   0   0
[8,] 2011  1   0   0

Any help on how to achieve this is much appreciated.

Thanks
Anup

[[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] Constraints when sampling from a distribution

2010-12-08 Thread Anup Nandialath
 Dear R-helpers,

My question is related to how to impose constraints when when sampling from a 
distribution.

For example, suppose I'm sampling a vector from a multivariate normal 
distribution

vbeta <- 100*diag(2)
mbeta <- c(1,1)
ans <- beta <- c(rmvnorm(1,mbeta,vbeta))

ans will thus be a vector with two elements. 

My question is how do I place a restriction on one of the elements of the ans 
vector. For example, suppose my goal is to reject any draw for ans[2] below -1 
and above 1, how should I implement that in the above setting?

Thanks in advance for your help.

Kind Regards

Anup



  
[[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] Help on loops

2010-12-08 Thread Anup Nandialath
Thanks Jim! 

--- On Tue, 12/7/10, jim holtman  wrote:

From: jim holtman 
Subject: Re: [R] Help on loops
To: "Anup Nandialath" 
Cc: r-help@r-project.org
Date: Tuesday, December 7, 2010, 7:47 PM

use split and lapply to make it easier.  Substitute in your own
calculations since this is just an example:

> x
  id t X1 X2
1  1 1  4  3
2  1 2  9  2
3  1 3  7  3
4  1 4  6  6
5  2 1  6  4
6  2 2  5  3
7  2 3  1  1
8  3 1  9  6
9  3 2  5  5
> # first split the data by 'id'
> x.s <- split(x, x$id)
> # then process each group
> result <- lapply(x.s, function(.grp){
+     output <- .grp[1, 3:4] * 4  # data from the first row
+     i <- 2
+     # repeat for the rest of the row
+     while (i <= nrow(.grp)){
+         output <- output + .grp[i, 3:4] * 2
+         i <- i + 1
+     }
+     output  # return value
+ })
> result
$`1`
  X1 X2
1 60 34

$`2`
  X1 X2
5 36 24

$`3`
  X1 X2
8 46 34

>


On Tue, Dec 7, 2010 at 5:43 PM, Anup Nandialath
 wrote:
> Dear R-helpers,
>
> I have a basic question on using loops.
>
> I have a panel data set with different variables measured for "n" firms over 
> "t" time periods. A snapshot of the data is given below
>
> id    t    X1    X2
> 1    1     4    3
> 1    2    9    2
> 1    3    7    3
> 1    4    6    6
> 2    1    6    4
> 2    2    5    3
> 2    3    1    1
> 3    1    9    6
> 3    2    5    5
>
> thus total sample n=9
>
> My problem is as follows. I need to do some computations on the data where 
> the first observation for each firm (ie. id=1 and t=1; id=2 and t=1; id=3 and 
> t=1) requires a specific operation on x1 and x2 and the subsequent operations 
> are based on the computed value of the first operation.
>
> For example the pseudocode is as follows
>
> ##define output matrix
>
> output <- rep(0,n)
>
> ## define coefficient vector
> b <- c(1,1)
>
> for (i in 1:number of simulations)
> {
>    for (j in 1:id)
>     {
>   for(k in 1:t)
>  {
>    if(Data$t[,2]==1)
>   {
>  meanvec <- Data[k,3:4]%*%b
>  output[k] = calc (meanvec) # output from calc is a scalar
>   }
>     else
>   output[k]= calc(a*output[k-1]+Data[k,3:4]%*%b)
>    }
>    }
>     }
>
> Thus the end result should be a vector "output" with nine observations based 
> on the computations.
>
> I hope the problem is clear and I greatly appreciate any help in solving this 
> problem . Thanks in advance for your help.
>
> Kind Regards
>
> Anup
>
>
>
>
>        [[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.
>
>



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?



  
[[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] Help on loops

2010-12-07 Thread Anup Nandialath
Dear R-helpers,

I have a basic question on using loops. 

I have a panel data set with different variables measured for "n" firms over 
"t" time periods. A snapshot of the data is given below

id    t    X1    X2
1    1     4    3
1    2    9    2
1    3    7    3
1    4    6    6
2    1    6    4
2    2    5    3
2    3    1    1
3    1    9    6
3    2    5    5

thus total sample n=9

My problem is as follows. I need to do some computations on the data where the 
first observation for each firm (ie. id=1 and t=1; id=2 and t=1; id=3 and t=1) 
requires a specific operation on x1 and x2 and the subsequent operations are 
based on the computed value of the first operation. 

For example the pseudocode is as follows

##define output matrix

output <- rep(0,n)

## define coefficient vector
b <- c(1,1)

for (i in 1:number of simulations)
{
   for (j in 1:id)
    {
  for(k in 1:t)
 {
   if(Data$t[,2]==1)
  {
 meanvec <- Data[k,3:4]%*%b
 output[k] = calc (meanvec) # output from calc is a scalar
  }
    else
  output[k]= calc(a*output[k-1]+Data[k,3:4]%*%b) 
   }
   }
    }

Thus the end result should be a vector "output" with nine observations based on 
the computations. 

I hope the problem is clear and I greatly appreciate any help in solving this 
problem . Thanks in advance for your help. 

Kind Regards

Anup



  
[[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] Metropolis acceptance rates

2008-04-08 Thread Anup Nandialath
Hi Michael,

Actually I was wrong in the initial mail. rbprobitGibbs uses the Gibbs Sampler. 
I think the rhierbinLogit does the Logit model which uses a Metropolis hastings 
approach uses the acceptance rate and the function outputs the rejection rate 
in the output list. So you are not required to change the source code.

HTH 

ANup

Michael Margolis <[EMAIL PROTECTED]> wrote: Is there a way to recover 
Metropolis-step acceptance rates AFTER  
completing posterior draws?

The immediate application is in the probit.bayes and logit.bayes  
models used by Zelig... which I believe is merely calling MCMCpack.  
So one strategy, to which I am fixing to resort, is to call, say,  
MCMClogit with verbose set to mcmc (or mcmc divided by an integer)   
and then look at my screen. But it seems odd that this datum is not  
being put into output objects, so I wonder if I'm missing something.

Plus sometimes I can save the output object, but then the power goes  
out and what's on screen is gone forever.

Thanks,
Michael



Michael Margolis
Profesor Investigador
Escuela de Economía
Universidad de Guanajuato
UCEA-Campus Marfil, Fracc 1
El Establo, Guanajuato, Gto. CP 36250
Mexico
(52)(473) 735 29 25 Ext. 2858





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


   
-
[[elided Yahoo spam]]
[[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] Metropolis acceptance rates

2008-04-08 Thread Anup Nandialath
Hi Michael,
 
 If you are using the standard logit or probit model, it is fairly easy to save 
the acceptance rate after each draw. I would recommend using the "bayesm" 
package as the source code is easy to manipulate. For instance in the probit 
function (rbprobitGibbs),  you need to include a couple of lines of code to 
save the acceptance rate. I'm sure it is possible with the other packages as 
well. 

HTH

Anup 

Michael Margolis <[EMAIL PROTECTED]> wrote: Is there a way to recover 
Metropolis-step acceptance rates AFTER  
completing posterior draws?

The immediate application is in the probit.bayes and logit.bayes  
models used by Zelig... which I believe is merely calling MCMCpack.  
So one strategy, to which I am fixing to resort, is to call, say,  
MCMClogit with verbose set to mcmc (or mcmc divided by an integer)   
and then look at my screen. But it seems odd that this datum is not  
being put into output objects, so I wonder if I'm missing something.

Plus sometimes I can save the output object, but then the power goes  
out and what's on screen is gone forever.

Thanks,
Michael



Michael Margolis
Profesor Investigador
Escuela de Economía
Universidad de Guanajuato
UCEA-Campus Marfil, Fracc 1
El Establo, Guanajuato, Gto. CP 36250
Mexico
(52)(473) 735 29 25 Ext. 2858





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


   
-
[[elided Yahoo spam]]
[[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] Rejection sampling to draw from distributions

2008-03-14 Thread Anup Nandialath
Dear friends,

Please find below the code that I have employed for a rejection sampler to draw 
from asymmetric laplace distributions. I was wondering if this code can be 
written more efficiently? Are there more efficient ways of drawing random 
numbers from asymmetric laplace distributions??

Thanks in advance for your help and have a great weekend.

Regards

Anup

***
ral <- function(n,p,s=3)
{
rout <- matrix(0,n)
for(i in 1:n)
{
  repeat
{
  root <- rnorm(1)
  ratio <- dal(val=root, p=p)/(s*dnorm(root))
  alpha <- runif(1, min=0, max=1)
  if(alphahttps://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] Bootstrapping

2007-12-31 Thread Anup Nandialath
Dear friends,

I'm interested in obtaining bootstrapped standard errors for a model that I'm 
estimating. I do realize that i can use the sample command and do the bootstrap 
by hand. But I was hoping somebody can help me on how to use the "boot" 
package. 

The model is as follows

# Likelihood function
log.like <- function (beta, data1, data2)
{


}

## Gradient 
grad.like <- function(beta, data1, data2)
{
.
.
}

# Dataset
data1 <- cbind(.)
data2 <- cbind()
startval <- rep(0,ncol(data1)+ncol(data2))


# Optimization run

estimate <- est.out <- optim(startval, fn=log.like, gr=grad.like, 
method="BFGS", data1=data1, data2=data2, hessian=T, control=c(fnscale=-1))

params <- estimate$par


Thanks in advance and wishing everybody a happy new year.

Regards

Anup

   
-

[[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] Is there a simpler way to do this?

2007-11-20 Thread Anup Nandialath
Dear Friends,

My objective is to do element wise multiplication of two vectors. For example 
suppose I have 

a <- (1,1,1)
b <- (2,4)

My output should be (2,4,2,4,2,4). I managed to write it down with loops as 
follows

r <- c(1,1,1)
l <- c(2,4)
x <- 1
for (j in 1:3)
{
  for (i in 1:2)
{
  new[x,] <- r[j]*l[i]
  x <- x+1
}
}


Is there a simpler solution to this without using the loops?


Thanks and Regards

Anup

   
-
Be a better sports nut! Let your teams follow you with Yahoo Mobile. Try it now.
[[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.