Since WinBUGS and R have different ways of organizing data for arrays, how should I organize the data when using R2WinBUGS so that the order is correct? Thanks!
You shouldn't have to worry about this with R2WinBUGS if you specify the data as a named list of objects (see ?bugs - data argument). R2WinBUGS will reorganize the data so that its structure in WinBUGS will be the same as it was in R.
For example, if you specify an array in R:
y <- array(1:24,dim=c(2,3,4))
which looks like
> y
, , 1
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
, , 2
[,1] [,2] [,3]
[1,] 7 9 11
[2,] 8 10 12
, , 3
[,1] [,2] [,3]
[1,] 13 15 17
[2,] 14 16 18
, , 4
[,1] [,2] [,3]
[1,] 19 21 23
[2,] 20 22 24
and then specify it in data argument of bugs function
(e.g., bugs(data=list(y=y)...) then the data for WinBUGS (data.txt) is:
list(y= structure(.Data= c(1.00000E+00, 7.00000E+00, 1.30000E+01, 1.90000E+01,
3.00000E+00, 9.00000E+00, 1.50000E+01, 2.10000E+01, 5.00000E+00, 1.10000E+01,
1.70000E+01, 2.30000E+01, 2.00000E+00, 8.00000E+00, 1.40000E+01, 2.00000E+01,
4.00000E+00, 1.00000E+01, 1.60000E+01, 2.20000E+01, 6.00000E+00, 1.20000E+01,
1.80000E+01, 2.40000E+01), .Dim=c(2, 3, 4)))
which looks like this in WinBUGS:
y[1,1,1] 1.0
y[1,1,2] 7.0
y[1,1,3] 13.0
y[1,1,4] 19.0
y[1,2,1] 3.0
y[1,2,2] 9.0
y[1,2,3] 15.0
y[1,2,4] 21.0
y[1,3,1] 5.0
y[1,3,2] 11.0
y[1,3,3] 17.0
y[1,3,4] 23.0
y[2,1,1] 2.0
y[2,1,2] 8.0
y[2,1,3] 14.0
y[2,1,4] 20.0
y[2,2,1] 4.0
y[2,2,2] 10.0
y[2,2,3] 16.0
y[2,2,4] 22.0
y[2,3,1] 6.0
y[2,3,2] 12.0
y[2,3,3] 18.0
y[2,3,4] 24.0
Related
I have this dataset:
dbppre dbppost per1pre per1post per2pre per2post
0.544331824055634 0.426482748529805 1.10388140870983 1.14622255457398 1.007302668 1.489675646
0.44544008292805 0.300746382647025 0.891104906479033 0.876840408251785 0.919450773 0.892276804
0.734783578764543 0.489971007532308 1.02796075709944 0.79655130374748 0.610340504 0.936092006
1.04113077142586 0.386513119551008 0.965359488375859 1.04314173155816 1.122001994 0.638452078
0.333368637355291 0.525460160226716 NA 0.633435747 1.196988457 0.396543005
1.76769244892893 0.726077921840058 1.08060419667991 0.974269083108835 1.245643507 1.292857474
1.41486783 NA 0.910710353033318 1.03435985624106 0.959985314 1.244732938
1.01932795229362 0.624195252685448 1.27809687379565 1.59656046306852 1.076534265 0.848544508
1.3919315726037 0.728230610741795 0.817900465495852 1.24505216554384 0.796182044 1.47318564
1.48912544220417 0.897585509143984 0.878534099910696 1.12148645028777 1.096723799 1.312244217
1.56801709691326 0.816474814896344 1.13655475536592 1.01299018097117 1.226607978 0.863016615
1.34144721808244 0.596169010679233 1.889775937 NA 1.094095173 1.515202105
1.17409999971024 0.626873517936125 0.912837009713984 0.814632450682884 0.898149331 0.887216585
1.06862027138743 0.427855128881696 0.727537839417515 1.15967069522768 0.98168375 1.407271061
1.50406121956726 0.507362673558659 1.780752715 0.658835953 2.008229626 1.231869338
1.44980944220763 0.620658801480513 0.885827192590202 0.651268425772394 1.067548223 0.994736445
1.27975202574336 0.877955236879164 0.595981804265367 0.56002696152466 0.770642278 0.519875921
0.675518080750329 0.38478948746306 0.822745530980815 0.796051785239611 1.16899539 1.16658889
0.839686262472682 0.481534573379965 0.632380676760052 0.656052506855686 0.796504954 1.035781891
.
.
.
As you can see, there are multiple cuantitative variables for gene expression data, each gene meassured two times, pre and post treatment, with some missing values in some of the variables.
Each row corresponds to one individual, and they are also divided in two groups (0 = control, 1 = really treated).
I would like to make a correlation (Spearman or Pearson depending on normality, but by group, and obtaining the correlation value and the p-value significance, avoiding the NAs.
Is it possible?
I know how to implement cor.test() function to compare two variables, but I could not find any variable inside this function to take groups into account.
I also discovered plyr and data.table libraries to do so, by groups, but they return just the correlation value without p-value, and I haven't been able to make it word for variables with NAs.
Suggestions?
You could use the Hmisc package.
library(Hmisc)
set.seed(10)
dt<-matrix(rnorm(100),5,5) #create matrix
dt[1,1]<-NA #introduce NAs
dt[2,4]<-NA #introduce NAs
cors<-rcorr(dt, type="spearman") #spearman correlation
corp<-rcorr(dt, type="pearson") #pearson correlation
> corspear
[,1] [,2] [,3] [,4] [,5]
[1,] 1.0 0.4 0.2 0.5 -0.4
[2,] 0.4 1.0 0.1 -0.4 0.8
[3,] 0.2 0.1 1.0 0.4 0.1
[4,] 0.5 -0.4 0.4 1.0 -0.8
[5,] -0.4 0.8 0.1 -0.8 1.0
n
[,1] [,2] [,3] [,4] [,5]
[1,] 4 4 4 3 4
[2,] 4 5 5 4 5
[3,] 4 5 5 4 5
[4,] 3 4 4 4 4
[5,] 4 5 5 4 5
P
[,1] [,2] [,3] [,4] [,5]
[1,] 0.6000 0.8000 0.6667 0.6000
[2,] 0.6000 0.8729 0.6000 0.1041
[3,] 0.8000 0.8729 0.6000 0.8729
[4,] 0.6667 0.6000 0.6000 0.2000
[5,] 0.6000 0.1041 0.8729 0.2000
For further details see the help section: ?rcorr
rcorr returns a list with elements r, the matrix of correlations, n
the matrix of number of observations used in analyzing each pair of
variables, and P, the asymptotic P-values. Pairs with fewer than 2
non-missing values have the r values set to NA.
I want to plot Delta~Project.Types in R. I have 10 Project Types. I know how to do the boxplot :
boxplot(Delta~Project.Types). However, how can I put the fivenum (min, max, 1st, 2nd, and 3rd quantile) on each boxplot? How can I do for that every boxplot of the image will have its five number shown? That would be easier to compare the boxplots when the values are shown
Thanks!
The stats you want can also be obtained with fivenum
five <- by(InsectSprays$count, InsectSprays$spray, fivenum)
do.call(cbind, five)
# A B C D E F
# [1,] 7.0 7.0 0.0 2.0 1.0 9
# [2,] 11.0 12.0 1.0 3.5 2.5 12
# [3,] 14.0 16.5 1.5 5.0 3.0 15
# [4,] 18.5 18.0 3.0 5.0 5.0 23
# [5,] 23.0 21.0 7.0 12.0 6.0 26
Alternatively, these stats are one of the return values of boxplot (note that you need to use range = 0 to get the min and max since there are some values which are outlying):
bp <- boxplot(count ~ spray, data = InsectSprays, col = "lightgray", range = 0)
bp$stats
# [,1] [,2] [,3] [,4] [,5] [,6]
# [1,] 7.0 7.0 0.0 2.0 1.0 9
# [2,] 11.0 12.0 1.0 3.5 2.5 12
# [3,] 14.0 16.5 1.5 5.0 3.0 15
# [4,] 18.5 18.0 3.0 5.0 5.0 23
# [5,] 23.0 21.0 7.0 12.0 6.0 26
Then just add to each box:
text(x = col(bp$stats) - .5, y = bp$stats, labels = bp$stats)
You can add a "legend" to a base R plot that contains what you want like this:
legend("topright", bty = "n", legend = summary(Delta))
I'm assuming that it's "Delta" that you're running summary() on, so change that as needed. You can modify the appearance of what shows up in the legend using paste(), i.e.
legend("topright", bty = "n", legend = c(paste("min =", summary(Delta)[1]),
paste("max =", summary(Delta)[2])))
etc.
I have a problem dealing with time series in R.
#--------------read data
wb = loadWorkbook("Countries_Europe_Prices.xlsx")
df = readWorksheet(wb, sheet="Sheet2")
x <- df$Year
y <- df$Index1
y <- lag(y, 1, na.pad = TRUE)
cbind(x, y)
It gives me the following output:
x y
[1,] 1974 NA
[2,] 1975 50.8
[3,] 1976 51.9
[4,] 1977 54.8
[5,] 1978 58.8
[6,] 1979 64.0
[7,] 1980 68.8
[8,] 1981 73.6
[9,] 1982 74.3
[10,] 1983 74.5
[11,] 1984 72.9
[12,] 1985 72.1
[13,] 1986 72.3
[14,] 1987 71.7
[15,] 1988 72.9
[16,] 1989 75.3
[17,] 1990 81.2
[18,] 1991 84.3
[19,] 1992 87.2
[20,] 1993 90.1
But I want the first value in y to be 50.8 and so forth. In other words, I want to get a negative lag. I don't get it, how can I do it?
My problem is very similar to this problem, but however I cannot solve it. I guess I still do not understand the solution(s)...
Basic lag in R vector/dataframe
How about the built-in 'lead' function? (from the dplyr package)
Doesn't it do exactly the job of Ahmed's function?
cbind(x, lead(y, 1))
If you want to be able to calculate either positive or negative lags in the same function, i suggest a 'shorter' version of his 'shift' function:
shift = function(x, lag) {
require(dplyr)
switch(sign(lag)/2+1.5, lead(x, abs(lag)), lag(x, abs(lag)))
}
What it does is creating 2 cases, one with lag the other with lead, and chooses one case depending on the sign of your lag (the +1.5 is a trick to transform a {-1, +1} into a {1, 2} alternative).
There is an easier way of doing this which I have captured fully from this link. What I will do here is explaining what should you do in steps:
First create the following function by running the following code:
shift<-function(x,shift_by){
stopifnot(is.numeric(shift_by))
stopifnot(is.numeric(x))
if (length(shift_by)>1)
return(sapply(shift_by,shift, x=x))
out<-NULL
abs_shift_by=abs(shift_by)
if (shift_by > 0 )
out<-c(tail(x,-abs_shift_by),rep(NA,abs_shift_by))
else if (shift_by < 0 )
out<-c(rep(NA,abs_shift_by), head(x,-abs_shift_by))
else
out<-x
out
}
This will create a function called shift with two arguments; one is the vector you need to operate its lag/lead and the other is number of lags/leads you need.
Example:
Suppose you have the following vector:
x<-seq(1:10)
x
[1] 1 2 3 4 5 6 7 8 9 10
if you need x's first order lag
shift(x,-1)
[1] NA 1 2 3 4 5 6 7 8 9
if you need x's first order lead (negative lag)
shift(x,1)
[1] 2 3 4 5 6 7 8 9 10 NA
Simpler solution:
y = dplyr::lead(y,1)
The opposite of lag() function is lead()
EDITED BELOW TO SHOW A REALLY NEAT SOLUTION -- THANKS TO HADLEY WICKHAM.
I have a very specific query, but it also relates to some general shortcomings in my R knowledge which I would like to rectify. I'd like also (if possible) not just solve my problem but do so in an elegant and efficient way---maybe I am setting my sights to high. Can anyone both answer my specific queries, but also recommend a good source to find out more? Any help greatly appreciated. It seems Hadley Wickham has wrestled with a similar problem here - http://www.slideshare.net/hadley/plyr-one-data-analytic-strategy - but these are slides from a presentation, and I struggle to understand the slides by themselves.
I am trying to manipulate MCMC output stored in a list in R. The data are grouped into five years, and for each year I have four groups. The goal is to plot these. To make the problem tractable, here is the output for just ten iterations, like so.
iterations [,1] [,2] [,3] [,4]
[1,] 49.184181 4.3515983 16.051958 -14.896019
[2,] 45.910362 2.1738066 17.161775 -29.880989
[3,] 14.575248 7.9476606 8.385455 -34.753004
[4,] 55.029604 2.3422748 16.366960 -66.182627
[5,] 25.338546 8.3039173 16.937638 -26.697235
[6,] 48.633115 0.4698014 16.130142 -65.659992
[7,] 1.356642 3.0249349 2.388576 -1.700559
[8,] 49.831352 -2.0644832 15.403726 -23.378055
[9,] 13.057886 -2.8856576 11.481152 -36.697754
[10,] 50.889166 2.6846852 15.763382 -23.049868
, , 2
iterations [,1] [,2] [,3] [,4]
[1,] 51.6134663 15.659392 17.218244 -47.864892
[2,] 46.0545981 17.067779 18.158151 -38.336587
[3,] 16.5690775 10.386358 10.991029 -30.225820
[4,] 55.5724832 14.840466 15.556193 -54.432882
[5,] 26.1064404 5.656579 15.063810 -5.085942
[6,] 57.3084200 12.551751 16.212203 -52.459935
[7,] 0.9825892 6.651478 1.999976 -5.350995
[8,] 56.1117252 3.204124 16.011812 -21.179722
[9,] 15.4204854 5.761157 12.594028 -43.691113
[10,] 50.1407397 16.404882 15.990908 -26.019990
, , 3
iterations [,1] [,2] [,3] [,4]
[1,] 53.521436 24.340327 16.073063 -20.939950
[2,] 46.040969 21.025351 16.535917 -47.611395
[3,] 19.276578 16.575285 14.824175 -18.432136
[4,] 58.050774 20.886686 15.944355 -37.646286
[5,] 26.008007 11.449253 13.027001 -56.572886
[6,] 61.474771 18.270354 15.879238 -31.316868
[7,] 1.515227 1.434234 3.568761 -1.328706
[8,] 61.725967 19.212081 16.717331 -18.993349
[9,] 15.303739 6.939953 11.940742 -54.261739
[10,] 47.968838 20.070758 17.168400 -48.598802
, , 4
iterations [,1] [,2] [,3] [,4]
[1,] 51.952695 24.267668 17.867717 -28.129743
[2,] 49.680524 22.914727 16.001512 -44.434294
[3,] 18.519755 17.961953 15.831455 -57.110802
[4,] 59.652211 21.655724 16.876315 -24.965724
[5,] 29.091609 20.831196 15.546565 -59.272164
[6,] 62.190041 21.112490 15.759867 -19.910655
[7,] 3.116584 1.187595 1.050807 -7.721749
[8,] 61.384355 27.331487 16.646250 -17.793893
[9,] 16.320224 14.321294 13.726538 -47.748184
[10,] 47.676867 27.325987 17.056364 -31.032911
, , 5
iterations [,1] [,2] [,3] [,4]
[1,] 55.326522 33.737691 19.698060 -46.34804
[2,] 51.122038 31.055026 19.668949 -64.52942
[3,] 22.036674 17.577561 13.546166 -85.24881
[4,] 60.481009 34.300432 16.903054 -25.19277
[5,] 29.168884 26.811356 16.066908 -37.56252
[6,] 54.221450 28.760434 16.480317 -36.42441
[7,] 3.672456 1.571084 2.397663 -10.91522
[8,] 56.223306 30.730421 18.185858 -28.30282
[9,] 16.955258 16.699139 18.101711 -36.85851
[10,] 48.220404 29.749342 17.557532 -38.22831
Some further information:
> str(a.type)
List of 1
$ a_type: num [1:10, 1:4, 1:5] 49.2 45.9 14.6 55 25.3 ...
..- attr(*, "dimnames")=List of 3
.. ..$ iterations: NULL
.. ..$ : NULL
.. ..$ : NULL
What I am looking for (for the immediate problem) is a way of naming the dimensions (i.e. the groups and the years) of this (with the dimnames() command), and second, taking some summary values from each column (group) in each of the five years. Something that will apply the following to each of the four columns for each of the five years:
myfunc <- function(x)c(mean(x),
quantile(x,c(.025,.975)))
Any help greatly appreciated. Also, as I said, if anyone can recommend a good source on problems like this, I might not have to ask questions like this so often in future.
Note added: Based on the helpful answer below, I have figured out part of my problem. I can name the dimensions as follows:
dimnames(a.type[[1]])=list(paste('iter',1:10,sep=''), ## 10 iterations
paste(c("Delivery", "Other", "Regulatory", "Transfer")), ## 4 groups
paste('Year',1:5,sep='')) ## 5 Years
This makes the following (just showing year 1):
> a.type
$a_type
, , Year1
Delivery Other Regulatory Transfer
iter1 49.184181 4.3515983 16.051958 -14.896019
iter2 45.910362 2.1738066 17.161775 -29.880989
iter3 14.575248 7.9476606 8.385455 -34.753004
iter4 55.029604 2.3422748 16.366960 -66.182627
iter5 25.338546 8.3039173 16.937638 -26.697235
iter6 48.633115 0.4698014 16.130142 -65.659992
iter7 1.356642 3.0249349 2.388576 -1.700559
iter8 49.831352 -2.0644832 15.403726 -23.378055
iter9 13.057886 -2.8856576 11.481152 -36.697754
iter10 50.889166 2.6846852 15.763382 -23.049868
So that works. A further question: how can I just name the groups and years---I have not much interest in naming the iterations, and indeed I want to be able to output different numbers of iterations without changing my code. In other words is there a logical way to skip over naming the iterations. If I do...
dimnames(a.type[[1]])=list(, ##
paste(c("Delivery", "Other", "Regulatory", "Transfer")), ## 4 groups
paste('Year',1:5,sep='')) ## 5 Years
...then I get an error message...
> dimnames(a.type[[1]][2:3])=list(#paste('iter',1:10,sep=''), ## 10 years
+ paste(c("Delivery", "Other", "Regulatory", "Transfer")), ## 4 groups
+ paste('Year',1:5,sep='')) ## 5 Years
Error in dimnames(a.type[[1]][2:3]) = list(paste(c("Delivery", "Other", :
'dimnames' applied to non-array
On the other thing, applying a function. I can do the following, but that gives me I think the mean and quantiles across all years:
> myfunc <- function(x)c(mean(x),
+ quantile(x,c(.025,.975)))
>
>
>
>
> a.type.bar <- apply(a.type[[1]], 2, myfunc)
> a.type.bar
Delivery Other Regulatory Transfer
38.351706 14.892788 14.450314 -34.61954
2.5% 1.392323 -1.494269 2.087411 -66.06503
97.5% 61.669447 33.134091 19.335254 -2.46227
>
On the other hand, I can do the following, and apply my function to just one year at a time:
a.type.bar <- apply(a.type[[1]][,,1], 2, myfunc)
Now obviously, that would solve my problem -- I would just have to type five lines of code. But to figure out the deeper problem, is there a way of getting means and quantiles a year at a time?
Thanks.
Note added 17 March 2013. Thanks to Hadley Wickham's marvellous plyr package, I seem to have a solution---and thanks Zach for turning me onto it.
library(plyr)
myfunc <- function(x)c(mean(x),
quantile(x,c(.025,.975)))
summaries <- adply(a.type[[1]], 2:3, myfunc)
This gives the following output.
> summaries
X1 X2 V1 2.5% 97.5%
1 Delivery 1996 78.6691388 39.912455 109.61078
2 Other 1996 4.3485461 -4.584758 16.61764
3 Regulatory 1996 19.6444938 14.135322 24.00373
4 Transfer 1996 -0.7922307 -195.263744 203.95175
5 Delivery 1997 79.6291215 29.853200 109.26860
6 Other 1997 14.3462871 5.607952 22.68043
7 Regulatory 1997 22.4131984 16.861994 30.09017
8 Transfer 1997 4392.7699174 991.168626 8426.64365
9 Delivery 1998 85.9237011 52.100181 115.78991
10 Other 1998 21.4735955 9.790307 37.40546
11 Regulatory 1998 25.5654754 19.558132 30.58021
12 Transfer 1998 6166.7374268 2456.330035 10249.00350
13 Delivery 1999 90.1843678 52.574874 128.28546
14 Other 1999 27.2028622 14.373959 38.54636
15 Regulatory 1999 28.8851480 20.913437 34.59272
16 Transfer 1999 8116.6049650 4186.782183 12030.65517
17 Delivery 2000 91.0299168 47.211931 125.35626
18 Other 2000 31.5885924 16.087480 46.28089
19 Regulatory 2000 31.7628775 21.082236 40.29969
20 Transfer 2000 9203.9975199 2349.851364 14382.00472
All that is left now is to plot this (well, and several other versions of the same model). I am having a play with ggplot.
You want to get your data into a data frame instead of a matrix, and then use the formula interface to aggregate.
Ideally you want to get your MCMC output in a form that you can read directly into a data frame, but if you are stuck with the matrix, then use melt or reshape + as.data.frame or just do something like this (assuming you have a matrix called M with the three dimensions discussed above):
d<-data.frame(year=rep(1991:1995,each=40),
agency=rep(c("D","O","T","R"),50),
iteration=rep(0:9,5,each=4),
spend=as.vector(M))
in order to get a data frame that looks like this:
year agency iteration spend
1 1996 D 0 49.184181
2 1996 O 0 4.351598
3 1996 R 0 16.051958
4 1996 T 0 -14.896019
5 1996 D 1 45.910362
6 1996 O 1 2.173807
7 1996 R 1 17.161775
...
Now you can use aggregate to apply your function, like this:
aggregate(spend~agency+year,d,myfunc)
to get
agency year spend.V1 spend.2.5% spend.97.5%
1 D 1996 35.380610 3.989422 54.098005
2 O 1996 2.634854 -2.700893 8.223760
3 R 1996 13.607076 3.737874 17.111344
4 T 1996 -32.289610 -66.065034 -4.669537
5 D 1997 37.588003 4.231116 57.039164
6 O 1997 10.818397 3.755926 16.918627
...
and now you can slice and dice as you wish
aggregate(spend~year,d,myfunc)
aggregate(spend~agency,d,myfunc)
etc...
I don't know the dimensions of your array , but here an example:
dat <- array(sample(1:5,10*4*5,rep=TRUE),c(10,4,5))
Using dimnames here is a good idea since you have many dimensions, this will help you to understand the output of your aggregation function. You need just to spply a list of names with the good dimensions.
dimnames(dat)=list(paste('year',1:10,sep=''), ## 10 years
paste('group',letters[1:4],sep=''), ## 4 groups
paste('iter',1:5,sep='')) ## 5 iterations
Then using apply to get means by iteration
apply(dat,3,rowMeans)
iter1 iter2 iter3 iter4 iter5
year1 2.25 3.00 3.75 3.00 3.00
year2 3.00 3.00 3.00 2.25 3.25
year3 3.75 3.50 3.50 3.50 3.50
year4 2.00 2.25 3.50 1.50 3.50
year5 2.50 2.50 3.50 2.75 3.50
year6 2.75 3.75 2.00 4.00 2.50
year7 3.50 2.50 3.50 2.50 2.75
year8 3.25 2.75 4.50 2.50 3.75
year9 4.50 3.25 3.25 3.00 2.25
year10 1.75 4.25 3.25 1.50 2.00
To get means by group over years
> apply(dat,3,colMeans)
iter1 iter2 iter3 iter4 iter5
groupa 3.1 3.0 3.3 2.8 2.9
groupb 2.7 3.6 3.0 2.8 2.7
groupc 3.6 3.3 3.4 2.1 3.3
groupd 2.3 2.4 3.8 2.9 3.1
I have this R code:
> coef
[1] 1.5 2.4 3.9 4.4
> y
[,1] [,2] [,3] [,4]
[1,] 1 2 12 45
[2,] 5 6 7 8
[3,] 9 10 2 12
[4,] 13 14 15 45
[5,] 17 18 39 7
I have to multiply each value of the column with the respective coef. The result should be:
First column:
1*1.5
5*1.5
9*1.5
13*1.5
17*1.5
Second column:
2*2.4
6*2.4
10*2.4
14*2.4
18*2.4
Third column:
12*3.9
7*3.9
2*3.9
15*3.9
39*3.9
Fourth column:
45*4.4
8*4.4
12*4.4
45*4.4
7*4.4
All the column's values moltiplied by the same coefficient at the same index in the vector.
How can I do this calculation?
The solution could be:
> y[,1] <- y[,1] * coef[1]
> y[,2] <- y[,2] * coef[2]
> y[,3] <- y[,3] * coef[3]
> y[,4] <- y[,4] * coef[4]
But doesn't seem too optimized! Something better?
Thank you!
This will give you what you want:
t( t(y) * coef )
Two more possibilities: sweep and scale (the latter only operates columnwise, and seems to me to be a bit of hack).
coef <- c(1.5,2.4,3.9,4.4)
y <- matrix(c(seq(1,17,by=4),
seq(2,18,by=4),
c(12,7,2,15,39,
45,8,12,45,7)),
ncol=4)
t(t(y)*coef)
t(apply(y,1,"*",coef))
sweep(y,2,coef,"*")
scale(y,center=FALSE,scale=1/coef)
library(rbenchmark)
benchmark(t(t(y)*coef),
y %*% diag(coef),
t(apply(y,1,"*",coef)),
sweep(y,2,coef,"*"),
scale(y,center=FALSE,scale=1/coef),
replications=1e4)
test replications elapsed relative
5 scale(y, center = FALSE, scale = 1/coef) 10000 0.990 4.342105
4 sweep(y, 2, coef, "*") 10000 0.846 3.710526
3 t(apply(y, 1, "*", coef)) 10000 1.537 6.741228
1 t(t(y) * coef) 10000 0.228 1.000000
2 y %*% diag(coef) 10000 0.365 1.600877
edit: added y %*% diag(coef) from #baptiste [not fastest, although it might be so for a big problem with a sufficiently optimized BLAS package ...] [and it was fastest in another trial, so I may just not have had a stable estimate]
edit: fixed typo in t(t(y)*coef) [thanks to Timur Shtatland] (but did not update timings, so they might be slightly off ...)
I also tried library(Matrix); y %*% Diagonal(x=coef), which is very slow for this example but might be fast for a large matrix (??). (I also tried constructing the diagonal matrix just once, but even multiplication by a predefined matrix was slow in this example (25x slower than the best, vs. 47x slower when defining the matrix on the fly.)
I have a mild preference for sweep as I think it expresses most clearly the operation being done ("multiply the columns by the elements of coef")
apply(y, 1, "*", coef)
# -- result --
[,1] [,2] [,3] [,4] [,5]
[1,] 1.5 7.5 13.5 19.5 25.5
[2,] 4.8 14.4 24.0 33.6 43.2
[3,] 46.8 27.3 7.8 58.5 152.1
[4,] 198.0 35.2 52.8 198.0 30.8
A late entry:
coef[col(y)]*y
On my system, this is the fastest.
test replications elapsed relative
6 coef[col(y)] * y 10000 0.068 1.000
5 scale(y, center = FALSE, scale = 1/coef) 10000 0.640 9.412
4 sweep(y, 2, coef, "*") 10000 0.535 7.868
3 t(apply(y, 1, "*", coef)) 10000 0.837 12.309
1 t(t(y) * coef) 10000 0.176 2.588
2 y %*% diag(coef) 10000 0.187 2.750