0

I installed mosaic package available in R to calculate derivative by typing install.packages('mosaic'). I created a function using makeFun and then i tried to calculate derivative in the following way

y1 <- makeFun(a +b *x  ~x, a=2, b=2)
dy1.dx <- D(a + b* x ~ x, a=2, b=2)
dy1.dx

but the console shows

" dy1.dx <- D(a+b*x~x, a=2, b=2)
Error in D(a + b * x ~ x, a = 2, b = 2) : unused arguments (a = 2, b = 2)"

How can I correct it?

  • Please read the documentation of the function and do `example("D")` ! – jogo Feb 13 '18 at 07:42
  • I read but couldn't get how to solve this problem. I used "quote" and that work but how to put value of constant in that – Pawan Kumar Feb 13 '18 at 08:14
  • Your example is not reproducible; `y1 <- makeFun(a +b *x ~x, a=2, b=2)` gives `Error in makeFun(a + b * x ~ x, a = 2, b = 2) : could not find function "makeFun"` **Please read** https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – jogo Feb 13 '18 at 08:29
  • 2
    This is because mosaic package is to be installed by typing install.packages('mosaic'). – Pawan Kumar Feb 13 '18 at 08:49
  • Please put additional information in your question - **not in a comment**, i.e. **edit your question**: https://stackoverflow.com/posts/48761689/edit – jogo Feb 13 '18 at 08:54
  • Sorry for the mistake, I am new to programming and stackoverflow – Pawan Kumar Feb 13 '18 at 08:59

2 Answers2

1

using the base R's stats package. No need to install additional packages like mosaic.

D will give the derivative of expression, so using expression() function, we create an expression and pass it to the D function.

Then eval will evaluate the expression and substitute will substitute values of a and b in the expression.

get the derivative for an expression with respect to x:

stats::D(expression(a + b * x), "x")
# b

evaluate the expression after substituting with values in the derivative. b is substituted with the value 2.

eval(substitute( stats::D(expression(a + b * x), "x"), list(a=2, b = 2) ))
# [1] 2

Another example:

stats::D(expression(a + a*b * x), "x")
# a * b
eval(substitute( D(expression(a + a*b * x), "x"), list(a=3, b = 2) ))
# 3 * 2
eval(eval(substitute( D(expression(a + a*b * x), "x"), list(a=3, b = 2) )))
# 6
Sathish
  • 12,453
  • 3
  • 41
  • 59
  • sorry sir, I am new to R and a learning slowly. Can you suggest me how i can correct my codes it if I have mosaic package installed. – Pawan Kumar Feb 13 '18 at 09:04
  • I do not have mosaic package installed on my system. – Sathish Feb 13 '18 at 09:05
  • I added some explanation to follow it. If you want to learn more, please read the documentation page by type `?D` or `?expression` – Sathish Feb 13 '18 at 09:08
0

D used in your example is from library(mosaicCalc) and not from base stat. Install and call the library. Your function works normally.

require(mosaicCalc)
#> Loading required package: mosaicCalc
#> Warning: package 'mosaicCalc' was built under R version 3.6.3
#> Loading required package: mosaicCore
#> Warning: package 'mosaicCore' was built under R version 3.6.3
#> Registered S3 method overwritten by 'mosaic':
#>   method                           from   
#>   fortify.SpatialPolygonsDataFrame ggplot2
#> 
#> Attaching package: 'mosaicCalc'
#> The following object is masked from 'package:stats':
#> 
#>     D
dy1.dx <- D(a + b * x ~ x, a = 2, b = 2)
dy1.dx
#> function (x, a = 2, b = 2) 
#> b
Ramakrishna S
  • 395
  • 1
  • 10