過去 1 週間のページビュー

2011年3月1日火曜日

Rの勉強1

最近は4月からの研究のためにRを勉強しなおしています。というより、勉強しています。正直恥ずかしくなるようなことですが、書いておきます。

今読んでいる本は、The R Tips―データ解析環境Rの基本技・グラフィックス活用集という本でありまして、この中に練習問題を解きつつ、分からなかったらその章をよく読むという形で進めております。

で、今日は数値計算と微積についてやりました。この辺はRではあまりやったことがなかったので。

> f<-function(x){exp(x)-2}
> f
function(x){exp(x)-2}
> f<-function(x) exp(x)-2
> f
function(x) exp(x)-2
> result<-uniroot(f,c(0,1))
> result
$root
[1] 0.6931457

$f.root
[1] -2.943424e-06

$iter
[1] 5

$estim.prec
[1] 6.103516e-05

> result$root
[1] 0.6931457
> polyroot(c(2,3,1))
[1] -1+0i -2-0i
> round
function (x, digits = 0) .Primitive("round")
> help(round)
starting httpd help server ... done
> polyroot(c(1,1,1))
[1] -0.5+0.8660254i -0.5-0.8660254i
> round(polyroot(c(1,1,1)),digits=3)
[1] -0.5+0.866i -0.5-0.866i
> round(polyroot(c(1,1,1)),digits=2)
[1] -0.5+0.87i -0.5-0.87i
> round(polyroot(c(1,1,1)),digits=1)
[1] -0.5+0.9i -0.5-0.9i
> round(polyroot(c(1,1,1)),digits=8)
[1] -0.5+0.8660254i -0.5-0.8660254i
> round(polyroot(c(1,1,1)),digits=9)
[1] -0.5+0.8660254i -0.5-0.8660254i
> f<- function(x) x^2-2*x
> f
function(x) x^2-2*x
> uniroot(f,c(1,3))
$root
[1] 2.000000

$f.root
[1] -5.356504e-07

$iter
[1] 6

$estim.prec
[1] 6.535148e-05

> polyroot(c(-2,5,-4,1))
[1] 1-0i 1+0i 2+0i
> f<- expression(a*x^4)
> f
expression(a * x^4)
> D(f,"x")
a * (4 * x^3)
> fix(DD)
>
> function (expr,name,order=1)
+ {
+ if(order<1){
+ stop("order' must be >= 1")}else if(order==1){
+ D(expr,name)}else{DD(D(expr,name),name,order -1)}
+
+ }
function (expr,name,order=1)
{
if(order<1){
stop("order' must be >= 1")}else if(order==1){
D(expr,name)}else{DD(D(expr,name),name,order -1)}

}
>
> DD(f,"x",3)
a * (4 * (3 * (2 * x)))
> deriv(~x^2,"x",func=T)
function (x)
{
.value <- x^2
.grad <- array(0, c(length(.value), 1L), list(NULL, c("x")))
.grad[, "x"] <- 2 * x
attr(.value, "gradient") <- .grad
.value
}
> f<-deriv(~x^2,"x",func=T)
> f
function (x)
{
.value <- x^2
.grad <- array(0, c(length(.value), 1L), list(NULL, c("x")))
.grad[, "x"] <- 2 * x
attr(.value, "gradient") <- .grad
.value
}
> f(-2)
[1] 4
attr(,"gradient")
x
[1,] -4
> g<- deriv(~x^2*y,c("x","y"),func=T)
> g(2,3)
[1] 12
attr(,"gradient")
x y
[1,] 12 4
> help(deriv)
> f<-deriv(~x^2,"x",func=F)
> f
expression({
.value <- x^2
.grad <- array(0, c(length(.value), 1L), list(NULL, c("x")))
.grad[, "x"] <- 2 * x
attr(.value, "gradient") <- .grad
.value
})
> f<-deriv(~x^2,"x",func=T)
> f
function (x)
{
.value <- x^2
.grad <- array(0, c(length(.value), 1L), list(NULL, c("x")))
.grad[, "x"] <- 2 * x
attr(.value, "gradient") <- .grad
.value
}
>

あと、これまでメモ帳を使って関数を作っていたのですが、fixという関数でRのエディタが立ち上がることが分かったので、これでやっていくべきなのかなぁと…。

本当にどうってことないことばかりですが、以上。

0 件のコメント:

コメントを投稿