R版本2.15.0(2012-03-30)RStudio 0.96.316 Win XP,上次更新
我有一个包含40个变量和15.000个观测值的数据集.我想使用bestglm来搜索可能的好模型(逻辑回归).我已经尝试过bestglm,但它不适用于这样的中型数据集.经过几次试验,我认为当有超过30个变量时,bestglm会失败,至少在我的电脑上是这样(4G ram,双核心).
您可以自己尝试bestglm限制:
library(bestglm)
bestBIC_test <- function(number_of_vars) {
# Simulate data frame for logistic regression
glm_sample <- as.data.frame(matrix(rnorm(100*number_of_vars), 100))
# Get some 1/0 variable
glm_sample[,number_of_vars][glm_sample[,number_of_vars] > mean(glm_sample[,number_of_vars]) ] <- 1
glm_sample[,number_of_vars][glm_sample[,number_of_vars] != 1 ] <- 0
# Try to calculate best model
bestBIC <- bestglm(glm_sample, IC="BIC", family=binomial)
}
# Test bestglm with increasing number of variables
bestBIC_test(10) # OK, running
bestBIC_test(20) # OK, running
bestBIC_test(25) # OK, running
bestBIC_test(28) # Error: cannot allocate vector of size …Run Code Online (Sandbox Code Playgroud) 我将数据帧名称作为字符串传递给函数.如何从字符串中获取引用数据框的内容?假设我有字符串'mtcars',我想打印数据帧mtcars:
printdf <- function(dataframe) {
print(dataframe)
}
printdf('mtcars');
Run Code Online (Sandbox Code Playgroud) 我有一个数据框,如果它是空的,我想测试真的很快.我知道没有行或有整数(没有缺失值).到目前为止,我已经测试了五种不同的选项(见下文).有没有人有更快的解决方案?
df <- data.frame(a = integer(0), b = integer(0), c = integer(0))
fa <- function(){
nrow(df) > 0
}
fb <- function(){
any(dim(df)[1L])
}
fc <- function(){
(dim(df)[1L]) != 0
}
fd <- function() {
any(.subset2(df, 1)[1])
}
fe <- function() {
any(.subset2(df, 1))
}
library(microbenchmark)
microbenchmark(fa(), fb(), fc(), fd(), fe(), times = 1000)
Run Code Online (Sandbox Code Playgroud)
结果:
> microbenchmark(fa(), fb(), fc(), fd(), fe(), times = 1000)
Unit: nanoseconds
expr min lq mean median uq max neval cld
fa() 5664 6725 8672.462 6725 …Run Code Online (Sandbox Code Playgroud) 如何从Rcpp函数打印整数向量?在我的功能中,我想打印IntegerVector a.在RI中使用例如调用此函数compnz_next(5,3,c(1,2,2))
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
IntegerVector compnz_next(int n, int k, IntegerVector a) {
bool more = true;
int i;
static int h = 0;
static int t = 0;
for ( i = 0; i < k; i++ ) {
a[i] = a[i] - 1;
}
if ( 1 < t ) {
h = 0;
}
h = h + 1;
t = a[h-1];
a[h-1] = 0;
a[0] = t - 1; …Run Code Online (Sandbox Code Playgroud) 我意外地写了一个类似的查询select from my_table;,令人惊讶的是它是有效的声明.对我来说更有趣的是,即使SELECT;是PostgreSQL中的有效查询.您可以尝试使用以下方法编写很多有趣的查询:
select union all select;
with t as (select) select;
select from (select) a, (select) b;
select where exists (select);
create table a (b int); with t as (select) insert into a (select from t);
Run Code Online (Sandbox Code Playgroud)
这是一些定义SQL标准的结果,还是有一些用例,或者只是有趣的行为,没有人关心以编程方式限制?
文档的generate_series说这样的说法可能是int或bigint用于generate_series(start, stop)与generate_series(start, stop, step)案件timestamp或timestamp with time zone为generate_series(start, stop, step interval).
generate_series使用date类型作为输入和返回的原因是什么timestamp with timezone?
pg=# select generate_series('2014-01-01'::date,'2014-01-02'::date,'1 day');
generate_series
------------------------
2014-01-01 00:00:00+01
2014-01-02 00:00:00+01
(2 rows)
Run Code Online (Sandbox Code Playgroud) 我有一个主函数,它将参数传递给另一个用于生成模型的函数rpart。我希望能够rpart.control使用省略号从主函数中指定。如果没有定义cp或minbucket,那么我想使用这两个参数的默认值。
到目前为止,我还没有成功 - 请参阅下面的草稿。目前该函数抱怨没有cp找到。我理解为什么,但无法弄清楚,如果用户没有提供自己的控件,如何应用默认值。
require(rpart)
a <- function(df, ...) {
b(df, ...)
}
b <- function(df, ...) {
if (is.null(cp)) cp <- 0.001
if (is.null(minbucket)) minbucket = nrow(df) / 20
rcontrol <- rpart.control(cp = cp, minbucket = minbucket, ...)
rcontrol
}
a(iris) # no controls set my defaults for cp and minbucket
a(iris, cp = 0.123) # set cp, use my default for minbucket
a(iris, cp = 0.123, minbucket = …Run Code Online (Sandbox Code Playgroud) 我们经常在数据库中直接对数据进行评分,以获得线性或逻辑回归等简单模型.将所有系数从R正确传输到SQL总是有点棘手.我以为我可以为glm结果做一些R到SQL的翻译.对于数值变量,这非常简单:
library(rpart)
fit <- glm(Kyphosis ~ ., data = kyphosis, family = binomial())
coefs <- fit$coef[2:length(fit$coef)]
expr <- paste0('1/(1 + exp(-(',fit$coef[1], '+', paste0('(',
coefs, '*', names(coefs), ')', collapse = '+'),')))')
print(expr)
a <- with(kyphosis, eval(parse(text = expr)))
b <- predict(fit, kyphosis, type = 'response')
names(b) <- NULL
all.equal(a, b)
Run Code Online (Sandbox Code Playgroud)
生成的expr是:1/(1 + exp(-(-2.03693352129613+(0.0109304821420485*Age)+(0.410601186932733*Number)+(-0.206510049753697*Start)))).
有没有办法让这个factor变量工作?我想把因素放在case ... when ... then ... end条款中.假设我们有以下模型:
kyphosis$factor_variable <- rep(LETTERS[1:5],20)[1:81]
fit <- glm(Kyphosis ~ ., data = kyphosis, family = …Run Code Online (Sandbox Code Playgroud) 你怎么扭转cumprodR?
x <- c(0.5, 0.3, 0.1)
a <- cumprod(1 - x)
Run Code Online (Sandbox Code Playgroud)
现在,我想再次x离开a.
我正在使用 Marshmallow 将我的 Decision 类的实例发送到 JSON。但是,这也将转储属性None,例如,我的属性score将转换为nullJSON。之后,我无法使用相同的方法再次读取 JSON。
https://repl.it/repls/VolluminousMulticoloredFacts
最后一行是它当前失败的地方。我需要在加载过程中不转储None到 JSON 或跳过null:
import json
from marshmallow import Schema, fields, post_load
json_data = """{
"appid": "2309wfjwef",
"strategy": "First Strategy"
}"""
# Output class definition
class Decision(object):
def __init__(self, appid = None, strategy = None, score = None):
self.appid = appid
self.strategy = strategy
self.score = score
class DecisionSchema(Schema):
appid = fields.Str()
strategy = fields.Str()
score = fields.Int()
@post_load
def make_decision(self, data):
return …Run Code Online (Sandbox Code Playgroud) r ×7
postgresql ×2
dataframe ×1
ellipsis ×1
function ×1
glm ×1
json ×1
marshmallow ×1
python-3.x ×1
rcpp ×1
rpart ×1
timestamp ×1
types ×1