Displaying 20 results from an estimated 7000 matches similar to: "Help on loops"
2007 Apr 12
3
Random Sequence
Dear Friends,
I'm trying to generate a sequence of 100 observations
with either a 1 or -1. In other words the sequence
should look something like this.
y = 1 1 -1 1 -1 -1 -1 1 1 ......
Can somebody please give me some direction on how I
can do this in R.
Thanks
Anup
____________________________________________________________________________________
Don't get soaked. Take a
2007 Apr 18
2
Data Manipulation using R
Dear Friends,
I have data set with around 220,000 rows and 17 columns. One of the columns is an id variable which is grouped from 1000 through 9000. I need to perform the following operations.
1) Remove all the observations with id's between 6000 and 6999
I tried using this method.
remdat1 <- subset(data, ID<6000)
remdat2 <- subset(data, ID>=7000)
donedat <- rbind(remdat1,
2007 Jul 06
1
row index
Dear Friends,
Suppose I have a vector as follows
RI Value
1 10
2 11
3 8
4 4
6 12
I would like a function which returns the"row index number" for the minimum value of VALUE. Can somebody please give me some direction on how I can do this. In effect I'm trying to find a comparable function for the GAUSS command "minindc"
>From the GAUSS Manual
2007 Jul 24
1
Passing equations as arguments
Friends,
I'm trying to pass an equation as an argument to a function. The idea is as follows. Let us say i write an independent function
Ideal Situation:
ifunc <- function(x)
{
return((x*x)-2)
}
mainfunc <- function(a,b)
{
evala <- ifunc(a)
evalb <- ifunc(b)
if (evala>evalb){return(evala)}
else
return(evalb)
}
Now I want to try and write this entire program in a single
2007 Sep 08
1
Problem with the aggregate command
Dear friends,
I have a data set with 23 columns and 38000 rows. It is a panel running from the years 1991 through 2005. I want to aggregate the data and get the medians of each of the 23 columns for each of the years. In other words my output should be like this
Year Median
1991 123
1992 145
1993 132
etc.
The sample lines of code to do this operation is
set1 <-
2007 Aug 19
1
Creating a data set within a function
Dear Friends,
I'm trying to find if there is a way to automate creation of the design matrix. Suppose we are interested in say running an autoregressive model. The user inputs the following data
myfunAR <- function(y, order)
{.....
......
}
now here y is the data series and order represents the level of the process. In other words if order=2 then we have an AR (2) process. Now it is easy
2007 May 30
2
control axis
I have an outlier that I would still like to display, but would prefer to
shorten the axis. For example, display 0% - 40%, and 90% - 100%. Is this
possible? I am using an xyplot.
Thanks
Murray
--
Murray Pung
Statistician, Datapharm Australia Pty Ltd
0404 273 283
[[alternative HTML version deleted]]
2007 Jun 04
1
Help with conditional lagging of data
Dear Friends,
I have some data with three columns named ID, Year and Measure X. I need to create a column which gives me a lag for each ID (note not a continous lag), but a lag conditional on the id and the given year. Please find below a sample of the data
Input file sample
ID Year X
AB12 2000 100
AB12 2001 120
AB12 2002 140
AB12 2003 80
BL14 2000 180
BL14
2013 Jul 14
2
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.
year
2007 Feb 28
3
matrix manipulations
Dear friends,
I have a basic question with R. I'm generating a set
of random variables and then combining them using the
cbind statement. The code for that is given below.
for (i in 1:100)
{
y <- rpois(i,lambda=10)
X0 <- seq(1,1,length=i)
X1 <- rnorm(i,mean=5,sd=10)
X2 <- rnorm(i,mean=17,sd=12)
X3 <- rnorm(i,mean=3, sd=24)
ind <- rep(1:5,20)
}
2004 Nov 08
1
plotting lm coeficients with their means
I am trying to write a function that will run a linear
model and plot the regression coeficients with their
corresponding means. I am having two problems. I can
get the plot with the function below, but I am having
trouble labeling the points.
function(y,x1,x2,x3,x4){
outlm<-lm(y~x1+x2+x3+x4)
imp<-as.data.frame(outlm$coef[-1])
meanvec<-c(mean(x1),mean(x2),mean(x3),mean(x4))
2007 Feb 25
3
Random Integers
Hi all,
Is there an R function to generate random integers? Thanks in advance.
Sincerely
Anup
---------------------------------
Now that's room service! Choose from over 150,000 hotels
[[alternative HTML version deleted]]
2007 Jun 22
2
Data consistency checks in functions
Dear friends,
I'm writing a function with three arguments
myfun <- function(theta, X, values)
{
....
....
}
in this function, I'm trying to write consistency checks. In order to compute the statistic of interest I only need theta and values. The idea of having X in there is that, if values is not provided by the user, then values is computed from X.
my problem is I'm trying to
2013 Apr 04
5
Help for bootstrapping‏
I have a set of data for US t-bill returns and US stock returns frm 1980-2012. I am trying to bootstrap the data and obtain the minimum variance portfolio and repeat this portfolio 1000 times. However I am unable to get the correct code function for the minimum variance portfolio. When I tried to enter Opt(OriData+1, 1, 5, 0), I get "error:subscript out of bounds" Please help!
2008 Nov 21
1
Help with avoiding For Loop
Dear Friends,
I'm trying to see if there is some possibility that I can do the following
computations without a loop. I have attached a toy example below.
A <- c(1,2,3,4,5)
B <- rnorm(100)
store <- matrix(0,5,2)
for (i in 1:5)
{
store[i,1] <- mean(pnorm(A[i]*B))
store[i,2] <- var(pnorm(A[i]*B))
}
store
Thanks in advance for your help.
Kind Regards
Anup
[[alternative
2007 May 18
1
A programming question
Dear Friends,
My problem is related to how to measure probabilities from a probit model by changing one independent variable keeping the others constant.
A simple toy example is like this
Range for my variables is defined as follows
y=0 or 1, x1 = -10 to 10, x2=-40 to 100, x3 = -5 to 5
Model
output <- glim(y ~ x1+x2+x3 -1, family=binomial(link="probit"))
outcoef <-
2007 May 18
1
Bootstrapped standard errors
Dear Friends,
I'm trying to learn to how to get Bootstrapped standard errors for estimated coefficients from a regression.
For instance suppose I have the following model
logitmodel <- glm (y~X1+X2+X3, family=binomial(link="logit"))
beta <- logitmodel$coef
can somebody please guide me on how to use the package boot to obtain bootstrapped SE's for the associated betas.
2007 Aug 21
1
Random Sampling from a Matrix
Dear Friends,
I have a matrix of size 5000 X 20. The first two columns are indicator variables taking the value of either 0 or 1. Let us call the first two columns Y1 and Y2.
I need to randomly sample 1000 rows with all the associated columns, in other words my new matrix should be of size 1000 X 20. I realize that using this command
newmat <- mainmat[sample(1000,replace=F),]
achieves
2007 Nov 21
1
Is there a simpler way to do this?
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
2008 Mar 14
1
Rejection sampling to draw from distributions
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