Hi all, I am doing analysis which involves fitting a line on trellis plot. The factor is month. As you know a year has 12 months and I expect to get 12 lines one for each month. I am getting the following results which is different to my expectation. Am I have to do anything about the data? Any suggestion is welcome. Thanks. Call: lm(formula = curr_data[[tmin_col]] ~ curr_data[[year_col]] - 1 | curr_data[[month_col]]) Residuals: Min 1Q Median 3Q Max -5.0625 -0.7625 0.0375 0.9375 4.9375 Coefficients: (1 not defined because of singularities) Estimate Std. Error t value Pr(>|t|) (Intercept) 15.56254 0.01079 1442 <2e-16 *** curr_data[[year_col]] | curr_data[[month_col]]TRUE NA NA NA NA --- Signif. codes: 0 ?***? 0.001 ?**? 0.01 ?*? 0.05 ?.? 0.1 ? ? 1 Residual standard error: 1.327 on 15115 degrees of freedom (72 observations deleted due to missingness) Regards, Frederic. Frederic Ntirenganya Maseno University, African Maths Initiative, Kenya. Mobile:(+254)718492836 Email: fredo at aims.ac.za https://sites.google.com/a/aims.ac.za/fredo/ [[alternative HTML version deleted]]
On 25/03/2015 12:30, Frederic Ntirenganya wrote:> Hi all, > > I am doing analysis which involves fitting a line on trellis plot.But the commands below (or at least the output from them) are not plotting commands. The> factor is month. As you know a year has 12 months and I expect to get > 12 lines one for each month. I am getting the following results which > is different to my expectation. Am I have to do anything about the > data? Any suggestion is welcome. Thanks. > > > Call: > lm(formula = curr_data[[tmin_col]] ~ curr_data[[year_col]] - > 1 | curr_data[[month_col]])It would be simpler to say lm(tmin_col ~ year_col - 1 | month_col, data = curr_data) What did you think this was going to do? Is there any mention in the help for lm that you can use the | here? It would also be helpful to see the result of str(curr_data) as we do not know what year_col is and we need to be sure that month_col is indeed a factor. I am sure calendar experts on this list are readying themselves to question your assertion that all calendars have twelve months but that is rather off-topic.> > Residuals: > Min 1Q Median 3Q Max > -5.0625 -0.7625 0.0375 0.9375 4.9375 > > Coefficients: (1 not defined because of singularities) > Estimate Std. > Error t value Pr(>|t|) > (Intercept) 15.56254 > 0.01079 1442 <2e-16 *** > curr_data[[year_col]] | curr_data[[month_col]]TRUE NA > NA NA NA > --- > Signif. codes: 0 ?***? 0.001 ?**? 0.01 ?*? 0.05 ?.? 0.1 ? ? 1 > > Residual standard error: 1.327 on 15115 degrees of freedom > (72 observations deleted due to missingness) > > Regards, > > Frederic. > > > Frederic Ntirenganya > Maseno University, > African Maths Initiative, > Kenya. > Mobile:(+254)718492836 > Email: fredo at aims.ac.za > https://sites.google.com/a/aims.ac.za/fredo/ > > [[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. > > ----- > No virus found in this message. > Checked by AVG - www.avg.com > Version: 2015.0.5751 / Virus Database: 4315/9378 - Release Date: 03/25/15 >-- Michael http://www.dewey.myzen.co.uk
> Call: > lm(formula = curr_data[[tmin_col]] ~ curr_data[[year_col]] - > 1 | curr_data[[month_col]])First, this is not a sensible formula for lm; lm() does not use '|' to denote grouping. It would be a valid formula for xyplot, in which | specifies grouping variables. In lm(), '|' is simply being treated as 'or', which is why one of your coefficients is for ' curr_data[[year_col]] | curr_data[[month_col]]TRUE' Second, you should not normally need things like curr_data[[month_col]], either in lm or xyplot. If curr_data is a data frame, things like lm(tmin ~ year, data=curr_data) should work. Third, 'nested' models in lm use the nesting operator '/', not '|'. So if you want 12 lines with separate intercept and gradient from an lm, you need (with month as a factor and the default intercept suppressed) lm(tmin~month+year/month -1, data=curr_data) #-1 suppresses the intercept and provides a zero-based intercept for each month This gives you 12 'month' intercepts and one gradient per month. If you wanted a common intercept you'd do lm(tmin~ year/month, data=curr_data) But beware; the coefficients in both cases cannot be interpreted as a simple gradient and intercept for each month: if I recall correctly, the gradients for month2 and on are modelled as an additive increment on the first month gradient. Use predict() if you want an easy way to predict a value for a given (possibly fractional) time of year and 'month'. [Incidentally I don?t immediately see why that is a sensible thing to do - this fits a monthly summary against a numeric year. But I'm going to assume you know what you want there.] Finally, though, this model will not help you much with lattice as there's no _simple_ way of putting those lines on different panels in a lattice plot. If you just want a line on each of 12 panels, that's much easier. You can use the panel() function with panel.lmline to put it there. For example, if you want to plot a line over the data, use xyplot(tmin~year|month, curr_data, panel=function(x, y, ...) { panel.xyplot(x, y, ...) panel.lmline(x, y, ...) } ) S Ellison ******************************************************************* This email and any attachments are confidential. Any use, copying or disclosure other than by the intended recipient is unauthorised. If you have received this message in error, please notify the sender immediately via +44(0)20 8943 7000 or notify postmaster at lgcgroup.com and delete this message and any copies from your computer and network. LGC Limited. Registered in England 2991879. Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UK
Thanks for the help. Frederic Ntirenganya Maseno University, African Maths Initiative, Kenya. Mobile:(+254)718492836 Email: fredo at aims.ac.za https://sites.google.com/a/aims.ac.za/fredo/ On Wed, Mar 25, 2015 at 4:48 PM, S Ellison <S.Ellison at lgcgroup.com> wrote:> > > Call: > > lm(formula = curr_data[[tmin_col]] ~ curr_data[[year_col]] - > > 1 | curr_data[[month_col]]) > > > First, this is not a sensible formula for lm; lm() does not use '|' to > denote grouping. It would be a valid formula for xyplot, in which | > specifies grouping variables. In lm(), '|' is simply being treated as 'or', > which is why one of your coefficients is for ' curr_data[[year_col]] | > curr_data[[month_col]]TRUE' > > Second, you should not normally need things like curr_data[[month_col]], > either in lm or xyplot. If curr_data is a data frame, things like > > lm(tmin ~ year, data=curr_data) > > should work. > > Third, 'nested' models in lm use the nesting operator '/', not '|'. So if > you want 12 lines with separate intercept and gradient from an lm, you need > (with month as a factor and the default intercept suppressed) > lm(tmin~month+year/month -1, data=curr_data) #-1 suppresses the intercept > and provides a zero-based intercept for each month > > This gives you 12 'month' intercepts and one gradient per month. If you > wanted a common intercept you'd do > lm(tmin~ year/month, data=curr_data) > But beware; the coefficients in both cases cannot be interpreted as a > simple gradient and intercept for each month: if I recall correctly, the > gradients for month2 and on are modelled as an additive increment on the > first month gradient. Use predict() if you want an easy way to predict a > value for a given (possibly fractional) time of year and 'month'. > [Incidentally I don?t immediately see why that is a sensible thing to do - > this fits a monthly summary against a numeric year. But I'm going to assume > you know what you want there.] > > Finally, though, this model will not help you much with lattice as there's > no _simple_ way of putting those lines on different panels in a lattice > plot. If you just want a line on each of 12 panels, that's much easier. You > can use the panel() function with panel.lmline to put it there. For > example, if you want to plot a line over the data, use > > xyplot(tmin~year|month, curr_data, > panel=function(x, y, ...) { > panel.xyplot(x, y, ...) > panel.lmline(x, y, ...) > } > ) > > > S Ellison > > > > > ******************************************************************* > This email and any attachments are confidential. Any u...{{dropped:18}}