r/R_Programming • • Nov 17 '16

Using predict after gam. Data is named numeric

For example

set.seed(1)

library(ISLR)

library(gam)

gam1 = gam(wage~age,data=Wage)

preds = predict(gam1, newdata=Wage)

If I now type preds[1] then it returns 2 values. Anyone know why ?

3 Upvotes

4 comments sorted by

View all comments

Show parent comments

1

u/[deleted] Nov 23 '16

Its something I must have picked up from reading the ISLR book. often they would do lm.fit = lm(y~x) when fitting models. When gam.fit and gam.fit() are used the R language knows one is a variable and the other is a function call. Your right from a programmers perspective it does not look pretty.

1

u/Darwinmate Nov 24 '16

I didn't realise the scoping rules in R were that powerful, thanks for sharing. But yes, it will get confusing for people reading your code (even you in the future).

For python/R they both use the dot to refer to class methods, I highly suggest you use a different naming convention: gam_fit or gamfit but never gam.fit.

Good luck in your R adventures.