> On Jun 7, 2018, at 7:18 AM, Veerappa Chetty <chettyvk at gmail.com>
wrote:
>
> I use solve(A,b) inside my function, myfun2; it works fine when I return
> one value or a list.
> I want use the return values in ggplot as below: ggplot(data.frame(
> x=c(0.1,0.8)),aes(x=x))+stat_function(fun=myfun.2,geom="line")
> I get a blank graph. Would greatly appreciate help! Thanks.
>
> Here are my codes:
> p.lm<-0.05 ##to initialze only
> p.lh<-0.1
> p.ll<-1-p.lm-p.lh
> p.ml<-0.3
> p.mh<-0.1
> p.mm<-1-p.ml-p.mh
> p.hl<-0.05
> p.hm<-0.5
> p.hh<-1-p.hl-p.hm
> myfun.5<-function(xvar){
> y<-numeric(2)
> p.lm<-xvar
>
A<-matrix(c(p.lm+p.lh+p.hl,p.hl-p.ml,p.hm-p.lm,p.ml+p.mh+p.hm),nrow=2,byrow
> = TRUE)
> b<-c(p.hl,p.hm)
> y<-solve(A,b)
> z<-list(y[1],y[2],1-y[1]-y[2])
> z[1]
> }
>
> g.2<-ggplot(data.frame(
> x=c(0.1,0.8)),aes(x=x))+stat_function(fun=myfun.5,geom="line")
> g.2
You are a) failing to pay attention to the warning message:
> g.2
Warning message:
Computation failed in `stat_function()`:
'a' (2 x 102) must be square
>
.... and b) as a consequence failing to debug your function. Add a print(A) line
immediately after your construction of A:
myfun.5(1:10)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
[1,] 1.15 2.15 3.15 4.15 5.15 6.15 7.15 8.15 9.15 10.15 -0.25
[2,] -0.50 -1.50 -2.50 -3.50 -4.50 -5.50 -6.50 -7.50 -8.50 -9.50 0.90
Error in solve.default(A, b) : 'a' (2 x 11) must be square
The fun argument in stat_fun is supposed to accept a vector with x values and
return y values for "predictions". You have not indicated what plot
was expected, You've given no indication what the various constants are
supposed to represent, and you are only giving x-values to aes.ggplot, so
I'm unable to infer what is intended.
> --
> Professor of Family Medicine
> Boston University
> Tel: 617-414-6221, Fax:617-414-3345
> emails: chettyvk at gmail.com,vchetty at bu.edu
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
David Winsemius
Alameda, CA, USA
'Any technology distinguishable from magic is insufficiently advanced.'
-Gehm's Corollary to Clarke's Third Law