r语言predict函数中option函数什么意思

R语言字符处理
Encoding(x)
Encoding(x) &- value
enc2native(x)
enc2utf8(x)
读取或设置字符向量的编码
& ## x is intended to be in
& x &- "fa\xE7ile"
& Encoding(x)
[1] "latin1"
& Encoding(x) &- "latin1"
& xx &- iconv(x, "latin1",
& Encoding(c(x, xx))
[1] "latin1" "UTF-8"
& Encoding(xx) &- "bytes" # will be encoded
& cat("xx = ", xx, "\n", sep =
xx = fa\xc3\xa7ile
nchar(x, type = "chars", allowNA = FALSE)
返回字符长度,在我的测试中allowNA参数没有作用?
nzchar(x) 判断是否空字符
对于缺失值NA,nchar和nzchar函数认为是字符数为2的字符串。
所以在对字符串进行测量之前,最好先使用is.na()函数判断一下是否是NA。
对于NULL,nchar和nzchar函数会忽略掉。
& nchar(c("em","yqu","",NA))
[1] 2 3 0 2
& nzchar(c("em","yqu","",NA))
[1] TRUE TRUE FALSE TRUE &
nzchar(c("em","yqu",NULL,"",NA))
[1] TRUE TRUE FALSE TRUE
& nchar(c("em","yqu",NULL,"",NA))
[1] 2 3 0 2
& nchar(NULL)
integer(0)
& nzchar(NULL)
logical(0)
substr(x, start, stop)
substring(text, first, last = 1000000L)
substr(x, start, stop) &- value
substring(text, first, last = 1000000L) &- value
提取或替换字符向量的子字段,substring同substr功能一样,兼容S语言。
参数start大于stop时,抽取时返回"",替换时无操作。
如果x包含NA,对应结果为NA。
& substr("abcdef", 2, 4)
& substr("abcdef", -3, 9)
[1] "abcdef"
& substring("abcdef", 1:6, 1:6)
[1] "a" "b" "c" "d" "e" "f"
& x &-c("asfef", "qwerty", "yuiop[", "b",
"stuff.blah.yech")
& substring(x, 2, 4:5)
[1] "sfe" "wert" "uio" "" "tuf"
strtrim(x, width)
按显示宽度截断字符串
& x&-c("abcdef",NA,"66")
& strtrim(x,c(2,1,3))
[1] "ab" NA "66"
paste (..., sep = " ", collapse = NULL)
paste0(..., collapse = NULL)
通过sep连接间隔连接对象,返回字符串向量
设定collapse的话,会通过collapse连接间隔
将上一步的字符串向量连接成一个字符串
paste0(..., collapse)等同于paste(..., sep = "", collapse)
& paste(1:6) # same as
as.character(1:6)
[1] "1" "2" "3" "4" "5" "6"
& paste("A", 1:6, sep = "=")
[1] "A=1" "A=2" "A=3" "A=4" "A=5" "A=6"
& paste("A", 1:6, sep = "=",
collapse=";")
[1] "A=1;A=2;A=3;A=4;A=5;A=6"
strsplit(x, split, fixed = FALSE, perl = FALSE, useBytes =
基于split子句分割字符向量x
fixed为TRUE的话,完全匹配split;
否则,基于正则表达式
可以使用split=NULL来分割每个字符。
& x &- c(as = "mfe", qu = "qwerty",
"70", "yes")
& strsplit(x, "e")
[1] "qw" "rty"
[1] "y" "s"
& strsplit("Hello world!", NULL)
[1] "H" "e" "l" "l" "o" " " "w" "o" "r" "l" "d" "!"
& ## Note that 'split' is a
& unlist(strsplit("a.b.c", "."))
[1] "" "" "" "" ""
& ## If you really want to split on '.',
& unlist(strsplit("a.b.c",
[1] "a" "b" "c"
& unlist(strsplit("a.b.c", ".",
[1] "a" "b" "c"
字符转换和大小写转换
chartr(old, new, x)
将x中的字符old变换为字符new
& x &- "MiXeD cAsE 123"
& chartr("iXs", "why", x)
[1] "MwheD cAyE 123"
& chartr("a-cX", "D-Fw", x)
[1] "MiweD FAsE 123"
tolower(x)
toupper(x)
casefold(x, upper = FALSE)
casefold是为了兼容S-PLUS而实现的
tolower和toupper函数封装器。
& x &- "MiXeD cAsE 123"
& tolower(x)
[1] "mixed case 123"
& toupper(x)
[1] "MIXED CASE 123"
格式化输出
sprintf(fmt, ...) 系统C库函数sprintf封装器
& sprintf("%s is %f feet tall\n", "Sven",
[1] "Sven is 7.100000 feet tall\n"
format 格式化输出
formatC 格式化(C语言风格)输出
strwrap(x, width = 0.9 * getOption("width"),
indent = 0, exdent = 0, prefix = "",
simplify = TRUE, initial = prefix)
将字符串封装成格式化段落
& str &- "Now is the time
& strwrap(str, width=60,indent=1)
[1] " Now is the time"
& strwrap(str, width=60,indent=2)
[1] " Now is the time"
& strwrap(str, width=60,indent=3)
[1] " Now is the time"
& strwrap(str, prefix="kx&")
[1] "kx&Now is the time"
字符串匹配
pmatch(x, table, nomatch = NA_integer_, duplicates.ok =
局部字符串匹配,返回匹配的下标。
pmatch的行为因duplicates.ok参数而异。
当duplicates.ok为TRUE,有完全匹配的情况返回
第一个完全匹配的下标,否则有唯一一个局部匹配
的情况返回该唯一一个局部匹配的下标,没有匹配
则返回nomatch参数值。
空字符串与任何字符串都不匹配,甚至是空字符串。
当duplicates.ok为FALSE,table中的值一旦匹配
都被排除用于后继匹配,
空字符串例外。
NA被视为字符常量"NA"。
& pmatch(c("", "ab", "ab"), c("abc",
"ab"), dup = FALSE)
[1] NA 2 1
& pmatch(c("", "ab", "ab"), c("abc", "ab"),
dup = TRUE)
[1] NA 2 2
& pmatch("m", c("mean", "median", "mode")) #
returns NA
charmatch(x, table, nomatch = NA_integer_)
局部字符串匹配,返回匹配的下标。
charmatch与uplicates.ok为TRUE的pmatch近似,
当有单个完全匹配的情况返回该完全匹配的下标,
否则有唯一一个局部匹配的情况返回该唯一一个
局部匹配的下标,有多个完全匹配或局部匹配返回0,
没有匹配则返回nomatch参数值。
charmatch允许匹配空字符串。
NA被视为字符常量"NA"。
& charmatch(c("", "ab", "ab"),
c("abc","ab"))
& charmatch("m", c("mean", "median", "mode"))
# returns 0
match(x, table, nomatch = NA_integer_, incomparables =
x %in% table
值匹配,不限于字符串
c("e","ab","M",NA,"@","bla","P","%")
& sstr[sstr %in% c(letters,
[1] "e" "M" "P"
模式匹配和替换
grep(pattern,x,ignore.case=FALSE,
perl=FALSE,value=FALSE,fixed=FALSE,
useBytes=FALSE,invert=FALSE)
返回匹配下标
grepl(pattern,x,ignore.case=FALSE,
perl=FALSE,fixed=FALSE,useBytes=FALSE)
返回匹配逻辑结果
sub(pattern,replacement,x,ignore.case=FALSE,
perl=FALSE,fixed=FALSE,useBytes=FALSE)
替换第一个匹配的字符串
gsub(pattern,replacement,x,ignore.case=FALSE,
perl=FALSE,fixed=FALSE,useBytes=FALSE)
替换全部匹配的字符串
regexpr(pattern,text,ignore.case=FALSE,
perl=FALSE,fixed=FALSE,useBytes=FALSE)
返回第一个匹配的下标和匹配长度
gregexpr(pattern,text,ignore.case=FALSE,
perl=FALSE,fixed=FALSE,useBytes=FALSE)
返回全部匹配的下标和匹配长度
regexec(pattern,text,ignore.case=FALSE,
fixed=FALSE,useBytes=FALSE)
返回第一个匹配的下标和匹配长度
这些函数(除了不支持Perl风格正则表达式的regexec函数)可以工作在三种模式下:
fixed = TRUE: 使用精确匹配
perl = TRUE: 使用Perl风格正则表达式
fixed = FALSE且perl = FALSE: 使用POSIX 1003.2扩展正则表达式
useBytes = TRUE时逐字节匹配,否则逐字符匹配。
其主要作用是避免对多字节字符码中无效输入和虚假匹配
的错误/告警,但是对于regexpr,它改变了输出的解释。
它会阻止标记编码的输入进行转换,尤其任一输入被标记
为“字节”时强制禁止转换。
& str&-c("Now is ","the"," time
& grep(" +", str)
& grepl(" +", str)
[1] TRUE FALSE TRUE
& sub(" +", "", str)
[1] "Nowis " "the" "time "
& sub("[[:space:]]+", "", str) ## white space,
POSIX-style
[1] "Nowis " "the" "time "
& sub("\\s+", "", str, perl = TRUE) ##
Perl-style white space
[1] "Nowis " "the" "time "
& gsub(" +", "", str)
[1] "Nowis" "the" "time"
& regexpr(" +", str)
[1] 4 -1 1
attr(,"match.length")
[1] 1 -1 1
attr(,"useBytes")
& gregexpr(" +", str)
attr(,"match.length")
attr(,"useBytes")
attr(,"match.length")
attr(,"useBytes")
attr(,"match.length")
attr(,"useBytes")
& regexec(" +", str)
attr(,"match.length")
attr(,"match.length")
attr(,"match.length")
regmatches(x, m, invert = FALSE)
regmatches(x, m, invert = FALSE) &- value
抽取或替换正则表达式匹配子串
invert = TRUE则抽取或替换不匹配子串
& str&-c("Now is ","the"," time
& m&-regexpr(" +",str)
& regmatches(str,m)&- "kx"
[1] "Nowkxis " "the" "kxtime "
& str&-c("Now is ","the"," time
& m&-gregexpr(" +",str)
& regmatches(str,m, invert=TRUE)&-
[1] "kx kx kx" "kx" "kx kx kx"
agrep(pattern, x, max.distance = 0.1,
costs = NULL, ignore.case = FALSE,
value = FALSE, fixed = TRUE,
useBytes = FALSE)
agrepl(pattern, x, max.distance = 0.1,
costs = NULL, ignore.case = FALSE,
fixed = TRUE, useBytes = FALSE)
使用广义Levenshtein编辑距离进行字符串近似匹配
待进一步研究
& str &- c("1 lazy", "1", "1
& agrep("laysy", str, max = 2)
grepRaw(pattern, x, offset = 1L,
ignore.case = FALSE, value = FALSE,
fixed = FALSE, all = FALSE,
invert = FALSE)
对原始数据向量进行模式匹配
& raws &- charToRaw("Now is the time
[1] 4e 6f 77 20 69 73 20 74 68 65 20 74 69 6d 65 20
& grepRaw(charToRaw(" +"),raws)
glob2rx(pattern, trim.head = FALSE, trim.tail = TRUE)
将通配符模式变成正则表达式
& glob2rx("abc.*")
[1] "^abc\\."
& glob2rx("a?b.*")
[1] "^a.b\\."
& glob2rx("a?b.*", trim.tail =
[1] "^a.b\\..*$"
& glob2rx("*.doc")
[1] "^.*\\.doc$"
& glob2rx("*.doc", trim.head =
[1] "\\.doc$"
& glob2rx("*.t*")
[1] "^.*\\.t"
& glob2rx("*.t??")
[1] "^.*\\.t..$"
& glob2rx("*[*")
[1] "^.*\\["
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。二次元同好交流新大陆
扫码下载App
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
本文转载自AthenaXu
阅读(12434)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
loftPermalink:'',
id:'fks_',
blogTitle:'【转载】R语言扩展包dplyr笔记',
blogAbstract:'http://bqnw.me/post/dplyr-note引言2014年刚到, 就在&',
blogTag:'',
blogUrl:'blog/static/',
isPublished:1,
istop:false,
modifyTime:0,
publishTime:9,
permalink:'blog/static/',
commentCount:1,
mainCommentCount:1,
recommendCount:1,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:true,
hostIntro:'',
hmcon:'1',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}R语言实现期权二叉树定价的函数 - 简书
下载简书移动应用
写了2490字,被2人关注,获得了2个喜欢
R语言实现期权二叉树定价的函数
以后就在这里写一些文字,主要是自己各种各样的学习笔记。尽管现在只是个ATM管理员,但勉强也算是金融行业从业者,内容以金融为主,如果学到些其他旁门左道的东西,也会考虑分享一下。日最近在学R语言,看过《R语言初学者指南》,看了约翰霍普金斯大学的
网络公开课(非常推荐这个课程,不过不建议刚开始接触R就看,对于没有编程基础的人还是有点难度,建议先看《R语言初学者指南》),感觉收获颇多,自己也手痒想写点什么东西,于是乎想写个期权定价函数。结果自己折腾了半天还是写不出来,最后只能把《EXCEL及VBA高级金融建模》里面的VBA函数照搬过来,发觉还是能用。不过R语言的循环着实不太好用,二叉树划分到5000步以上计算时间就已经相当长了,或者是算法还有优化的空间。#欧式期权EUOption &- function(iopt, S, X, r, q, tyr, sigma, nstep){#iopt 1 call, -1 put#S 现价#X 行权价#r 无风险利率#q 红利收益率#tyr 期限#sigma 波动率(标准差)#nstep 二叉树步数delt &- tyr/nstep #每一步的期限erdt &- exp(r*delt) #折现因子ermqdt &- exp((r-q)*delt) #考虑股利效应u &- exp(sigma*sqrt(delt))d &- 1/uP &- (ermqdt - d)/(u - d)Pstar &- 1-Pvvec &- vector(length = nstep + 1)#例如9步二叉树最后会有10个结果#算出最后一期全部可能的股价情形下的期权价值for(i in 1 :nstep +1){vvec[i] &- max(iopt*(S*u^(i-1)*d^(nstep +1 -i) - X),0)#注意iopt后面的括号,-1的时候为看跌期权,括号要把X也包进来}#倒推回去各个节点的价值for(j in nstep : 1){for(k in 1 : j){vvec[k] &- (P*vvec[k+1] + Pstar*vvec[k])/erdt}}vvec[1]}#可选择美式或者欧式期权的函数OptionPrice &- function(iopt, iea, S, X, r, q, tyr, sigma, nstep){#iopt 1 call, -1 put#iea 1欧式,2为美式#S 现价#X 行权价#r 无风险利率#q 红利收益率#tyr 期限#sigma 波动率(标准差)#nstep 二叉树步数delt &- tyr/nstep #每一步的期限erdt &- exp(r*delt) #折现因子ermqdt &- exp((r-q)*delt) #考虑股利效应u &- exp(sigma*sqrt(delt))d &- 1/uP &- (ermqdt - d)/(u - d)Pstar &- 1-Pvvec &- vector(length = nstep + 1)#例如9步二叉树最后会有10个结果#算出最后一期全部可能的股价情形下的期权价值for(i in 1 :nstep +1){vvec[i] &- max(iopt*(S*u^(i-1)*d^(nstep +1 -i) - X),0)}#注意iopt后面的括号,-1的时候为看跌期权,括号要把X也包进来#美式期权#倒推回去各个节点的价值for(j in nstep : 1){for(k in 1 : j){vvec[k] &- (P*vvec[k+1] + Pstar*vvec[k])/erdtif(iea == 2){vvec[k] &- max(vvec[k],iopt*(S*u^(k-1)*d^(j-k) - X))}#和纯粹欧式不同,如总共9步,这里只需要算到第8步}}vvec[1]}
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
被以下专题收入,发现更多相似内容:
每个人都应该懂一点点金融知识,请关注【学点金融】
· 11774人关注
Write the Code, Change the World, 改变世界从编程开始, 收集编程相关的干货
· 11437人关注
记录工作中和业余时间学习的金融知识
· 70人关注
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
选择支付方式:R语言汇总函数_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
评价文档:
R语言汇总函数
上传于||暂无简介
大小:1.54KB
登录百度文库,专享文档复制特权,财富值每天免费拿!
你可能喜欢}

我要回帖

更多关于 r语言 自定义函数 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信