Matti Viljamaa
2016-Sep-25 13:01 UTC
[R] curve() doesn't seem to use the whole range of x? And Error: longer object length is not a multiple of shorter object length
I?m trying to plot regression lines using curve() The way I do it is: bs <- coef(fit2) and then for example: curve(bs["(Intercept)"]+bs["mies"]*0+bs["kouluB"]+bs["lka"]*x+bs["kouluB:clka"]*clka, from=min(lka), to=max(lka), add=TRUE, col='red') This above code runs into error: Error in curve(bs["(Intercept)"] + bs["mies"] * 0 + bs["kouluB"] + bs["lka"] * : 'expr' did not evaluate to an object of length 'n' In addition: Warning message: In bs["(Intercept)"] + bs["mies"] * 0 + bs["kouluB"] + bs["lka"] * : longer object length is not a multiple of shorter object length Which I?ve investigated might be related to the lengths of the different objects being multiplied or summed. Taking length(g$x) or length(g$y) of g <- curve(bs["(Intercept)"]+bs["mies"]*0+bs["kouluB"]+bs["lka"]*x, from=min(lka), to=max(lka), add=TRUE, col='red') returns 101. However length(lka) is 375. But perhaps these being different is not the problem? I however do see that the whole range of lka is not plotted, for some reason. So how can I be sure that it passes through all x-values in lka? And i.e. that the lengths of objects inside curve() are correct? What can I do?
Matti Viljamaa
2016-Sep-25 13:10 UTC
[R] curve() doesn't seem to use the whole range of x? And Error: longer object length is not a multiple of shorter object length
Writing: bs["(Intercept)"]+bs["mies"]*0+bs["kouluB"]+bs["lka"]*lka+bs["kouluB:clka"]*clka i.e. without that being inside curve produces a vector of length 375. So now it seems that curve() is really skipping some lka-/x-values.> On 25 Sep 2016, at 16:01, Matti Viljamaa <mviljamaa at kapsi.fi> wrote: > > I?m trying to plot regression lines using curve() > > The way I do it is: > > bs <- coef(fit2) > > and then for example: > > curve(bs["(Intercept)"]+bs["mies"]*0+bs["kouluB"]+bs["lka"]*x+bs["kouluB:clka"]*clka, from=min(lka), to=max(lka), add=TRUE, col='red') > > This above code runs into error: > > Error in curve(bs["(Intercept)"] + bs["mies"] * 0 + bs["kouluB"] + bs["lka"] * : > 'expr' did not evaluate to an object of length 'n' > In addition: Warning message: > In bs["(Intercept)"] + bs["mies"] * 0 + bs["kouluB"] + bs["lka"] * : > longer object length is not a multiple of shorter object length > > Which I?ve investigated might be related to the lengths of the different objects being multiplied or summed. > Taking length(g$x) or length(g$y) of > > g <- curve(bs["(Intercept)"]+bs["mies"]*0+bs["kouluB"]+bs["lka"]*x, from=min(lka), to=max(lka), add=TRUE, col='red') > > returns 101. > > However length(lka) is 375. But perhaps these being different is not the problem? > > I however do see that the whole range of lka is not plotted, for some reason. So how can I be sure > that it passes through all x-values in lka? And i.e. that the lengths of objects inside curve() are correct? > > What can I do?
Duncan Murdoch
2016-Sep-25 15:30 UTC
[R] curve() doesn't seem to use the whole range of x? And Error: longer object length is not a multiple of shorter object length
On 25/09/2016 9:10 AM, Matti Viljamaa wrote:> Writing: > > bs["(Intercept)"]+bs["mies"]*0+bs["kouluB"]+bs["lka"]*lka+bs["kouluB:clka"]*clka > > i.e. without that being inside curve produces a vector of length 375. > > So now it seems that curve() is really skipping some lka-/x-values.How could curve() know what the length of lka is? You're telling it to set x to a sequence of values of length 101 (the default) from min(lka) to max(lka). You never tell it to set x to lka. curve() is designed to plot expressions or functions, not vectors. If you actually want to plot line segments using your original data, use lines(). (You'll likely need to sort your x values into increasing order if you do that, or you'll get a pretty ugly plot.) Duncan Murdoch> >> On 25 Sep 2016, at 16:01, Matti Viljamaa <mviljamaa at kapsi.fi> wrote: >> >> I?m trying to plot regression lines using curve() >> >> The way I do it is: >> >> bs <- coef(fit2) >> >> and then for example: >> >> curve(bs["(Intercept)"]+bs["mies"]*0+bs["kouluB"]+bs["lka"]*x+bs["kouluB:clka"]*clka, from=min(lka), to=max(lka), add=TRUE, col='red') >> >> This above code runs into error: >> >> Error in curve(bs["(Intercept)"] + bs["mies"] * 0 + bs["kouluB"] + bs["lka"] * : >> 'expr' did not evaluate to an object of length 'n' >> In addition: Warning message: >> In bs["(Intercept)"] + bs["mies"] * 0 + bs["kouluB"] + bs["lka"] * : >> longer object length is not a multiple of shorter object length >> >> Which I?ve investigated might be related to the lengths of the different objects being multiplied or summed. >> Taking length(g$x) or length(g$y) of >> >> g <- curve(bs["(Intercept)"]+bs["mies"]*0+bs["kouluB"]+bs["lka"]*x, from=min(lka), to=max(lka), add=TRUE, col='red') >> >> returns 101. >> >> However length(lka) is 375. But perhaps these being different is not the problem? >> >> I however do see that the whole range of lka is not plotted, for some reason. So how can I be sure >> that it passes through all x-values in lka? And i.e. that the lengths of objects inside curve() are correct? >> >> What can I do? > > ______________________________________________ > 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. >
Greg Snow
2016-Sep-26 18:52 UTC
[R] curve() doesn't seem to use the whole range of x? And Error: longer object length is not a multiple of shorter object length
If your goal is to visualize the predicted curve from an lm fit (or other model fit) then you may want to look at the Predict.Plot and TkPredict functions from the TeachingDemos package. On Sun, Sep 25, 2016 at 7:01 AM, Matti Viljamaa <mviljamaa at kapsi.fi> wrote:> I?m trying to plot regression lines using curve() > > The way I do it is: > > bs <- coef(fit2) > > and then for example: > > curve(bs["(Intercept)"]+bs["mies"]*0+bs["kouluB"]+bs["lka"]*x+bs["kouluB:clka"]*clka, from=min(lka), to=max(lka), add=TRUE, col='red') > > This above code runs into error: > > Error in curve(bs["(Intercept)"] + bs["mies"] * 0 + bs["kouluB"] + bs["lka"] * : > 'expr' did not evaluate to an object of length 'n' > In addition: Warning message: > In bs["(Intercept)"] + bs["mies"] * 0 + bs["kouluB"] + bs["lka"] * : > longer object length is not a multiple of shorter object length > > Which I?ve investigated might be related to the lengths of the different objects being multiplied or summed. > Taking length(g$x) or length(g$y) of > > g <- curve(bs["(Intercept)"]+bs["mies"]*0+bs["kouluB"]+bs["lka"]*x, from=min(lka), to=max(lka), add=TRUE, col='red') > > returns 101. > > However length(lka) is 375. But perhaps these being different is not the problem? > > I however do see that the whole range of lka is not plotted, for some reason. So how can I be sure > that it passes through all x-values in lka? And i.e. that the lengths of objects inside curve() are correct? > > What can I do? > ______________________________________________ > 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.-- Gregory (Greg) L. Snow Ph.D. 538280 at gmail.com