1

Dataset:

Distance <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,15 ,15 ,15 ,15 ,15 ,15 ,15 ,15 ,15 ,15 ,15 ,15, 15 ,15, 15 ,15 ,15 ,15,15 ,15 ,20, 20 ,20, 20 ,20, 20 ,20, 20 ,20 ,20 ,20 ,20 ,20 ,20, 20 ,20, 20 ,20, 20 ,20, 20)
CHeight <- c(400,300,200,0,-200,-400,-600,-800,-1000,-1000,-1200,-1220,-1300 ,-1400,-1400,-1500,-1600,-1700,-1700,-1800,-1900,400,200,0,-200,-400,-600,-800,-1000,-1200,-1200,-1400,-1600,-1600,-1800,-2000,-2000,-2200,-2200,-2400,-2600,-2800,-3000,400,200,0,-200,-300,-400,-500,-600,-700,-800,-900,-1000,-1100,-1200,-1200,-1400,-1600,-1800,-1800,-2000,-2200,-2400,-2400,-2600,-2800,-3000,400,200,0,-200,-400,-600,-800,-1000,-1200,-1400,-1600,-1600,-1800,-2000,-2200,-2400,-2400,-2600,-2800,-3000,1000,800,600,400,200,0,-200,-400,-600,-800,-1000,-1200,-1400,-1600,-1800,-2000,-2200,-2400,-2600,-2800,-3000)
Value <- c(163301.080, 269704.110 ,334570.550, 409536.530, 433021.260 ,418962.060, 349554.460, 253987.570,124461.710, 140750.480  ,52612.790 , 54286.427  ,26150.025  ,14631.210  ,15780.244 ,  8053.618, 4402.581,   2251.137  , 2743.511 ,  1707.508 ,  1246.894 ,176232.060 ,270797.240 ,323096.710,333401.080, 311949.900 ,272821.770 ,189571.850 ,114263.560  ,51939.070  ,62578.665  ,36905.438,  22625.515,  22940.591  ,14576.295  , 9686.653 , 10344.214  , 6912.779  , 7092.919   ,5366.797, 4058.492,   3270.734  , 2528.644  ,89311.555 ,116698.175 ,143588.620 ,139203.190, 145399.445, 145635.715, 134671.110 ,128931.160 ,119734.835 ,108708.815 , 90221.955  ,81692.585 , 64882.275,58215.735,  60443.190  ,44690.690 , 33224.152 , 24140.272 , 24913.280  ,19082.689  ,13920.669, 11074.718,  10015.653   ,8743.850  , 7516.880 ,  6377.743 , 36888.842  ,43088.720  ,47904.490, 51298.710,  51120.887  ,47687.488  ,42238.912 , 38563.007 , 33902.918  ,28565.303  ,23700.862,24818.393,  21620.129 , 17816.061  ,15377.097 , 12992.321  ,12985.911  ,11177.941   ,9536.621,8357.279,  13052.178 , 14325.789  ,15120.314 , 16227.575  ,17226.307  ,18557.270  ,18680.326, 18844.544,  18205.607,  17770.311  ,16605.438 , 16062.309  ,14785.654  ,14324.493  ,13373.627,12135.392,  10632.699,   9155.762  , 8240.951,   6934.240  , 6475.927)
Factor1 <- c(0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1)
Factor2 <- c(0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1)
Factor3 <- c(0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1)

Frame=data.frame(Distance, CHeight, Value, Factor1, Factor2, Factor3)

I have a function

mainfunction<-function(CHeight,Distance,a,b,f,g,k){
  (a * exp(-((Height + b)/2 * g)^2) - (k * Distance)) + a * 
   exp(-((CHeight + b)/(2 * g + f * Distance))^2) * (1 - exp(-k * Distance))
}

And I have an xyplot like:

library(lattice)
xyplot(Value ~ CHeight | Distance*Factor1, 
       data=Frame[Frame$Factor2==1 & Frame$Factor3==1,])

Now I would like to put some lines on the matrix scatterplot

lines(x, mainfunction(x,10,40000,0,1/500,1/500,0))

Which doesn't work, so any help regarding that would be great. But in a perfect world I would like to be able to put in a separate sequence. To make the value for y a sequence as well.

So something like

Distance <- seq(0,20,5)

So in short I essentially want to be able to change the value of parameters in the function and visually see how they impact the fit of the data via lines on a scatterplot matrix.

5
  • Are you set on using lattice (please add the tag if that's the case)? Also, please make your example reproducible (i.e., provide an example of data, see these instructions). Commented May 11, 2015 at 8:27
  • Changed everything, I think it's reproducible now. Commented May 11, 2015 at 8:41
  • It's better now. Are you sure you don't want to fit your function to the data? Unfortunately I can't help you with lattice, because I'm mainly a ggplot2 user. Commented May 11, 2015 at 8:56
  • not too fussed with lattice or ggplot2, just want something that can graph the plots and lines at this stage for fitting later on, sorry about the sloppyness earlier,it's been a long day. Regards Commented May 11, 2015 at 9:25
  • Produce a grid of Distance and CHeight values (expand.grid is useful here). Evaluate your function for these values. Put everything in a data.frame and plot. Commented May 11, 2015 at 10:02

1 Answer 1

1

Typo in mainfunction, you have a Height instead of CHeight.

lines won't work in lattice. This will plot them, using panel.lines:

    xyplot(Value ~ CHeight | Distance*Factor1, 
           data=Frame[Frame$Factor2==1 & Frame$Factor3==1,]
    ,panel=function(x,y,...){
         panel.xyplot(x,y,...)
         panel.lines(x, mainfunction(x,10,40000,0,1/500,1/500,0))
})

I guess then you need to make them line up.

UPDATE:

If I understand your comment, you would like the line drawn in each panel to use the appropriate value for Distance rather than a fixed value of 10. One way is to use the subscripts argument to the panel function, which gives the row numbers of the original data that are used in each panel. With that, you can look up the value of Distance for those rows and use in mainfunction. For example:

    xyplot(Value ~ CHeight | Distance*Factor1, 
           data=Frame[Frame$Factor2==1 & Frame$Factor3==1,]
    ,panel=function(x,y,subscripts,...){
         panel.xyplot(x,y,...)
         D =Frame[Frame$Factor2==1 & Frame$Factor3==1,]$Distance[subscripts[1]]
        panel.text(0,0,D) #diagnostic to show D for each panel
         panel.lines(x, mainfunction(x,D,40000,0,1/500,1/500,0))
})

Here I set D to the value of Distance for the current subset and use that in mainfunction. I also printed it out just to show it is different in each panel. The curves all look the same to me, but if for example you set k to 10, you get some differences.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, would you happen to know if there's a way i could make the lines for the Distance component show up automatically? So i can compare the matrix of scatter plots to my function across distance and height at the same time?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.