I have a vector named y which has n numerical elements and a matrix n*n named w where its elements are numbers. I want to use the below formula for the above data:
I have written the following code in R using functions colSums and sum:
dy<-y-mean(y)
n*(y-mean(y))*colSums(w*dy)/sum(dy^2)#=local[,1]
Now, I want to change my above code to write it with for loop (without using colSums and sum). Indeed, I want to make the formula using for loop instead of using colSums and sum.
Thank you in advance for your help.
I don't know why you want to use a for-loop since in this case there are many disadvantages, but here we go.
First we calculate the donominator:
dy_square_sum <- 0
for (i in seq_along(y)) {
dy_square_sum <- dy_square_sum + dy[i]^2
}
In the next step, we calculate the enumerator and build up your desired output:
weighted_sum <- rep(0,n)
output <- rep(0, n)
for (i in seq_along(y)) {
for (j in seq_along(y)) {
weighted_sum[i] <- weighted_sum[i] + w[j,i] * dy[j]
}
output[i] <- n * dy[i] / dy_square_sum * weighted_sum[i]
}
or a little simplified but less efficient
output <- rep(0, n)
for (i in seq_along(y)) {
for (j in seq_along(y)) {
output[i] <- output[i] + n * dy[i] * w[j,i] * dy[j] / dy_square_sum
}
}
Related
I try to calculate the value for the following equation in R.
I have the dataset and the value for each corresponding F_x, F_{x+1}...
However, as both Q and s have too many values, I am considering write a loop in loop. It's bit confused. As a loop for Q seems conflicting as the loop for s
But if I write loop as below, seems like I need to by hand calculate Q 100 times to get all of the answer. Also my loop seems to be wrong...How can I fix this problem? Thank you so much
Y <- function(x,s, Q){
n <- length(s-x)-1
Q <- c(1:100)
for(s in seq(1:n)){
Y[s] <- sum(s*Q[s]*cumprod(Fx[1:s]))
}
return(Y)
}
I am not sure if the code below reaches your objective
Y <- function(x,s) {
Q <- 1:100
S <- 1:(s-x)
outer(Q,S,FUN = function(q,s) q * sum(c(1:s) * cumprod(Fx[1:s])))
}
for loop version
Y <- function(x,s) {
nr <- 100
nc <- s-x
y <- matrix(nr*nc,nrow = nr)
for (Q in 1:nr) {
for (S in 1:nc) {
y[Q,S] <- Q * sum(c(1:S) * cumprod(Fx[1:S]))
}
}
y
}
I would like to store result of X[i,j].
X[i,j] = alpha[i] + beta [j]
I tried writing this double for loop but could not get it to store the result. Appreciate any help here. Thanks!
for (i in length(alpha)) {
for (j in length(beta)) {
Xij <- alpha[i] + beta[j]
matrix[i,j] <- Xij
}
}
Edit: Is there a more efficient way to do this? The for loop run is taking a long time as the dataset is huge.
If the loop is providing poor performance, you should try the outer statement:
outer(alpha, beta, FUN = `+`)
for (i in 1:length(alpha)) {
for (j in 1:length(beta)) {
Xij <- alpha[i] + beta[j]
matrix[i,j] <- Xij
}
}
I am trying to implement a nested summation in R. The for loop implementation is:
sum = 0
for(i in 1:n){
for(j in 1:n){
for(k in 1:n){
sum = sum + w[i,j]*w[j,k]
}
}
}
where w is a symmetric square matrix and n is the number of rows (or columns).
Please see the formula I am trying to implement. (SO did not allow me to write Latex nor add the image here.)
The nested for loops above takes forever. How do I implement this efficiently the R way?
Try this:
Sum2 <- sum(w %*% w)
all.equal(Sum, Sum2)
## [1] TRUE
Note
We used for comparison:
# input
set.seed(123)
n <- 5
w <- matrix(rnorm(n^2), n)
# from question
Sum = 0
for(i in 1:n){
for(j in 1:n){
for(k in 1:n){
Sum = Sum + w[i,j]*w[j,k]
}
}
}
I'm trying wherever possible to replace my for loops with apply / map functions
However I am stuck when it comes to times where I need to use the loop index as a position. This is easy to do with a for loop
Take the following code, I use the index i in both the left hand and the right hand side of the assignment:
score <- function(x) {
n <- length(x)
right <- x
for(i in 1:n) {
right[i] <- (n - x[i] + 1) / (i * n)
}
(1 / n) * sum(right)
}
score(c(2,1,3))
how do i rewrite the above using map or apply functions?
You could use this:
x = c(2,1,3)
n = length(x)
(1/n) * sum(sapply(1:n, function(i) (n - x[i] + 1)/(i*n) ))
We could vectorize this
v1 <- c(2, 1, 3)
n <- length(v1)
(1/n) *sum((n - v1 + 1)/(seq_along(v1) * n))
#[1] 0.4259259
I've been having a problem in
sum <- (abs(x[i, i] - x[i, j])^p) ^ (1/p)
I want it to be for example x[1,1] ,x[2,2] etc. So I thought x[i,i] in the for loop would do the job, but it's only giving me back 0 as a result.
example <- function (x,p) {
sum <- 0
for (i in 1:ncol(x)) {
for (j in i:nrow(x)){
sum<-(abs(x[i,i] - x[i,j])^p) ^ (1/p)
}
}
return (sum)
}
#x is a matrix
You are replacing sum each time you loop through,
sum <- sum + (abs(x[i,i] - x[i,j])^p) ^ (1/p) should do.