At some point in the optimization, your function is returning a value greater than .Machine$double.xmax
(which is 1.797693e+308
on my machine).
Since your function f1(...)
is defined as sum(log(exp(...)))
, and since log(exp(z)) = z
for any z, why not use this:
par1 = c(1, 1, 2, 1.5, 1, 1.5, 1) x = seq(0, 500, length=100) f1 = function(par, x){ sum(-(par[7])*(par[1]*x + par[2]*x^2/2 + par[3] * (par[4]-x)^3/3+par[6] *(x-par[7])^3/3)) } result <- optim(par1, f1, x=x, method = "L-BFGS-B", lower = rep(0, length(par1)), upper = rep(Inf,length(par1)), control = list(trace = 5,fnscale=-1)) result$par # [1] 2.026284e-01 2.026284e-01 8.290126e+08 0.000000e+00 1.000000e+00 9.995598e+35 2.920267e+27 result$value # [1] 2.423136e+147
Note that the vector of parameters (par
) must be the first argument to f1
.
Related Posts:
- R Hex to RGB converter
- How do I select the first row in an R data frame that meets certain criteria?
- Poker hand range chart visualization in R
- Error in plot.new() : figure margins too large, Scatter plot
- How to join (merge) data frames (inner, outer, left, right)
- Interpreting “condition has length > 1” warning from `if` function
- rbind error: “names do not match previous names”
- rbind error: “names do not match previous names”
- $ operator is invalid for atomic vectors for dataframe R
- adding x and y axis labels in ggplot2
- Set NA to 0 in R
- R: Using equation with natural logarithm in nls
- kmeans complains “NA/NaN/Inf in foreign function call (arg 1)”, when there are none?
- Why use as.factor() instead of just factor()
- Update R using RStudio
- mean() warning: argument is not numeric or logical: returning NA
- ‘x’ and ‘y’ lengths differ ERROR when plotting
- What does “Error: object ‘
‘ not found” mean? - mean() warning: argument is not numeric or logical: returning NA
- Difference between paste() and paste0()
- How do I delete rows in a data frame?
- Error in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : Polygon edge not found
- Drop data frame columns by name
- Having trouble setting working directory
- How to find the statistical mode?
- How can two strings be concatenated?
- Error in colMeans(x, na.rm = TRUE) : ‘x’ must be numeric in KNN classification
- Error in ggplot.data.frame : Mapping should be created with aes or aes_string
- SummarySE (Rmisc package) to produce a barplot with error bars (ggplot2)
- Remove a row from a data table in R
- Editing legend (text) labels in ggplot
- Add color to boxplot – “Continuous value supplied to discrete scale” error
- case_when in mutate pipe
- Error in plot.window(…) : need finite ‘xlim’ valuescc
- “Error: Continuous value supplied to discrete scale” in default data set example mtcars and ggplot2
- Logistic Regression on factor: Error in eval(family$initialize) : y values must be 0 <= y <= 1
- Error: Incorrect number of dimensions in R
- cannot coerce type ‘closure’ to vector of type ‘character’
- dim(X) must have a positive length when applying function in data frame
- Basic – T-Test -> Grouping Factor Must have Exactly 2 Levels
- Error: attempt to use zero-length variable name
- How does one reorder columns in a data frame?
- R error: cannot coerce type ‘closure’ to vector of type ‘double’
- scale_color_manual() not working
- Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, …) 0 non-na cases
- Clear R environment of all objetcs & packages
- Why I get this error writing data to a file
- How to save a data frame as CSV to a user selected location using tcltk
- Subscript out of bounds – general definition and solution?
- R: Error in eval(predvars, data, env) : numeric ‘envir’ arg not of length one in intrinsic.pars() in multgee package
- Calculate correlation for more than two variables?
- Convert a list to a data frame
- What does the double percentage sign (%%) mean?
- How to subset a Data frame column wise using column names?
- Create an empty data.frame
- Error : length of ‘dimnames’ [2] not equal to array extent
- Can’t draw Histogram, ‘x’ must be numeric
- How can I plot data with confidence intervals?
- MLE error in R: initial value in ‘vmmin’ is not finite
- Rename multiple columns by names
- Unimplemented type list when trying to write.table
- Remove rows with all or some NAs (missing values) in data.frame
- Subscript out of bounds – general definition and solution?
- Convert date-time string to class Date
- Reshaping data.frame from wide to long format
- How to set limits for axes in ggplot2 R plots?
- duplicate ‘row.names’ are not allowed error
- predict.lm() in a loop. warning: prediction from a rank-deficient fit may be misleading
- Convert categorical variables to numeric in R
- How to sum data.frame column values?
- How do I get RSS from a linear model output
- Warning message: In `…` : invalid factor level, NA generated
- Mean of a column in a data frame, given the column’s name
- How to overcome error:”attempt to set ‘colnames’ on an object with less than two dimension” in xts object
- What does “The following object is masked from ‘package:xxx'” mean?
- grep using a character vector with multiple patterns
- How to avoid “Error in stripchart.default(x1, …) : invalid plotting method” error?
- ‘height’ must be a vector or a matrix. barplot error
- install.packages fails in knitr document: “trying to use CRAN without setting a mirror”
- Unused arguments in R
- How to convert a data frame column to numeric type?
- R apply function with multiple parameters
- Error in eval(predvars, data, env) : invalid ‘envir’ argument of type ‘character’
- Error in
: target of assignment expands to non-language object - run a for loop in parallel in R
- How do I combine two data-frames based on two columns?
- Efficiently sum across multiple columns in R
- How to eliminate “NA/NaN/Inf in foreign function call (arg 7)” running predict with randomForest
- Indicator function in R
- IF “OR” multiple conditions
- R4DS error comparison (1) is possible only for atomic and list types
- Replace all 0 values to NA
- Error in x[[i]] : recursive indexing failed at level 2
- non-numeric argument to binary operator
- How to avoid “operations are possible only for numeric, logical or complex types” when computing top 3 elements in each group
- Column standard deviation R [duplicate]
- Difference between as.POSIXct/as.POSIXlt and strptime for converting character vectors to POSIXct/POSIXlt
- invalid (do_set) left-hand side to assignment in R
- How do I print a hexadecimal number with leading 0 to have width 2 using sprintf?
- In R, dealing with Error: ggplot2 doesn’t know how to deal with data of class numeric