Linearly regress a vector against each column of a matrix - r

I have a very simple question which I am sure there is an elegant answer to (I am also sure the title above is inappropriate). I have a vector of y values:
y = matrix(c(1, 2, 3, 4, 5, 6, 7), nrow=7, ncol=1)
which I would like to regress against each column in a matrix, x:
x = matrix(c(1, 2, 3, 4, 5, 6, 7, 7, 6, 5, 4, 3, 2, 1, 4, 4, 4, 4, 4, 4, 4), nrow=7, ncol=3)
For example I would like to linearly regress the first column of x against y and then the second column of x against y until the last column of x is reached:
regression.1=lm(y~x[,1])
regression.2=lm(y~x[,2])
I would later like to plot the slope of these regression versus other parameters so it would be useful if the model coefficient parameters are easily accessible in the usual way:
slope.1 = summary(regression.1)$coefficients[2,1]
I am guessing a list using something like plyr but I am too new to this game to find the simplest way to code this.

store <- mapply(col.ind = 1:ncol(x),function(col.ind){ lm(y~x[,col.ind]) })
You can then access the slope using:
> store[1,]
[[1]]
(Intercept) x[, col.ind]
6.713998e-16 1.000000e+00
[[2]]
(Intercept) x[, col.ind]
8 -1
[[3]]
(Intercept) x[, col.ind]
4 NA

Another way:
regression <- apply(x, 2, function(z)lm(y~z))
slope <- sapply(regression, function(z)unname(coef(z)[2]))
Result:
> slope
[1] 1 -1 NA

Related

Is there in R function for finding index of an array?

this is going to be a body of the particular Question.
which function we are using in array .
You can find the index of the element by the functions
which() or match()
Example for using which():
# vector created
v <- c(0, 1, 2, 3, 4,
5, 6, 7, 8, 9)
# which function is used
# to get the index
which(v == 5) # output is: 6
Example for using match():
# vector created
v <- c(0, 1, 2, 3, 4,
5, 6, 7, 8, 9)
# match function is
# used to get the index
match( 5 , v ) # output is: 6
You can see here more information
For a matrix or an array, set the argument arr.ind = TRUE:
which(myarray == 5, arr.ind = TRUE)

Calculate the Average of Data frame iteratively

I have a set of Data and I want to be able to iteratively calculate the Average. i.e. the first two points, then the first three points, then first four points, and so on.
For example,
D <- [1, 2, 3, 4, 5, 6]
Average <- [1.5, 2, 2.5, 3, 3.5]
Is there any way to do that in R?
Try the following:
D <- c(1, 2, 3, 4, 5, 6)
(cumsum(D)/1:length(D))[-1]
# [1] 1.5 2.0 2.5 3.0 3.5

How to only get value from data.frame in R?

i am trying to calculate the probabilities of 4 dices being thrown in R. I am nearly finished, i just want to know how i could possibly access ONLY the value in a specific row of my test1 dataframe? If i write rowSums(test1[1,]) it gives me both the index AND the sum, but i only want to access the sum to be able to store how many possibilities there are to get i.e. a 4 with 4 dices etc.
HereĀ“s the important place of the code.
wurf1 <- c(1, 2, 3, 4, 5, 6)
wurf2 <- c(1, 2, 3, 4, 5, 6)
wurf3 <- c(1, 2, 3, 4, 5, 6)
wurf4 <- c(1, 2, 3, 4, 5, 6)
test1 <- data.frame(expand.grid(wurf1, wurf2, wurf3, wurf4))
rowSums(test1[1,]) #this gives me:
1
4 #because the sum of the values in index 1 = 4
Thank you for your help in advance.

Variable sample upper value in R

I have the following matrix
m <- matrix(c(2, 4, 3, 5, 1, 5, 7, 9, 3, 7), nrow=5, ncol=2,)
colnames(x) = c("Y","Z")
m <-data.frame(m)
I am trying to create a random number in each row where the upper limit is a number based on a variable value (in this case 1*Y based on each row's value for for Z)
I currently have:
samp<-function(x){
sample(0:1,1,replace = TRUE)}
x$randoms <- apply(m,1,samp)
which work works well applying the sample function independently to each row, but I always get an error when I try to alter the x in sample. I thought I could do something like this:
samp<-function(x){
sample(0:m$Z,1,replace = TRUE)}
x$randoms <- apply(m,1,samp)
but I guess that was wishful thinking.
Ultimately I want the result:
Y Z randoms
2 5 4
4 7 7
3 9 3
5 3 1
1 7 6
Any ideas?
The following will sample from 0 to x$Y for each row, and store the result in randoms:
x$randoms <- sapply(x$Y + 1, sample, 1) - 1
Explanation:
The sapply takes each value in x$Y separately (let's call this y), and calls sample(y + 1, 1) on it.
Note that (e.g.) sample(y+1, 1) will sample 1 random integer from the range 1:(y+1). Since you want a number from 0 to y rather than 1 to y + 1, we subtract 1 at the end.
Also, just pointing out - no need for replace=T here because you are only sampling one value anyway, so it doesn't matter whether it gets replaced or not.
Based on #mathematical.coffee suggestion and my edited example this is the slick final result:
m <- matrix(c(2, 4, 3, 5, 1, 5, 7, 9, 3, 7), nrow=5, ncol=2,)
colnames(m) = c("Y","Z")
m <-data.frame(m)
samp<-function(x){
sample(Z + 1, 1)}
m$randoms <- sapply(m$Z + 1, sample, 1) - 1

Use layout function within levelplot

I am doing mapping in R and found the very useful levelplot function in rasterVis package. I will like to display multiple plots in a window. However, par(mfcol) does not fit within lattice. I found layout function very useful in my case but it fails to perform what I want to do.
Here is my code:
s <- stack(Precip_DJF1, Precip_DJF2, Precip_DJF3, Precip_DJF4,
Precip_DJF5, Precip_DJF6)
levelplot(s, layout(matrix(c(1, 2, 0, 3, 4, 5), 2, 3)),
at=seq(floor(3.81393), ceiling(23.06363), length.out=20),
par.settings=themes, par.strip.text=list(cex=0),
scales=list(alternating=FALSE))
Using
layout(matrix(c(1, 2, 0, 3, 4, 5), 2, 3))
fails while layout(3, 2) works but the plots are displayed row-wise instead of column-wise. I want the plots to be displayed in column 1, then column 2 etc. Something like:
mat <- matrix(c(1, 2, 3, 4, 5, 6), 2, 3)
> mat
# [,1] [,2] [,3]
# [1,] 1 3 5
# [2,] 2 4 6
Is there a function within levelplot or lattice to do this kind of layout?
Thanks in advance.
As suggested by #Pascal, you can use index.cond to do this:
For example:
library(rasterVis)
s <- stack(replicate(6, raster(matrix(runif(100), 10))))
levelplot(s, layout=c(3, 2), index.cond=list(c(1, 3, 5, 2, 4, 6)))
If you don't want to hard-code the list passed to index.cond, you can use something like:
index.cond=list(c(matrix(1:nlayers(s), ncol=2, byrow=TRUE)))
where the 2 indicates the number of rows you will have in your layout.
Of course you could also pass a stack with layers arranged in the desired row-wise plotting order, e.g.:
levelplot(s[[c(matrix(1:nlayers(s), ncol=2, byrow=TRUE))]], layout=c(3, 2))

Resources