1
votes

I have a problem with my R code about a multiple linear regression. First, I try to use the gam function but this gives me an error. Here is the code:

install.packages("nlme")
library("mgcv")
library("ggplot2")
#Import dataset
setwd("/Users/Sarah/Documents/Master T&O/Master 1/Statistics IV/Assignment 2 ")
weight_data = read.csv("WeightLossGroup190.csv", sep = "", dec = ".", header = TRUE)

#Name of used data
weight <- weight_data$weight  
date <- weight_data$date 
dayNr <- weight_data$dayNumber 
time <- weight_data$time 
#Check linearity 
gam1 <- gam(as.numeric(weight_data$weight) ~ s(as.numeric(weight_data$dayNumber)) + s(as.numeric(weight_data$time)))
summary(gam1)
plot.gam(gam1, se = FALSE, rug = TRUE, all.terms = TRUE)

This gives me the following error:

Error in smooth.construct.tp.smooth.spec(object, dk$data, dk$knots) : 
  A term has fewer unique covariate combinations than specified maximum degrees of freedom

Does anyone have an idea of what I'm doing wrong?

1
I googled the error message you posted, and I think this link will help you along.Paul Hiemstra
Your data doesn't have sufficient observations to support as many degrees of freedom as you are asking for. You need to set a smaller value of k than the default in at least one of the smoothers. Also, never use $ in formulas. gam has a data argument for a reason. Make sure that you variables are numeric before you pass them to gam.Roland
I tried this : gam1 <- gam(weight ~ s(dayNr2) + s(time2),k=2, data= weight_data) But then I get this error: Error in data[[txt]] : subscript out of boundsSarah.d
k is an argument of s and not of gam.Roland
You might need to show your data and updated code. The error means exactly what I've explained in my first comment.Roland

1 Answers

0
votes

You might want to try controlling the number of knots with "k = " in the gam.