Displaying 20 results from an estimated 10000 matches similar to: "Help with avoiding For Loop"
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)
}
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 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
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 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 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 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
2007 Mar 12
2
Query about substituting characters in a df
Hi
I have a data frame with 40,000 rows and 4 columns,
one of which is "class".
For each row, the "class" column can be one of 10
possible NUMERIC values.
I wish to substitute these numeric values with
words/characters.
For example, I wish to substitute all occurences of
"5467" in the column "class" with "alpha", "7867" with
2010 Dec 07
1
Help on loops
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
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 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 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 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 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
2010 Dec 09
1
Constraints when sampling from a distribution
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
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 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
2007 May 29
1
Help with optim
Dear Friends,
I'm using the optim command to maximize a likelihood function. My optim command is as follows
estim.out <- optim(beta, loglike, X=Xmain, Y=Y, hessian=T, method="BFGS", control=c(fnscale=-1, trace=1, REPORT=1))
Setting the report=1, gives me the likelihood function value (if i'm correct) at each step. The output from running this is as follows
initial value