[R] lattice add a fit
Duncan Mackay
dulcalma at bigpond.com
Wed Oct 8 06:15:23 CEST 2014
Hi Stephen
if you want and uncomplicated graph you can do the following
(data is from
https://stat.ethz.ch/pipermail/r-help/2008-December/181887.html
which has an example of smoothing with a panel function a (follow the
thread))
May be un necessary for what you want
dput(test)
structure(list(x = 1:200, y = c(-0.211833603383945, -1.0415911337336,
-1.1533075572692, 0.321531497643745, -1.5001298759544, -0.445533260997329,
1.73404543257509, 0.511295615763455, 0.0996450365533331,
-0.0578911103962697,
-1.74278762900362, -1.32495298319389, -0.547933881534948, -1.45638427585301,
0.0826868204409038, 0.927578947155782, -0.716769334937502,
0.962399675187835,
1.54588458200656, -1.00976361360517, 0.557410853590579, 0.168781513971099,
0.155258858301712, 2.36776473870934, -1.58564441351633, -1.10353350945666,
0.902787057776989, 0.0683183515567076, -1.14946795040125, -0.90127498047004,
-1.19845352398361, -0.510163823550385, 0.306246343638814,
-0.0678770276660271,
0.372202425855059, 1.14449122171645, -1.43961215364919, -0.745407767319761,
-1.08426028148107, -0.911810459312264, 0.254473860506602,
0.0545428435691033,
0.86939682821168, -0.495822138795001, 0.788983956205307,
-0.0482778318087694,
0.28444147880148, -2.3449130823549, 0.352139869980497, -0.73670929392907,
0.152865554816491, 0.466635540442577, -1.34862166637197, -0.371330904577685,
2.04819500702545, -1.03035368989461, -0.808913708097202, -0.899512468883577,
1.32164919515955, -1.90258984438615, -1.43516196895465, 0.0465901079414443,
-0.050190824605222, -1.28753166678936, 1.15411244955901, -1.37573809203985,
-1.14829872319585, 1.41625774885229, 0.105763612116132, -0.384659376665841,
1.78448216497794, -1.1152932761564, -0.0721496590810866, -0.654929692334926,
1.23475033091273, -0.355004059316614, 0.0242929525502168, -1.12014689112359,
0.131516716053627, -1.01789380466415, 1.54412508731677, -0.612498366500995,
-1.47544725093144, -1.33884056677665, 0.116000294553631, 0.441007318370771,
0.253328166388004, -0.601324732898278, -0.681872432317887,
-0.638406595370473,
1.1255146487638, 1.68030106840915, -0.0458152886664351, -0.359702695597414,
-1.27276961899765, 1.65150890035352, 0.970733803202561, -0.299164223313285,
0.0863388612193544, -1.56778219134085, -0.934175135343763,
-1.75098468827598,
-0.350775124642839, -0.497774672576354, 0.771324636489948,
-0.69490617657808,
-0.776286103585292, 0.0401278987521509, -0.965161644356313,
-0.443562752383677,
0.482231363899495, 0.299362730723139, -1.13975716906454, 0.750123380303915,
-0.346046820684503, -0.298660657494398, 1.15743160079942, 1.37831825909263,
-0.561195292140498, -0.727441974730694, 0.542558262494051, 1.07423749905296,
-0.671511488353386, -0.140489880785358, 1.46724871140033, 0.563360651277434,
1.24069952075524, -0.253423940974, 0.196985403588085, -1.29263886475009,
-0.700235249467159, 0.386254280503009, -0.489119591847831,
-0.863120570714887,
0.21167415527797, -0.104722187456112, 0.885271725582058, -1.33715921875481,
-0.419738882938569, 0.114904893946579, 0.579793678083023, 1.3375478692142,
0.00553313645797594, -1.0480172540963, -0.293082520385054,
-0.607995018773811,
0.22269595909434, 0.404577306401601, 0.212081041192621, -1.28370971577478,
-0.186741150561225, 1.47504624554164, -0.335669248934062,
-0.0752650377217833,
0.938248892501711, -1.19915518471386, -0.823130111133665, 1.77585103279627,
-0.726506122852284, -0.854585428953646, 0.18721613856176, 0.664751685525484,
-0.378660174874064, -0.406279864316906, -0.377953114167246,
-0.578277117855972,
-0.195896309465962, -0.752879703028692, -1.20718148517937,
0.103036243241138,
0.707569503031958, 0.488185644365067, -0.674354094380548, 1.45780393551308,
-0.426823625045982, 0.372084624745127, -0.489364737649147, 2.00664228219939,
0.0918083578741706, -0.563102471737825, -0.0372326923340063,
1.00352069476112, -1.14244200325742, 1.14469060546702, 1.26070182057624,
0.664616631427959, 0.75021683374034, -1.39029533734557, 0.818844192795068,
-0.948663137610259, 2.87434018113975, 0.237475434622456, 0.745884070517981,
1.19494964206119, 0.627396315033843, -0.810728351760724, 1.58246498427057,
-0.414333063166778, -1.91414027760821, -1.68038800516161), groups =
structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("1", "2", "3", "4"), class =
"factor")), .Names = c("x",
"y", "groups"), row.names = c(NA, -200L), class = "data.frame")
xyplot(y~x|groups, test, type = c("p","smooth"), span = 0.8, col.line="red")
span and col.line are not the default values
You will have to make your own panel function for locfit if you want to use
it
I have done it in the past - read the help for
library(locfit)
?plot.locfit
and the links
?lattice::prepanel
Regards
Duncan
Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: mackay at northnet.com.au
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Bond, Stephen
Sent: Tuesday, 7 October 2014 23:02
To: r-help at R-project.org
Subject: [R] lattice add a fit
What is the way to add an arbitrary fit from a model to a lattice
conditioning plot ?
For example
xyplot(v1 ~v2 | v3,data=mydata,
panel=function(...){
panel.xyplot(...)
panel.loess(...,col.line="red")
}
)
Will add a loess smoother. Instead, I want to put a fit from lm (but not a
simple straight line) and the fit has to be done for each panel separately,
not one fit for the full data set, so sth like an lm equivalent of
panel.locfit (there is no panel.lmfit)
Thank you.
Stephen B
[[alternative HTML version deleted]]
______________________________________________
R-help at r-project.org mailing list
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.
More information about the R-help
mailing list