I’m using RStudio and trying to fit my data to a thermal performance curve model using rTPC, and keep encountering this error when trying to fit the data:
Error in nls.multstart::nls_multstart(rate ~ beta_2012(temp = temp, a, :
There must be as many parameter starting bounds as there are parameters.Parameters to estimate: temp a b c d e
Parameters supplied as input data:
Number of parameters in start_lower: 5
Number of parameters in start_upper: 5If there are parameters listed as input data which should be estimated,
this can be caused by variables in the environment/data with the same
name as model parameters.
rm(list=ls())
bug <- read.csv("earwigtimes.csv", header=TRUE)
df <- bug
df$Temperature <- as.factor(df$Temperature)
head(df)
library(ggplot2)
#examine data structure
str(bug)
bug$Treatment <- as.factor(bug$Treatment)
summary(bug)
mean(bug$Time) #3.296052
var(bug$Time)
sd(bug$Time)
#check normality
par(row=c(1))
hist(bug$Time)
boxplot(bug$Time)
boxplot(Time ~ Treatment * Temperature, data = bug,
xlab = "Temperature",
ylab = "Time")
#Function to calculate the mean and sd
data_summary <- function(data, varname, groupnames){
require(plyr)
summary_func <- function(x, col){
c(mean = mean(x[[col]], na.rm=TRUE),
sd = sd(x[[col]], na.rm=TRUE))
}
data_sum<-ddply(data, groupnames, .fun=summary_func,
varname)
data_sum <- rename(data_sum, c("mean" = varname))
return(data_sum)
}
# Calculate mean and sd for each combination of Temperature and Treatment
bug2 <- data_summary(df, varname="Time", groupnames = c("Temperature", "Treatment"))
# Convert Time to a factor variable
bug2$Time <- as.factor(bug2$Time)
# Calculate standard deviation
bug2 <- ddply(df, .(Temperature, Treatment), summarise,
mean_time = mean(Time, na.rm=TRUE),
sd_time = sd(Time, na.rm=TRUE))
# Raw data plot with spline interpolation
pRaw <- ggplot(df, aes(x = Temperature, y = Time, group = Treatment, color = Treatment)) +
geom_point(alpha = 0.7, size = 1.5, position = position_dodge(0.2)) +
labs(title = "Activity per acclimation temperature", x = "Temperature (°C)", y = "Time to reach endpoint (seconds)") +
scale_color_manual(values = c('#BA2BE5','#2BE59E'), labels = c("Stable", "Variable")) +
theme_light()
print(pRaw)
#Plot of Means and Standard Error
pMeans <- ggplot(bug2, aes(x = Temperature, y = mean_time, group = Treatment, color = Treatment)) +
geom_point(size - 1.5)+
geom_errorbar(aes(ymin=mean_time - sd_time, ymax=mean_time + sd_time), width=.3, position=position_dodge(0.3)) +
labs(title="Means and SD of activity per acclimation temperature", x="Temperature (°C)", y = "Time to reach dark zone (seconds)") +
theme_light() +
scale_color_manual(values=c('#BA2BE5','#2BE59E'), labels = c("Stable", "Variable"))
print(pMeans)
#rTPC
b <- subset(bug, Treatment == "c")
start_vals <- get_start_vals(b$Temperature, b$Time, model_name = "beta_2012")
mod <- nls.multstart::nls_multstart(rate~beta_2012(temp = temp, a, b, c, d, e),
data = b,
iter = c(7,7,7,7,7),
start_lower = start_vals - 10,
start_upper = start_vals + 10,
lower = get_lower_lims(d$temp, d$rate, model_name = 'beta_2012'),
upper = get_upper_lims(d$temp, d$rate, model_name = 'beta_2012'),
supp_errors = 'Y',
convergence_count = FALSE)
#linear model ggplot
ggplot(bug2,aes(x, y)) +
geom_point() +
geom_smooth(method='lm')
# Summary Statistics
summary(lm(Time ~ Temperature * Treatment, data = bug))
I’ve tried using other models and changing the start_lower and start_upper values but nothing’s helped.
Felix Borrowdale is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.