Adrian
Adrian

Reputation: 9793

Error when plotting natural cubic spline with knots

library(splines)
set.seed(3)
x <- rnorm(100)
plot(x)
lines(ns(x))

This works just fine, but if I try to add specific knots, i.e.

lines(ns(x, knots = c(1, 2, 3, 50)))

Running the above code gives me the following error:

Error in qr.default(t(const)) : 
  NA/NaN/Inf in foreign function call (arg 1)

Edit:

As per Peter's suggestion, a knot at 50 is way to big, so I modify the code to:

> lines(ns(x, knots = sort(x, decreasing = TRUE)[1:10]))
Error in qr.default(t(const)) : 
      NA/NaN/Inf in foreign function call (arg 1)

Now I want to place 10 knots at the 10 highest values of x. But I'm still getting an error? Why is that?

Upvotes: 1

Views: 673

Answers (1)

Peter Flom
Peter Flom

Reputation: 2388

50 is way too big. x is a normal with mean 0 and sd 1. So, e.g

lines(ns(x, knots = c(1, 1.5)))

works fine. (One more example of an unhelpful R error message).

Upvotes: 1

Related Questions