Re: [R] Help writing basic loop

2011-09-20 Thread David L Carlson
You did ask for a loop, but if you are willing to consider sapply(), the
result you want can be obtained with a single (admittedly long) command:

 slope-sapply(1:100, function(x) lm(c(rnorm(1, mean=.01, sd=.001),
rnorm(1, mean=.1, sd=.01))~c(10,400))$coefficients[2])
 head(slope)
  c(10, 400)   c(10, 400)   c(10, 400)   c(10, 400)   c(10, 400)   c(10,
400)
0.0001994346 0.0002118383 0.0002729246 0.0002249865 0.0002323564
0.0002113820

If the c(10, 400) labels are distracting, you can replace them with
consecutive integers:

 names(slope)-1:100
 head(slope)
   12345
6
0.0001994346 0.0002118383 0.0002729246 0.0002249865 0.0002323564
0.0002113820

--
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-boun...@r-project.org] On
Behalf Of MacQueen, Don
Sent: Friday, September 16, 2011 6:58 PM
To: Luke Miller; beaulieu.j...@epamail.epa.gov
Cc: r-help@r-project.org
Subject: Re: [R] Help writing basic loop

Just a minor aside; I would have done

  my.slopes - numeric(100)

Note that:
 class(numeric(5))
[1] numeric

-Don


-- 
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062





On 9/16/11 12:37 PM, Luke Miller mille...@gmail.com wrote:

Create an output vector to hold your slopes before starting the loop, then
use your index i to place each new slope in the appropriate position in
your
vector.

y1-rnorm(100, mean=0.01, sd=0.001)
y2-rnorm(100, mean=0.1, sd=0.01)

x-(c(10,400))

my.slopes = vector(numeric,100)  #  initialize a numeric vector, length
100, filled with zeros initially

for (i in 1:100) {

#create the linear model for each data set
model1-lm(c(y1[i],y2[i])~x)
slope1-model1$coefficients[2]
my.slopes[i] = slope1 #  stick each new slope value into my.slopes[i]
}

You could skip the slope1 - model1$coefficients[2]  step and just put the
slope directly into my.slopes[i] as well.

On Fri, Sep 16, 2011 at 3:25 PM, beaulieu.j...@epamail.epa.gov wrote:

 Hello,

 I would like to write a loop to 1) run 100 linear regressions, and 2)
 compile the slopes of all regression into one vector.  Sample input data
 are:

 y1-rnorm(100, mean=0.01, sd=0.001)
 y2-rnorm(100, mean=0.1, sd=0.01)

 x-(c(10,400))

 #I have gotten this far with the loop

 for (i in 1:100) {

 #create the linear model for each data set
 model1-lm(c(y1[i],y2[i])~x)
 slope1-model1$coefficients[2]
 }

 How can I compile the slopes from all 100 regressions into one vector?

 Thanks,
 Jake
[[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.




-- 
___
Luke Miller
Postdoctoral Researcher
Marine Science Center
Northeastern University
Nahant, MA
(781) 581-7370 x318

   [[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@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 writing basic loop

2011-09-16 Thread Beaulieu . Jake
Hello,

I would like to write a loop to 1) run 100 linear regressions, and 2) 
compile the slopes of all regression into one vector.  Sample input data 
are:

y1-rnorm(100, mean=0.01, sd=0.001)
y2-rnorm(100, mean=0.1, sd=0.01)

x-(c(10,400))

#I have gotten this far with the loop

for (i in 1:100) {

#create the linear model for each data set
model1-lm(c(y1[i],y2[i])~x)
slope1-model1$coefficients[2]
}

How can I compile the slopes from all 100 regressions into one vector?

Thanks,
Jake
[[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 writing basic loop

2011-09-16 Thread Luke Miller
Create an output vector to hold your slopes before starting the loop, then
use your index i to place each new slope in the appropriate position in your
vector.

y1-rnorm(100, mean=0.01, sd=0.001)
y2-rnorm(100, mean=0.1, sd=0.01)

x-(c(10,400))

my.slopes = vector(numeric,100)  #  initialize a numeric vector, length
100, filled with zeros initially

for (i in 1:100) {

#create the linear model for each data set
model1-lm(c(y1[i],y2[i])~x)
slope1-model1$coefficients[2]
my.slopes[i] = slope1 #  stick each new slope value into my.slopes[i]
}

You could skip the slope1 - model1$coefficients[2]  step and just put the
slope directly into my.slopes[i] as well.

On Fri, Sep 16, 2011 at 3:25 PM, beaulieu.j...@epamail.epa.gov wrote:

 Hello,

 I would like to write a loop to 1) run 100 linear regressions, and 2)
 compile the slopes of all regression into one vector.  Sample input data
 are:

 y1-rnorm(100, mean=0.01, sd=0.001)
 y2-rnorm(100, mean=0.1, sd=0.01)

 x-(c(10,400))

 #I have gotten this far with the loop

 for (i in 1:100) {

 #create the linear model for each data set
 model1-lm(c(y1[i],y2[i])~x)
 slope1-model1$coefficients[2]
 }

 How can I compile the slopes from all 100 regressions into one vector?

 Thanks,
 Jake
[[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.




-- 
___
Luke Miller
Postdoctoral Researcher
Marine Science Center
Northeastern University
Nahant, MA
(781) 581-7370 x318

[[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 writing basic loop

2011-09-16 Thread R. Michael Weylandt
You can also do the regressions in parallel as follows:

x = -25:25
y = 0.05*x^2  + 2* x - 4
y1 = y + rcauchy(51)
y2 = y + rcauchy(51)^2
y3 = y/10 - 5 + rnorm(51)

Y = cbind(y, y1, y2, y3)

m = lm(Y~x)
print(coef(m))

Hope this helps,

Michael

On Fri, Sep 16, 2011 at 3:37 PM, Luke Miller mille...@gmail.com wrote:

 Create an output vector to hold your slopes before starting the loop, then
 use your index i to place each new slope in the appropriate position in
 your
 vector.

 y1-rnorm(100, mean=0.01, sd=0.001)
 y2-rnorm(100, mean=0.1, sd=0.01)

 x-(c(10,400))

 my.slopes = vector(numeric,100)  #  initialize a numeric vector, length
 100, filled with zeros initially

 for (i in 1:100) {

 #create the linear model for each data set
 model1-lm(c(y1[i],y2[i])~x)
 slope1-model1$coefficients[2]
 my.slopes[i] = slope1 #  stick each new slope value into my.slopes[i]
 }

 You could skip the slope1 - model1$coefficients[2]  step and just put the
 slope directly into my.slopes[i] as well.

 On Fri, Sep 16, 2011 at 3:25 PM, beaulieu.j...@epamail.epa.gov wrote:

  Hello,
 
  I would like to write a loop to 1) run 100 linear regressions, and 2)
  compile the slopes of all regression into one vector.  Sample input data
  are:
 
  y1-rnorm(100, mean=0.01, sd=0.001)
  y2-rnorm(100, mean=0.1, sd=0.01)
 
  x-(c(10,400))
 
  #I have gotten this far with the loop
 
  for (i in 1:100) {
 
  #create the linear model for each data set
  model1-lm(c(y1[i],y2[i])~x)
  slope1-model1$coefficients[2]
  }
 
  How can I compile the slopes from all 100 regressions into one vector?
 
  Thanks,
  Jake
 [[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.
 



 --
 ___
 Luke Miller
 Postdoctoral Researcher
 Marine Science Center
 Northeastern University
 Nahant, MA
 (781) 581-7370 x318

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


Re: [R] Help writing basic loop

2011-09-16 Thread MacQueen, Don
Just a minor aside; I would have done

  my.slopes - numeric(100)

Note that:
 class(numeric(5))
[1] numeric

-Don


-- 
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062





On 9/16/11 12:37 PM, Luke Miller mille...@gmail.com wrote:

Create an output vector to hold your slopes before starting the loop, then
use your index i to place each new slope in the appropriate position in
your
vector.

y1-rnorm(100, mean=0.01, sd=0.001)
y2-rnorm(100, mean=0.1, sd=0.01)

x-(c(10,400))

my.slopes = vector(numeric,100)  #  initialize a numeric vector, length
100, filled with zeros initially

for (i in 1:100) {

#create the linear model for each data set
model1-lm(c(y1[i],y2[i])~x)
slope1-model1$coefficients[2]
my.slopes[i] = slope1 #  stick each new slope value into my.slopes[i]
}

You could skip the slope1 - model1$coefficients[2]  step and just put the
slope directly into my.slopes[i] as well.

On Fri, Sep 16, 2011 at 3:25 PM, beaulieu.j...@epamail.epa.gov wrote:

 Hello,

 I would like to write a loop to 1) run 100 linear regressions, and 2)
 compile the slopes of all regression into one vector.  Sample input data
 are:

 y1-rnorm(100, mean=0.01, sd=0.001)
 y2-rnorm(100, mean=0.1, sd=0.01)

 x-(c(10,400))

 #I have gotten this far with the loop

 for (i in 1:100) {

 #create the linear model for each data set
 model1-lm(c(y1[i],y2[i])~x)
 slope1-model1$coefficients[2]
 }

 How can I compile the slopes from all 100 regressions into one vector?

 Thanks,
 Jake
[[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.




-- 
___
Luke Miller
Postdoctoral Researcher
Marine Science Center
Northeastern University
Nahant, MA
(781) 581-7370 x318

   [[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@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 writing for loop

2009-11-25 Thread Jessica Schedlbauer
Hi, 

I’d like to ask for some help in writing a loop.  My situation is the following:

I have a matrix (matrix.A) containing 3 columns and 100 rows.  The columns 
represent parameter estimates a, b, and c.  The rows contain different values 
for these parameter estimates.  Each row is unique.

I want to insert these parameter estimates into a model (say, y = a + bx + 
cx^2) and solve for y given a separate matrix (matrix.B) of x values (where x 
has a length of 1500).

I want to solve for y 100 times using each set of the parameter estimates in 
matrix.A once.

At present my code looks like this and it only performs the first iteration.  

For (i in 1:length(matrix.A)) { y - matrix.A$a[[i]] + matrixA$b[[i]] * 
matrix.B$x + matrixA$c[[i]] * matrix.B$x^2)

I have not been able to figure out how to loop through the rows of parameter 
estimates in matrix.A.  I am new to writing loops, so any assistance would be 
much appreciated.

Regards,
Jessica Schedlbauer

__
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 writing for loop

2009-11-25 Thread Steven McKinney
Hi,

The great thing about the S language is that it is 'vectorized',
so you can do a lot of matrix manipulations without loops.

Here's a smaller example of what you describe.
matrix A with 3 columns and 10 rows (instead of 100)
matrix B with 3 columns and 15 rows (instead of 1500)

 set.seed(123)
 mA - matrix(runif(30), ncol = 3)
 mA
   [,1]   [,2]  [,3]
 [1,] 0.2875775 0.95683335 0.8895393
 [2,] 0.7883051 0.45333416 0.6928034
 [3,] 0.4089769 0.67757064 0.6405068
 [4,] 0.8830174 0.57263340 0.9942698
 [5,] 0.9404673 0.10292468 0.6557058
 [6,] 0.0455565 0.89982497 0.7085305
 [7,] 0.5281055 0.24608773 0.5440660
 [8,] 0.8924190 0.04205953 0.5941420
 [9,] 0.5514350 0.32792072 0.2891597
[10,] 0.4566147 0.95450365 0.1471136

 x - seq(15)
 mB - cbind(1, x, x^2)
 mB
 x
 [1,] 1  1   1
 [2,] 1  2   4
 [3,] 1  3   9
 [4,] 1  4  16
 [5,] 1  5  25
 [6,] 1  6  36
 [7,] 1  7  49
 [8,] 1  8  64
 [9,] 1  9  81
[10,] 1 10 100
[11,] 1 11 121
[12,] 1 12 144
[13,] 1 13 169
[14,] 1 14 196
[15,] 1 15 225

 mR - apply(mB, 1, function(x){mA %*% x})
 mR
  [,1] [,2]  [,3]  [,4]  [,5] [,6] [,7]
 [1,] 2.133950 5.759401 11.163931 18.347540 27.310227 38.05199 50.57284
 [2,] 1.934443 4.466187  8.383538 13.686496 20.375061 28.44923 37.90901
 [3,] 1.727054 4.326145  8.206250 13.367368 19.809500 27.53265 36.53681
 [4,] 2.449921 6.005363 11.549346 19.081867 28.602929 40.11253 53.61067
 [5,] 1.699098 3.769140  7.150594 11.843459 17.847736 25.16342 33.79052
 [6,] 1.653912 4.679328  9.121806 14.981344 22.257943 30.95160 41.06232
 [7,] 1.318259 3.196545  6.162963 10.217513 15.360195 21.59101 28.90995
 [8,] 1.528621 3.353106  6.365876 10.566930 15.956267 22.53389 30.29979
 [9,] 1.168515 2.363915  4.137635  6.489674  9.420032 12.92871 17.01571
[10,] 1.558232 2.954077  4.644149  6.628448  8.906974 11.47973 14.34671
  [,8] [,9] [,10] [,11] [,12] [,13] [,14]
 [1,] 64.87276 80.95176  98.80984 118.44700 139.86324 163.05856 188.03295
 [2,] 48.75440 60.98539  74.60199  89.60419 105.99201 123.76542 142.92445
 [3,] 46.82198 58.38816  71.23536  85.36358 100.77281 117.46305 135.43430
 [4,] 69.09735 86.57257 106.03633 127.48863 150.92947 176.35884 203.77676
 [5,] 43.72904 54.97896  67.54029  81.41304  96.59720 113.09277 130.89975
 [6,] 52.59011 65.53495  79.89685  95.67582 112.87184 131.48493 151.51508
 [7,] 37.31703 46.81224  57.39559  69.06706  81.82667  95.67440 110.61027
 [8,] 39.25398 49.39646  60.72722  73.24626  86.95358 101.84919 117.93309
 [9,] 21.68102 26.92466  32.74662  39.14689  46.12549  53.68240  61.81763
[10,] 17.50792 20.96335  24.71302  28.75691  33.09502  37.72737  42.65394
  [,15]
 [1,] 214.78642
 [2,] 163.46908
 [3,] 154.68657
 [4,] 233.18322
 [5,] 150.01814
 [6,] 172.96229
 [7,] 126.63428
 [8,] 135.20527
 [9,]  70.53119
[10,]  47.87474
 

so the results matrix mR has one column for each element of x,
and the i-th row element is the solution for
y = a + bx + cx^2
for the i-th row of coefficients in the parameter estimates matrix mA

You can spot check with e.g. 

 mA[1,] %*% mB[15,]
 [,1]
[1,] 214.7864
 mA[1,] %*% mB[5,]
 [,1]
[1,] 27.31023
 mA[1,]
[1] 0.2875775 0.9568333 0.8895393
 mB[5,]
x
 1  5 25 
 



HTH

Steven McKinney, Ph.D.

Statistician
Molecular Oncology and Breast Cancer Program
British Columbia Cancer Research Centre

email: smckinney +at+ bccrc +dot+ ca

tel: 604-675-8000 x7561

BCCRC
Molecular Oncology
675 West 10th Ave, Floor 4
Vancouver B.C.
V5Z 1L3
Canada


From: r-help-boun...@r-project.org [r-help-boun...@r-project.org] On Behalf Of 
Jessica Schedlbauer [jsche...@fiu.edu]
Sent: November 25, 2009 10:43 AM
To: r-help@r-project.org
Subject: [R] help writing for loop

Hi,

I’d like to ask for some help in writing a loop.  My situation is the following:

I have a matrix (matrix.A) containing 3 columns and 100 rows.  The columns 
represent parameter estimates a, b, and c.  The rows contain different values 
for these parameter estimates.  Each row is unique.

I want to insert these parameter estimates into a model (say, y = a + bx + 
cx^2) and solve for y given a separate matrix (matrix.B) of x values (where x 
has a length of 1500).

I want to solve for y 100 times using each set of the parameter estimates in 
matrix.A once.

At present my code looks like this and it only performs the first iteration.

For (i in 1:length(matrix.A)) { y - matrix.A$a[[i]] + matrixA$b[[i]] * 
matrix.B$x + matrixA$c[[i]] * matrix.B$x^2)

I have not been able to figure out how to loop through the rows of parameter 
estimates in matrix.A.  I am new to writing loops, so any assistance would be 
much appreciated.

Regards,
Jessica Schedlbauer

__
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