Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8314745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:55:39+00:00 2026-06-08T20:55:39+00:00

I am fitting GAM models to data using the mgcv package in R. Some

  • 0

I am fitting GAM models to data using the mgcv package in R. Some of my predictors are circular, so I am using a periodic smoother. I run into an issue in cross validation where my holdout dataset can contain values outside the range of the training data. Since the gam package automatically chooses knots for the smooths, this leads to an error (see my related question here — thanks to @nograpes and @DWin for their explanations of the errors there).

How can I manually specify the outer knots in a periodic smooth?

Example code

The first block generates some data.

library(mgcv)

set.seed(223) # produces error.
# set.seed(123) # no error.

# generate data:
x <- runif(100,min=-pi,max=pi)
linPred <- 2*cos(x) # value of the linear predictor
theta <- 1 / (1 + exp(-linPred)) # 
y <- rbinom(100,1,theta)
plot(x,theta)
df <- data.frame(x=x,y=y)

The next block fits the GAM model with the periodic smooth:

gamFit <- gam(y ~ s(x,bs="cc",k=5),data=df,family=binomial())
summary(gamFit)
plot(gamFit)

It will be somewhere in the specification of the smoother term s(x,bs="cc",k=5) where I’m sure you’ll be able to set some knots, but this is not obvious to me from the help of gam or from googling.

This block will fit some holdout data and produce the error if you set the seed as above:

# predict y values for new data:
x.2 <- runif(100,min=-pi,max=pi)
df.2 <- data.frame(x=x.2)
predict(gamFit,newdata=df.2)

Ideally, I would only set the outer knots and let gam pick the rest.

Apologies if this question is better for CrossValidated than SO.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T20:55:40+00:00Added an answer on June 8, 2026 at 8:55 pm

    Try this:

    gamFit <- gam(y ~ s(x,bs="cc",k=5), 
                  knots=list( x=seq(-pi,pi, len=5) ), 
                  data=df, family=binomial())
    

    You will find a worked example at:

    ?smooth.construct.cr.smooth.spec 
    

    I learned in testing this code that the ‘k’ parameter in s() needs to match the ‘len’ parameter in the ‘x’-seq() value passed to knots(). I thought incorrectly that the knots argument would get passed to s().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing some data fitting using the pyminuit Python bindings for the minuit
I have some time-series data that I'm fitting a loess curve in ggplot2, as
I have written a model that I am fitting to data using ML via
Apologies if this is better suited in CrossValidated. I am fitting GAM models to
I'm fitting NURBS surfaces onto some data points in the form of patches. I
I'm fitting least squares models with the same predictor and a large number of
I am writing an automated curve fitting routine for 2D data based on scipy's
Where is the most fitting place for security and roles authorization to fit into
I'm trying to do some fitting with lsqcurvefit. I have a function like that:
The curve-fitting problem for 2D data is well known (LOWESS, etc.) but given a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.