#A1 <- as.matrix(cbind(Intercept=1, A))

#nnls.mod <- nnls(A1, b)

#ESS <- sum((nnls.mod$fitted – mean(b))^2)

#RSS <- sum(nnls.mod$residuals^2)

#ESS/(ESS+RSS)

# [1] 0.9601099 ]]>

By % in my question I meant that in the end I would like to have a contribution of each of the input variables summing up to 1 (100%). Or better yet, contribution of each of them in % and the unexplained/error in % as well all of which summing to 1.

When doing that you proposed I get ESS/(ESS+RSS) of 0.96 (i.e. 96%), so it is pretty good but a sum of all coeffficients is greater than 1. Is this expected? How should I interpret this so that I get an answer I wanted to begin with?

> A1 nnls.mod ESS RSS ESS/(ESS+RSS)

[1] 0.9601099

sum(coef(nnls.mod))

[1] 1.011154

P.S. In case it matters: Sum of b is 1 and sum of all factors is 1 as well. It is implied that the observed variable b is formed by addition of all factors in different quantity.

> head(A1)

Intercept fact.1 fact.2 fact.3 fact.4 fact.5 fact.6

A 1 0.011098326 6.827082e-04 0.02217231 0.0365 0.014941548 0.0017

B 1 0.009149341 6.191072e-04 0.01787168 0.0309 0.008960918 0.0028

C 1 0.001490070 9.927896e-05 0.00213834 0.0183 0.002207846 0.0005

D 1 0.006233885 3.238914e-04 0.01626515 0.0243 0.009206905 0.0019

E 1 0.001801068 2.634810e-04 0.02400262 0.0097 0.011671022 0.0013

F 1 0.002580909 2.698660e-04 0.01216030 0.0054 0.007292089 0.0012

Many thanks, Marcin

]]>

nnls.mod < - nnls(A, b)

ESS < - sum((nnls.mod$fitted - mean(b))^2)

RSS < - sum(nnls.mod$residuals^2)

The percent of variance explained by the model is therefore ESS/(ESS+RSS); this assumes you have an intercept (constant column of 1s) in your independent matrix A. There's more detail on Wikipedia.

]]>Many thanks, Marcin

]]>The following `from` values were not present in `x`: col, color, pch, cex, lty, lwd, srt, adj, bg, fg, min, max

Any ideas?

]]>I just found a workaround. I just modified the

df % mutate(cs1=cumsum(value))

to

df % mutate(cs1=round(cumsum(value),2))

and now it works! ]]>

The later might be better for float values as, with rounding, your final_value might not equal 0 exactly. Additionally I would rewrite the if statement to use the testthat package and an expect statement. That would let you set an explicit epsilon (or error tolerance) for checking that decimal values add to 0.

]]>I am trying to produce my own waterfall plot, but unfortunately I get the

` error in sprintf("Final value doesn't return to 0. %.2d instead.", final_value):`

invalid format '%.2d'; use format %f, %e, %g or %a for numeric objects

Called from: sprintf("Final value doesn't return to 0. %.2d instead.", final_value)

Does that mean I am not allowed to use decimal numbers!?!? That would be strange…

My value vector ** df$value** looks like this:

The plotting is easy: it was done with ggplot2. The data frame contains columns for x1, x2, dataset, choice and so the code is something like:

`ggplot(data, aes(x=x1, y=x2)) + geom_point(aes(colour=choice, size=dataset))`