Description Usage Arguments Value Author(s) Examples
This function receives a matrix after the averageSameXs function has been used on it and calculates the relative weight of each observation by dividing its weight by the total weights. If at least one observation doesn't have a weight specified the function assumes all the observations are of equal weight
1 | calcWeights(mat)
|
mat |
a matrix after the averageSameXs function has been used on it |
A matrix with the updated weights column
Tal Carmi, Liat Gaziel
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | d <- c(1,1,3,4)
e <- c(5,6,7,8)
f <- c(1,1,1,1)
mydata <- data.frame(d,e,f)
names(mydata) <- c("X","Y","Weight")
mydata<-averageSameXs(mydata)
calcWeights(mydata)
## The function is currently defined as
function (mat)
{
n <- size(mat)
zeroWeight <- 0
sumWeight <- 0
for (i in 1:n) {
if (is.null(mat[i, 3]) || is.na(mat[i, 3]) || mat[i,
3] == 0) {
zeroWeight <- 1
}
sumWeight <- sumWeight + mat[i, 3]
}
if (zeroWeight == 1) {
mat[3] = 1/n
return(mat)
}
for (i in 1:n) {
mat[i, 3] <- mat[i, 3]/sumWeight
}
return(mat)
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.