我有一个运行大约 100 个回归模型的 R 代码,获取 R^2 值并将它们打印到一个 csv 文件中,如下所示:
filename<-"Reg_Results.csv";
cat("Setting,Origin Region,Destination Region,R^2\n",file=filename,append=FALSE);
for(setting in seq(from=1,to=3,by=1)) {
for(i in seq(from=1,to=7,by=1)) {
for(j in seq(from=1,to=7,by=1)) {
RRSub<-subset(RR,ORegion==ORegions[i]&DRegion==DRegions[j]);
if(nrow(RRSub)>1){
if(setting==1)
RRSub.LR <- lm(formula=Rev.per.Unit~RRs+Own+Miles+Category+STCC2.Description,data=RRSub);
if(setting==2)
RRSub.LR <- lm(formula=Rev.per.Unit~RRs+Own+Miles+Category+STCC5.Description,data=RRSub);
if(setting==3)
RRSub.LR <- lm(formula=Rev.per.Unit~RRs+Own+Miles+Category+STCC5.Description+OCity+DCity,data=RRSub);
cat(setting,file=filename,append=TRUE);
cat(",",file=filename,append=TRUE);
cat(ORegions[i],file=filename,append=TRUE);
cat(",",file=filename,append=TRUE);
cat(DRegions[j],file=filename,append=TRUE);
cat(",",file=filename,append=TRUE);
cat(summary(RRSub.LR)$r.squared,file=filename,append=TRUE);
cat("\n",file=filename,append=TRUE);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的目标是在同一个 .csv 文件中打印预测变量的名称(因为它们在每个回归模型中由于定性预测变量而不同)及其系数。
我的问题是:
任何 R 函数调用来获取预测变量的名称(不是系数值,我知道如何获取它们)?
有什么方法可以获取模型中使用了多少个预测变量?我将使用此值编写一个 for 循环来打印预测器名称。
我正在努力使用API获取一些歌词,并将歌词字符串转换为单词数组.我在preg_replace函数中得到了一些不寻常的行为.当我使用var_dump进行一些调试时,我看到var_dump为字符串"you"返回值10,这告诉我可能有错误.之后,preg_replace很奇怪.
这是我的代码:
$source = get_chart_lyrics_data("madonna","frozen");
$pieces = explode("\n", $source);
$lyrics = array();
for($i=0;$i<count($pieces);$i++){
if($i>10){
$words = explode(" ",$pieces[$i]);
foreach($words as $_word){
if($_word=="")
continue;
var_dump($_word);
$word = strtolower($_word);
var_dump($word);
$word = trim($word);
var_dump($word);
$word = preg_replace("/[^A-Za-z ]/", '', $word);
var_dump($word);
$lyrics[$word]++;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是此代码返回的前4行:
string(10) “You”
string(10) “you”
string(10) “you”
string(8) “lyricyou”
Run Code Online (Sandbox Code Playgroud)
为什么var_dump为"你"返回值10?为什么preg_replace就是这样的呢?
谢谢.
我希望在直方图中看到千位分隔格式的x轴数字.所以,例如,
y <- seq(10000, 100000, 10000)
hist(y)
Run Code Online (Sandbox Code Playgroud)
在这个图中,我想在x轴上看到10,000 20,000等.任何简单的方法来获得它?
在我的程序中,我使用了几个类和大量的函数.我想知道哪一个可以更快地工作,或者它们之间在速度方面没有区别.
1st:Class功能
class mex{
public:
int length,nof_evaluations,nof_fast_evaluations;
tree T;
calc_mex(vector<string>,vector<double>);
};
Run Code Online (Sandbox Code Playgroud)
这将被称为
mex m;
vector<string> v1;
vector<double> v2;
m.calc_mex(v1,v2);
Run Code Online (Sandbox Code Playgroud)
2nd:具有类指针的函数
class mex{
public:
int length,nof_evaluations,nof_fast_evaluations;
tree T;
};
calc_mex(mex*,vector<string>,vector<double>);
Run Code Online (Sandbox Code Playgroud)
这将被称为
mex m,*mptr;
mptr=&m;
vector<string> v1;
vector<double> v2;
calc_mex(mptr,v1,v2);
Run Code Online (Sandbox Code Playgroud)
我正在使用我的程序中的两种方式,但更倾向于方式1,因为它看起来更干净,更有条理.我也在一次运行程序中调用这些类型的函数100K次.所以我想知道他们中的任何一个是否会更好地适应时间.
谢谢!
我正在研究一个拥有超过300K元素的大数据集,并运行一些回归分析,尝试使用预测变量Distance来估计一个名为Rate的参数.我有回归方程.现在我想获得信心和预测间隔.我可以通过命令轻松获得系数的置信区间:
> confint(W1500.LR1, level = 0.95)
2.5 % 97.5 %
(Intercept) 666.2817393 668.0216072
Distance 0.3934499 0.3946572
Run Code Online (Sandbox Code Playgroud)
它给出了系数CI的上限和下限.现在我想获得预测间隔的相同上限和下限.到目前为止我唯一学到的是,我可以使用以下代码获取距离的特定值(例如200,500等)的预测间隔:
predict(W1500.LR1, newdata, interval="predict")
Run Code Online (Sandbox Code Playgroud)
这对我没用,因为我有超过300K的不同距离值,需要为每个距离值运行此代码.获取预测间隔的任何简单方法,如上面显示的confint命令?
r ×3
regression ×2
c++ ×1
class ×1
histogram ×1
php ×1
pointers ×1
prediction ×1
preg-replace ×1
tableau-api ×1
var-dump ×1