在使用R中的NLP时,有没有办法在词干中获得基本词而不是词根?
码:
> #Loading libraries
> library(tm)
> library(slam)
>
> #Vector
> Vec=c("happyness happies happys","sky skies")
>
> #Creating Corpus
> Txt=Corpus(VectorSource(Vec))
>
> #Stemming
> Txt=tm_map(Txt, stemDocument)
>
> #Checking result
> inspect(Txt)
A corpus with 2 text documents
The metadata consists of 2 tag-value pairs and a data frame
Available tags are:
create_date creator
Available variables in the data frame are:
MetaID
[[1]]
happi happi happi
[[2]]
sky sky
>
Run Code Online (Sandbox Code Playgroud)
我可以使用R得到"happyness happies happys"的基本单词"happy"(基本单词)而不是"happi"(根单词)
我正在使用模型的输出,其中存在可能不符合先验预期的参数估计.我想写一个函数,迫使这些效用估计值符合这些预期.为此,该函数应最小化起始值和新估计之间的平方偏差之和.由于我们有先验预测,因此优化应遵循以下约束:
B0 < B1
B1 < B2
...
Bj < Bj+1
Run Code Online (Sandbox Code Playgroud)
例如,下面的原始参数估计是针对B2和B3的翻转翻转.列Delta和Delta^2显示原始参数估计和新系数之间的偏差.我想尽量减少列Delta^2.我在Excel中对此进行了编码,并显示了Excel的Solver如何在提供约束条件的情况下优化此问题:
Beta BetaRaw Delta Delta^2 BetaNew
B0 1.2 0 0 1.2
B1 1.3 0 0 1.3
B2 1.6 -0.2 0.04 1.4
B3 1.4 0 0 1.4
B4 2.2 0 0 2.2
Run Code Online (Sandbox Code Playgroud)
通过阅读后?optim和?constrOptim,我不能神交如何设置这在R.我敢肯定,我只是作为一个有点迟钝,但可以在正确的方向上使用一些指针!
3/24/2012 - 添加了赏金,因为我不够聪明,无法翻译第一个答案.
这里有一些R代码应该在正确的路径上.假设测试版开始于:
betas <- c(1.2,1.3,1.6,1.4,2.2)
Run Code Online (Sandbox Code Playgroud)
我想尽量减少以下功能 b0 <= b1 <= b2 <= b3 <= b4
f <- function(x) {
x1 <- x[1]
x2 <- x[2] …Run Code Online (Sandbox Code Playgroud) 两个问题涉及boxplot:
na.action?文档没有列出它们.假设您正在使用大型工作环境,并且您不善于跟上环境变量,或者您有一些自动生成批量对象的过程.有没有办法扫描你ls()以识别具有给定类的所有对象?考虑以下简单示例:
#Random objects in my environment
x <- rnorm(100)
y <- rnorm(100)
z <- rnorm(100)
#I estimate some linear models for fun.
lm1 <- lm(y ~ x)
lm2 <- lm(y ~ z)
lm3 <- lm(y ~ x + z)
#Is there a programmatic way to identify all objects in my environment
#that are of the "lm" class? Or really, any arbitrary class?
outList <- list(lm1, lm2, lm3)
#I want to look at a bunch of plots for all …Run Code Online (Sandbox Code Playgroud) 我有一个超过150万行的大型数据集,来自600k个独特的主题,因此许多主题都有多行.我试图找到其中一个主题输入错误的DOB的情况.
test <- data.frame(
ID=c(rep(1,3),rep(2,4),rep(3,2)),
DOB = c(rep("2000-03-01",3), "2000-05-06", "2002-05-06",
"2000-05-06", "2000-05-06", "2004-04-06", "2004-04-06")
)
> test
ID DOB
1 1 2000-03-01
2 1 2000-03-01
3 1 2000-03-01
4 2 2000-05-06
5 2 2002-05-06
6 2 2000-05-06
7 2 2000-05-06
8 3 2004-04-06
9 3 2004-04-06
Run Code Online (Sandbox Code Playgroud)
我所追求的是一些基本上确定'2'有错误的代码.我可以想一些关于使用for循环的方法,但这在计算上是低效的.
谢谢
我将图形输出从R导出到pdf文件.
我想将Sys.time()和Sys.Date()添加到outfile名称.
比如我有一个声明
pdf("output filename.pdf", 8,10)
Run Code Online (Sandbox Code Playgroud)
我想输出看起来像输出 filename 2010-03-25 2pm.pdf
或类似的东西.
给定每月ts对象,例如:
dat <- ts(c(295, 286, 300, 278, 272, 268, 308, 321, 313, 308, 291, 296,
294, 273, 300, 271, 282, 285, 318, 323, 313, 311, 291, 293, 297,
273, 294, 259, 276, 294, 316, 325, 315, 312, 292, 301), frequency = 12)
Run Code Online (Sandbox Code Playgroud)
我怎样才能按月计算平均值?即我想计算1月,1年1月,2年+ 1月,3年等的平均值.然后能够比较2月的......
我想到的一种方法是将其转换为12列的矩阵并使用colMeans(),但我想有一种更好的方法可以利用对象的time()方面ts()?
colMeans(matrix(dat, ncol = 12, byrow = TRUE))
Run Code Online (Sandbox Code Playgroud) 我试图用R.重新创建下面的图.Minitab将其描述为正态概率图.

该probplot让你最那里的方式.不幸的是,我无法弄清楚如何在这个图周围添加置信区间带.
同样,ggplot的stat_qq()似乎与转换的x轴呈现相似的信息.看起来这geom_smooth()可能是增加乐队的候选者,但我还没弄明白.
最后,Getting Genetics Done的人在这里描述了类似的东西.
示例数据重新创建上图:
x <- c(40.2, 43.1, 45.5, 44.5, 39.5, 38.5, 40.2, 41.0, 41.6, 43.1, 44.9, 42.8)
Run Code Online (Sandbox Code Playgroud)
如果有人有基本图形或ggplot的解决方案,我会很感激!
编辑
在查看细节之后probplot,我已经确定这是如何在图表上生成拟合线:
> xl <- quantile(x, c(0.25, 0.75))
> yl <- qnorm(c(0.25, 0.75))
> slope <- diff(yl)/diff(xl)
> int <- yl[1] - slope * xl[1]
> slope
75%
0.4151
> int
75%
-17.36
Run Code Online (Sandbox Code Playgroud)
实际上,将这些结果与您从probplot对象中得到的结果进行比较似乎比较得很好:
> check <- probplot(x)
> str(check)
List of 3
$ qdist:function (p)
$ …Run Code Online (Sandbox Code Playgroud) 我编写了一个函数,它将基数为10的数字转换为另一个基数(我只对基数2 - 9感兴趣).我目前将基数10转换为基数2的函数如下:
cb2 <- function(num){
td<-{}
a <- {}
while (num 2 > 0 ){
a <- num %% 2
td <- paste(td,a, sep="")
num <- as.integer(num / 2)
}
return(td)
}
Run Code Online (Sandbox Code Playgroud)
用法是:
sapply(1:10, cb2)
Run Code Online (Sandbox Code Playgroud)
我想概括这个函数,并将首选的基数包含在函数的参数中,ala ...
convertbase <- function(num, base){
td<-{}
a <- {}
while (num / base > 0 ){
a <- num %% base
td <- paste(td,a, sep="")
num <- as.integer(num / base)
}
return(td)
}
Run Code Online (Sandbox Code Playgroud)
如果我只对转换成2-10基础的单个数字感兴趣,那么一切都很好:
mapply(convertbase, 10, 2:10)
Run Code Online (Sandbox Code Playgroud)
但是,如果我想要基数2:10的数字1:10,我会遇到问题:
mapply(convertbase, 1:10, 2:10)
Warning message: …Run Code Online (Sandbox Code Playgroud) 一次操作多列(或行)的编程方式是什么?考虑这个例子:
df2 <- data.frame(a = 1:10, b = 5:14, c = 10:1, d = 14:5)
Run Code Online (Sandbox Code Playgroud)
我想创建两个包含a/b和c/d比率的新列.为简单起见,我们假设所有列之间的空间关系是一致的.只需要制作两列,就可以这样做:
df2$ab <- with(df2, a/b)
df2$cd <- with(df2, c/d)
Run Code Online (Sandbox Code Playgroud)
要么
df2 <- transform(df2, ab = a/b, cd = c/d)
Run Code Online (Sandbox Code Playgroud)
要创建最终产品,例如:
a b c d ab cd
1 1 5 10 14 0.2000000 0.7142857
2 2 6 9 13 0.3333333 0.6923077
3 3 7 8 12 0.4285714 0.6666667
4 4 8 7 11 0.5000000 0.6363636
5 5 9 6 10 0.5555556 0.6000000
6 6 10 5 …Run Code Online (Sandbox Code Playgroud)