我正在尝试使用10倍交叉验证来估计逻辑回归.
#import libraries
library(car); library(caret); library(e1071); library(verification)
#data import and preparation
data(Chile)
chile <- na.omit(Chile) #remove "na's"
chile <- chile[chile$vote == "Y" | chile$vote == "N" , ] #only "Y" and "N" required
chile$vote <- factor(chile$vote) #required to remove unwanted levels
chile$income <- factor(chile$income) # treat income as a factor
Run Code Online (Sandbox Code Playgroud)
目标是估计一个glm模型,该模型预测投票"Y"或"N"的结果取决于相关的解释变量,并且基于最终模型,计算混淆矩阵和ROC曲线以掌握不同阈值水平的模型行为.
型号选择导致:
res.chileIII <- glm(vote ~
sex +
education +
statusquo ,
family = binomial(),
data = chile)
#prediction
chile.pred <- predict.glm(res.chileIII, type = "response")
Run Code Online (Sandbox Code Playgroud)
产生:
> head(chile.pred)
1 2 …Run Code Online (Sandbox Code Playgroud) 我想知道是否有可能在julia数组中选择除一个元素之外的所有元素(通过索引).
例如,在R语言中,为了不选择矩阵中的特定行,可以写:
a = matrix(1:9, 3, 3)
a
1 4 7
2 5 8
3 6 9
Run Code Online (Sandbox Code Playgroud)
然后:
a[-2, ]
1 4 7
3 6 9
Run Code Online (Sandbox Code Playgroud)
现在我想在朱莉娅做同样的事情.我尝试使用逻辑运算符,但我找不到(un)选择特定索引的方法.这是我尝试过的:
a = rand(3,3)
a[.!= 2, :]
ERROR: syntax "!=" is not a unary operator
Run Code Online (Sandbox Code Playgroud)
或如在R中:
a[-2, :]
Run Code Online (Sandbox Code Playgroud)
以及其他一些选择.在julia工作的是以下内容:
a[a .>= .5, :]
Run Code Online (Sandbox Code Playgroud)
要么
a[[2,3], :]
Run Code Online (Sandbox Code Playgroud)
选择秒和第三行.无论如何,我真的想知道如何在朱莉娅数组中选择除了一个特定元素(例如行)之外的所有元素.
我想知道是否有可能在julia中为变量分配自定义或现有类型而不调用它的构造函数.
类似于c ++中类的前向声明的东西.
这是我打算实现的一个例子:
type foo
a
end
#custom type
a = foo
b = foo
#julia type, force c to be of type Int
c = Int
a.a = 5.5
b.a = 4.5
c = 6
Run Code Online (Sandbox Code Playgroud)
编辑澄清我的问题:
在C++或Fortran中,通常的编码实践是在某个时刻声明变量以供以后使用.
我不记得正确的Fortran语法,但在C++中你会写如下:
class foo;
class bar;
int a;
class foo{
private:
bar myBar;
public:
foo(int a){ myBar( a ); }
}
class bar{
public:
bar(int a){
std::cout << a << std::endl;
}
}
a = 3;
foo( a );
Run Code Online (Sandbox Code Playgroud)
此代码结构的优点是它允许您在使用它们之前定义对象/类型/变量.
我尝试实现多态仿函数对象(纯抽象基类和子对象),仅用于理解目的.我的目标是创建基类的许多对象,这些对象使用纯虚函数的不同实现.
当我创建基类的指针并将其设置为等于新的子类时,我无法将该对象作为函数调用.错误是:
main.cpp:29:7: error: ‘a’ cannot be used as a function
Run Code Online (Sandbox Code Playgroud)
这是代码:
#include <iostream>
class foo{
public:
virtual void operator()() = 0;
virtual ~foo(){}
};
class bar: public foo{
public:
void operator()(){ std::cout << "bar" << std::endl;}
};
class car: public foo{
public:
void operator()(){ std::cout << "car" << std::endl;}
};
int main(int argc, char *argv[])
{
foo *a = new bar;
foo *b = new car;
//prints out the address of the two object:
//hence I know that they …Run Code Online (Sandbox Code Playgroud) 我正在尝试实现核密度估计.但是我的代码没有提供它应该的答案.它也是用朱莉娅写的,但代码应该是自我解释的.
这是算法:
哪里
因此算法测试是否x和观察X_I由一些常数因数(binwidth)加权之间的距离小于一个.如果是这样,它分配0.5 /(N*h)至该值,其中n =循环移位#of观测.
这是我的实现:
#Kernel density function.
#Purpose: estimate the probability density function (pdf)
#of given observations
#@param data: observations for which the pdf should be estimated
#@return: returns an array with the estimated densities
function kernelDensity(data)
|
| #Uniform kernel function.
| #@param x: Current x value
| #@param X_i: x value of observation i
| #@param width: binwidth
| #@return: Returns 1 if the absolute distance from
| #x(current) to x(observation) weighted by the binwidth …Run Code Online (Sandbox Code Playgroud) 我试图理解 R 中 AIC/BIC 的结果。出于某种原因,R 在要估计的参数数量上加了 1。2 * p - 2 * logLik因此,R 使用与(在高斯情况下logLik是残差平方和)不同的公式。事实上它使用:2 * (p + 1) - 2 * logLik.
经过研究,我发现问题与stats:::logLik.lm().
> stats:::logLik.lm ## truncated R function body
## ...
## attr(val, "df") <- p + 1
## ...
Run Code Online (Sandbox Code Playgroud)
作为一个真实的例子(使用 R 的内置数据集trees),请考虑:
x <- lm(Height ~ Girth, trees) ## a model with 2 parameters
logLik(x)
## 'log Lik.' -96.01663 (df=3)
Run Code Online (Sandbox Code Playgroud)
这实在令人费解。有人知道为什么吗?
Edit1:glm@crayfish44 的示例
model.g <- glm(dist …Run Code Online (Sandbox Code Playgroud) 我只是试图初始化一个容器,它碰巧是空的,并且遇到了以下现象:
#include <iostream>
#include <array>
#include <algorithm>
int main(int argc, char *argv[])
{
std::array<int,NULL> foo = {};
if ( std::all_of(foo.begin(), foo.end(), [](int i){return i==0;}) )
std::cout << "All the elements are zero.\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译:
clang++ -std=c++11 -stdlib=libc++ -o test test.cpp
Run Code Online (Sandbox Code Playgroud)
导致:
bash-3.2$ ./test
All the elements are zero.
Run Code Online (Sandbox Code Playgroud)
我试图弄清楚为什么空容器为此操作返回true.此问题可能与以下内容有关: 当list为空时,std :: list:begin()的行为
但是我无法找到这个特定问题的正确答案.
感谢您的时间.
我大约。我的计算机上有2 GB的免费DRAM。编译std :: array或标准数组:
#include <iostream>
#include <array>
int main(int argc, char *argv[]){
// int* a = new int[500000000];
std::array<int, 2000000> a;
}
Run Code Online (Sandbox Code Playgroud)
与:
$ g++ -std=c++11 main.cpp -o main
./main
Run Code Online (Sandbox Code Playgroud)
适用于两个数组。将std :: array的大小更改为:
// ceteris paribus
std::array<int, 2095300> a;
Run Code Online (Sandbox Code Playgroud)
导致:
$ ./main
Segmentation fault (core dumped)
Run Code Online (Sandbox Code Playgroud)
老实说,我不确定这个问题是否已经解决。
据我了解,std :: array是在堆栈上创建的,int * ...数组是在堆栈上创建的。现在我的猜测是,也许我的堆栈不会比〜8mb大,而与2 GB的堆相比听起来不相称。因此我也尝试了:
//int a[2096000];
Run Code Online (Sandbox Code Playgroud)
这也会导致分段错误。所以我的问题是,导致细分错误的原因是什么?
先感谢您。
c++ ×3
julia ×3
arrays ×2
glm ×2
r ×2
algorithm ×1
c++11 ×1
containers ×1
functor ×1
lm ×1
polymorphism ×1
r-caret ×1
regression ×1
select ×1
types ×1