JodeCharger100
JodeCharger100

Reputation: 1059

Weighted Average of several columns using predetermined weights

Assuming I have:

weights <- c(0.15, 0.25, 0.11, 0.9, 0.35, 0.05)

And the following datatable Phones :

make     model    price    users    rating    continent    market       years   success
Nokia     3310    800       5000       5       Europe     4000000        30        yes
Huawei    Foto    500      10000       7       Asia       1200000        10       no
Apple     XS      1500     90000       8       NAmerica   4200000         8        yes
Mi        125     300        500       5       Asia        300000         3        yes

I want to add a new column called Impact, which is the weight multiplied by columns price, users, rating, market, and years

So far, I am able to take the mean of the columns using:

Phones$wt <- rowMeans(subset(Phones, select = c(price, users, rating, market, years)), na.rm = TRUE)

So, I want to do a weighted mean depending on weights I have manually chosen.

Upvotes: 0

Views: 101

Answers (2)

SteveM
SteveM

Reputation: 2301

Note that a weighed average across a criteria set with different ranges is pretty much meaningless. E.g. small percent differences in market swamps big percent differences in rating. You should normalize each column vector and then apply the weights. Also I'm assuming low price is better, so a normalization should be done over the inverse of those values. So your normalized matrix would look like this:

enter image description here

Upvotes: 0

IceCreamToucan
IceCreamToucan

Reputation: 28675

A weighted mean is the same as matrix multiplication except you additionally divide the result by the sum of the weights. You have 6 weights and 5 columns so I removed the last weight.

m <- as.matrix(subset(Phones, select = c(price, users, rating, market, years)))

weights <- c(0.15, 0.25, 0.11, 0.9, 0.35)

m %*% weights / sum(weights)

#           [,1]
# [1,] 2046239.2
# [2,]  615101.9
# [3,] 2160641.3
# [4,]  153506.6

Data used:

Phones <- data.table::fread('
make     model    price    users    rating    continent    market       years   success
Nokia     3310    800       5000       5       Europe     4000000        30        yes
Huawei    Foto    500      10000       7       Asia       1200000        10       no
Apple     XS      1500     90000       8       NAmerica   4200000         8        yes
Mi        125     300        500       5       Asia        300000         3        yes
')

Upvotes: 2

Related Questions