在 R 中,我可以使用以下命令创建带有残差的三维回归平面
scatter3d(prestige ~ income + education, data=Duncan)
具有包scatter3d
中的功能car
R
。
我需要一些提示来制作相同的情节tikz
。任何帮助都将不胜感激。谢谢
library(car)
Duncan
type income education prestige
accountant prof 62 86 82
pilot prof 72 76 83
architect prof 75 92 90
author prof 55 90 76
chemist prof 64 86 90
minister prof 21 84 87
professor prof 64 93 93
dentist prof 80 100 90
reporter wc 67 87 52
engineer prof 72 86 88
undertaker prof 42 74 57
lawyer prof 76 98 89
physician prof 76 97 97
welfare.worker prof 41 84 59
teacher prof 48 91 73
conductor wc 76 34 38
contractor prof 53 45 76
factory.owner prof 60 56 81
store.manager prof 42 44 45
banker prof 78 82 92
bookkeeper wc 29 72 39
mail.carrier wc 48 55 34
insurance.agent wc 55 71 41
store.clerk wc 29 50 16
carpenter bc 21 23 33
electrician bc 47 39 53
RR.engineer bc 81 28 67
machinist bc 36 32 57
auto.repairman bc 22 22 26
plumber bc 44 25 29
gas.stn.attendant bc 15 29 10
coal.miner bc 7 7 15
streetcar.motorman bc 42 26 19
taxi.driver bc 9 19 10
truck.driver bc 21 15 13
machine.operator bc 21 20 24
barber bc 16 26 20
bartender bc 16 28 7
shoe.shiner bc 9 17 3
cook bc 14 22 16
soda.clerk bc 12 30 6
watchman bc 17 25 11
janitor bc 7 20 8
policeman bc 34 47 41
waiter bc 8 32 10
scatter3d(prestige ~ income + education, data=Duncan)
scatter3d(prestige ~ income + education | type, data=Duncan)
答案1
首先,除了调用 R(一些软件包可以做到)之外,没有基于 tex 的解决方案能够模仿 R 命令的简洁性进行统计分析;这是 R 的强项之一。
话虽如此,这里尝试使用 Asymptote 重做你的第一幅图像(包括统计分析)。请注意,统计分析需要smoothcontour3 包,除非您拥有最新版本的 Asymptote,否则您可能需要手动安装(即将文件复制到您的工作目录中)。
代码假定您的数据(包括标题)已被复制并粘贴到名为的文件中Duncan.dat
。(标题不是必需的;只是我明确跳过了第一行。)
\documentclass{standalone}
\usepackage{asypictureB}
\begin{document}
\begin{asypicture}{name=UnifiedRegression}
settings.outformat = "png";
settings.render = 8;
size(10cm);
import graph3;
import smoothcontour3; // for the leastsquares routine
Billboard.targetsize = true; // Perspective should not affect the labels.
currentprojection = perspective(60 * (5, 2, 3));
file duncan = input("Duncan.dat");
string headers = duncan;
real[][] independentvars;
real[] dependentvars;
while (!eof(duncan)) {
string line = duncan;
string[] entries = split(line);
if (entries.length < 5) continue;
string type = entries[1];
real income = (real)(entries[2]);
real education = (real)(entries[3]);
real prestige = (real)(entries[4]);
// include 1.0 for the residue
independentvars.push(new real[] {income, education, 1.0});
dependentvars.push(prestige);
}
real[] coeffs = leastsquares(independentvars, dependentvars, warn=false);
if (coeffs.length == 0) {
abort("Unable to find regression: independent variables are "
+ "linearly dependent.");
}
real f(pair xy) {
return coeffs[0] * xy.x // income
+ coeffs[1] * xy.y // education
+ coeffs[2]; // residue
}
real xmin = infinity, xmax = -infinity, ymin = infinity, ymax = -infinity;
for (real[] row : independentvars) {
if (row[0] < xmin) xmin = row[0];
if (row[0] > xmax) xmax = row[0];
if (row[1] < ymin) ymin = row[1];
if (row[1] > ymax) ymax = row[1];
}
// Draw the plane
draw(surface(f, (xmin, ymin), (xmax, ymax)),
surfacepen=emissive(blue + opacity(0.6)),
meshpen = blue);
for (int ii = 0; ii < independentvars.length; ++ii) {
triple pt = (independentvars[ii][0], independentvars[ii][1],
dependentvars[ii]);
draw(shift(pt) * unitsphere, material(yellow, emissivepen=0.2*yellow));
real z = f((pt.x, pt.y));
if (pt.z > z) draw (pt -- (pt.x, pt.y, z), green);
else draw(pt -- (pt.x, pt.y, z), red);
}
xaxis3("income", Bounds(Min, Min), InTicks);
yaxis3("education", Bounds(Min, Min), InTicks);
zaxis3("prestige", Bounds(Min, Min), InTicks);
\end{asypicture}
\end{document}
答案2
看一下这个pgfplots
包。计算回归平面可能最容易,R.
下面是一个最简单的示例(没有修饰)供您入门,其中 try.dat 包含您提供的数据。
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot3[only marks] table[x index=2, y index=3,z index=4]{try.dat};
\end{axis}
\end{tikzpicture}
\end{document}