Giter Site home page Giter Site logo

Comments (5)

rmcelreath avatar rmcelreath commented on July 22, 2024

It's not that they require the order to be the same. It's that the data frame will not be defined with the correct number of rows, unless the first variable you define has the right length. Putting sd.growing.season, in your example, first define the correct length. Then R will fill out the single values to all rows.

You could also just make sure each variable is the right length:

dpred = data.frame(mean.growing.season = rep(11,100),
                   sd.growing.season = seq(0, 6, len = 100),
                   logArea = rep(mean(n$logArea),100))

from rethinking.

michaellevy avatar michaellevy commented on July 22, 2024

I think data.frame uses the longest supplied vector to determine the number of rows. Here it does get it right:

> dpred = data.frame(mean.growing.season = 11,
+                    sd.growing.season = seq(0, 6, len = 100),
+                    logArea = mean(n$logArea))
> nrow(dpred)
[1] 100

Also, explicitly defining the length of each column doesn't fix the issue:

Wrong order, implied length:

> m = lm(loglpc ~ mean.growing.season * sd.growing.season + log10(area), data = n)
> dpred = data.frame(mean.growing.season = 11,
+                    sd.growing.season = seq(0, 6, len = 100),
+                    logArea = mean(n$logArea))
> l = link(m, dpred)
> summary(apply(l, 2, mean))
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 -3.615  -3.112  -2.608  -2.608  -2.105  -1.601 

Right order, implied length:

> dpred = dpred[, 3:1]
> l = link(m, dpred)
> summary(apply(l, 2, mean))
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 -2.753  -2.628  -2.502  -2.502  -2.377  -2.251 

Wrong order, explicit length:

> m = lm(loglpc ~ mean.growing.season * sd.growing.season + log10(area), data = n)
> dpred = data.frame(mean.growing.season = rep(11, 100),
+                    sd.growing.season = seq(0, 6, len = 100),
+                    logArea = rep(mean(n$logArea), 100))
> l = link(m, dpred)
> summary(apply(l, 2, mean))
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 -3.640  -3.126  -2.612  -2.612  -2.098  -1.584 

from rethinking.

rmcelreath avatar rmcelreath commented on July 22, 2024

I misunderstood the original issue. Is this behavior that happens only with lm?

The link method for lm is not something I ever finished writing. I could didn't get it to behave right, and couldn't justify spending time on it.

from rethinking.

michaellevy avatar michaellevy commented on July 22, 2024

Yes, the issue seems to be constrained to link for lm's. Maybe add at least a warning to link when called with an lm?

from rethinking.

rmcelreath avatar rmcelreath commented on July 22, 2024

I should either fix it or disable it. Might be an easy fix. But bug seems to arise from the way I've hacked into the predict method. So will take a little detective work.

from rethinking.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.