用回归值替换缺失数据<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />你好
我有一个时间序列,其中变量缺少值。 我想用从时间上最接近的可用值派生的线性模型来替换缺失值。
最好的方法是什么?
如果脚本可以对我表的所有列(时间除外)执行此操作,那就更好了
谢谢
例子
我拥有的
我想要的是:
| t | 输出 |
|---|
| 0 | 10 |
| 1 | 15 |
| 2 | 20 |
| 4 | 30 |
| 10 | 22.5 |
| 20 | 10 |
回复:用回归值替换缺失数据JMP 公式以及 JMP 函数的工作方式是,最后创建或引用的变量是从公式或函数传递的变量。 因此,在替换公式中,作为行值返回的是“Values”矩阵中等于 Row() 数字的值。 由于“值”矩阵是使用 << get value 消息获取列值的位置,并且通过指定在“值”矩阵上进行插值
值[行()]
该公式将一次一行的值移动到新列中。
另一种解释方法是通过公式的流程
所有公式都循环遍历数据表中的所有行,从数据表中的第 1 行到最后一行。
在处理第 1 行时,“p (g/L)”列中的所有值均移至矩阵中进行处理。 处理从矩阵中的元素 1 开始循环,一直到矩阵末尾,并在必要时执行插值。 循环结束时,矩阵的所有元素都已填充。 然后,“值”矩阵中第一个元素的值将作为用于列中第一行的值返回到公式。
然后移动行值以运行第二行的公式,并且为第二行执行的唯一语句是
值[行()]
它向公式返回第 2 行第 2 列的值,即“值”矩阵中的元素 2。
该公式对每个后续行继续循环一次,仅执行该语句
值{行()]
我希望这是有道理的回复:用回归值替换缺失数据吉姆,
非常感谢。现在效果非常好。你能向我解释一下最后一个“values[Row()];”是什么吗?做
回复:用回归值替换缺失数据如果您希望在 :Time 值为 0 时使 Interpreted 列中的值等于 0,则可以对如下代码进行更改
For( i = 1, i <= N Rows( values ), i++,
If( :Time[i] == 0, values[i]=0); // **************
If( Is Missing( values[i] ),
counter++,
If( counter > 0,
For( k = 1, k <= counter, k++,
base = values[i - counter - 1];
incr = Abs( (base - values[i]) / (counter + 1) );
If( base < values[i],
values[(i - counter - 1) + k] = base + k * incr,
values[(i - counter - 1) + k] = base - k * incr
);
);
counter = 0;
)
)
);回复:用回归值替换缺失数据这对第一个值执行此操作,但我需要的是所有“时间”都为 0,插值为零。时间列将有多个 0
回复:用回归值替换缺失数据从我提供给你的代码来看,这是你要问的吗?
If( Is Missing( values[1] ),
values[1] = 0
);回复:用回归值替换缺失数据吉姆,
我让脚本工作了。 我必须添加一些数学运算,以便考虑插值评估循环的增量计算的增量时间。最后一个问题是,由于我计划将多个运行文件连接在一起并运行脚本,因此我将有多个时间 = 0 的点(时间:0,12,24,48,0,12,24,48... ),因此对于给定的数据块,第一个插值应该为 0。 我不知道如何用矩阵表示法来做到这一点。
回复:用回归值替换缺失数据根据我的经验,在尝试引用公式中的实际公式列时,我遇到了太多问题。 因此,显示了该方法。 从您使用 For() 循环运行数据的次数的角度来看,我还担心您的公式。 我的方法仅遍历数据表一次......不计算迭代替换。回复:用回归值替换缺失数据吉姆,
非常感谢,我会尝试一下。 不过我很好奇;回到原始脚本,我认为递归错误来自第 3 行,因为它会计算 incr 并得到 rows-counter-1 = 3-2-1 =0。我会忽略错误消息,它成功地插入了该列的其余部分,除了最后之外。向表中添加更多数据进行插值可以缓解问题。我认为它们可能是不同的问题。
回复:用回归值替换缺失数据当您尝试更改公式列本身内的值时,您会遇到公式中的递归问题。我已经重新设计了您的公式,以使用矩阵预先完成所有工作。然后将结果值用于每行值
Names Default To Here( 1 );
col_name_list = Current Data Table() << get column names( string );
If( !Contains( col_name_list, "Interpolated p (g/L)" ),
Current Data Table() << New Column( "Interpolated p (g/L)",
Numeric,
Continuous,
Formula(
dt = Current Data Table();
If( Row() == 1,
counter = 0;
values = dt:Name( "p (g/L)" ) << get values;
If( Is Missing( values[1] ),
values[1] = 0
);
counter = 0;
For( i = 1, i <= N Rows( values ), i++,
If( Is Missing( values[i] ),
counter++,
If( counter > 0,
For( k = 1, k <= counter, k++,
base = values[i - counter - 1];
incr = Abs( (base - values[i]) / (counter + 1) );
If( base < values[i],
values[(i - counter - 1) + k] = base + k * incr,
values[(i - counter - 1) + k] = base - k * incr
);
);
counter = 0;
)
)
);
);
values[Row()];
)
)
);回复:用回归值替换缺失数据这是表格
回复:用回归值替换缺失数据吉姆,
谢谢你的澄清。 我的脚本有问题。它不会执行并插入最后一个间隔。我无法判断它是否与循环的嵌套或“while”条件有关。基本上无论表长度如何,它都无法填充丢失数据的最后一个间隔。
col_name_list = Current Data Table() << get column names( string );
If( !Contains( col_name_list, "Interpolated p (g/L)" ),
Current Data Table() << New Column(
"Interpolated p (g/L)",
Numeric,
Continuous,
Formula(
If(
Is Missing( :Name( "time (h)" ) ),
Empty(),
:name( "time (h)" ) == 0,
0,
!Is Missing( :Name( "p (g/L)" ) ),
:name( "p (g/L)" ),
dt = Current Data Table();
For( rows = 1, rows <= (N Rows( dt )), rows++,
If( Rows == 1, counter = 0 );
If( Is Missing( dt:name( "Interpolated p (g/L)" )[rows] ) == 1,
counter++);
If( counter > 0 & Is Missing( dt:name( "Interpolated p (g/L)" )[rows] ) == 0,
incr = (dt:name( "Interpolated p (g/L)" )[Rows] - dt:name( "Interpolated p (g/L)" )[Rows - counter - 1]) / (dt
:name( "time (h)" )[Rows] - dt:name( "time (h)" )[Rows - counter - 1]);
For( i = rows - counter, i <= rows - 1, i++,
dt:name( "Interpolated p (g/L)" )[i] = dt:name( "Interpolated p (g/L)" )[i - 1] + (incr * (dt:name( "time (h)" )[i]
-dt:name( "time (h)" )[i - 1]))
);
counter = 0;
);
);
)
)
)
);
回复:用回归值替换缺失数据JMP 在函数和语句中不区分大小写,因此 rows=Rows回复:用回归值替换缺失数据吉姆,
“行”和“行”有区别吗?你能解释一下吗? 我是个菜鸟。
回复:用回归值替换缺失数据不错的收获......应该是
If this is row 1 initialize a variable named counter to 0
回复:用回归值替换缺失数据谢谢它成功了!你评论了
If this is row 1 initialize a variable named counter to 1
,您的意思是“与 0 相对”吗?
回复:用回归值替换缺失数据这是我的原始代码的更新/注释版本。您应该做的就是将列名称从“输出”更改为您想要将值更改为的列名称。
// Set all default referenced memory variables to this unique application
Names Default To Here( 1 );
// Set the variable dt to be a pointer to the current active data table
dt = Current Data Table();
// Loop through the data table from row 1 to the last row and use a
// variable names "rows" as the index
For( rows = 1, rows <= N Rows( dt ), rows++,
// If this is row 1 initialize a variable named counter to 1
If( Rows == 1, counter = 0 );
// If the current row's value for the column named "Output" in the
// data table referenced by "dt" is missing (blank, null) increase
// the counter
If( Is Missing( dt:output[rows] ) == 1,
counter++
);
// If row is found with a non missing value for the column called Output,
// and the counter is > 0, which indicates previous rows with missing
// values, then calculate the required values and place them in their
// appropriate rows
If( counter > 0 & Is Missing( dt:output[rows] ) == 0,
// Calculate the incremental steps to use for the missing cells
incr = (dt:output[Rows] - dt:output[Rows - counter - 1]) / (counter + 1);
// Loop across the rows with missing values and fill in the data
For( i = Rows - counter, i <= Rows - 1, i++,
dt:output[i] = dt:output[i - 1] + incr
);
// reinitialize the counter
counter = 0;
);
);回复:用回归值替换缺失数据特克斯尼尔森,
你能用评论更新脚本吗? 我是 JSL 新手,需要帮助了解它如何应用于我的问题。 我和OP有同样的需求,但只有一栏需要填写。或者,我过去使用过以下VBA代码来填充Excel中缺失的数据。 我想知道JSL中是否有类似于End(xlDown).value函数的东西。偏移量用于指定时间点数据所在的位置。 谢谢!
Sub interpolatedata()
For Each cell In Range("k4:k29")
If cell.Value = "" Then
y2 = cell.End(xlDown).Value
x2 = cell.End(xlDown).Offset(0, -8).Value
y1 = cell.End(xlUp).Value
x1 = cell.End(xlUp).Offset(0, -8).Value
x = cell.Offset(0, -8).Value
y = (y1) + (x - x1) * (y2 - y1) / (x2 - x1)
cell.Value = y
End If
Next
回复:用回归值替换缺失数据瞄准MS,
干杯!
回复:用回归值替换缺失数据几年前我遇到了类似的问题,并编写了一个脚本来通过“局部回归”进行插补。
这是更新的版本:
dt = Current Data Table();
n = N Rows(dt);
x = Column(1);// Column("t")
For(i = 2, i <= N Col(dt), i++,
y = Column(i);
For Each Row(
If(1 < Row() < n & Is Missing(y[]),
// find previous non-missing row
Eval(Eval Expr(r1 = Max(dt << get rows where(!Is Missing(y[]) & Row() < Expr(Row())))));
// find next non-missing row
Eval(Eval Expr(r2 = Min(dt << get rows where(!Is Missing(y[]) & Row() > Expr(Row())))));
y[] = y[r1] + (y[r2] - y[r1]) / (x[r2] - x[r1]) * (x[] - x[r1]);
)
)
);
回复:用回归值替换缺失数据这是一个可以完成任务的脚本
Names Default To Here( 1 );
dt = Current Data Table();
For( rows = 1, rows <= N Rows( dt ), rows++,
If( Rows == 1, counter = 0 );
If( Is Missing( dt:output[rows] ) == 1,
counter = counter + 1,
If( counter > 0,
incr = (dt:output[Rows] - dt:output[Rows - counter - 1]) / (counter + 1);
For( i = Rows - counter, i <= Rows - 1, i++,
dt:output = dt:output[i - 1] + incr
);
counter = 0;
)
);
);
// Set all default referenced memory variables to this unique application
Names Default To Here( 1 );
// Set the variable dt to be a pointer to the current active data table
dt = Current Data Table();
// Loop through the data table from row 1 to the last row and use a
// variable names "rows" as the index
For( rows = 1, rows <= N Rows( dt ), rows++,
// If this is row 1 initialize a variable named counter to 1
If( Rows == 1, counter = 0 );
// If the current row's value for the column named "Output" in the
// data table referenced by "dt" is missing (blank, null) increase
// the counter
If( Is Missing( dt:output[rows] ) == 1,
counter++
);
// If row is found with a non missing value for the column called Output,
// and the counter is > 0, which indicates previous rows with missing
// values, then calculate the required values and place them in their
// appropriate rows
If( counter > 0 & Is Missing( dt:output[rows] ) == 0,
// Calculate the incremental steps to use for the missing cells
incr = (dt:output[Rows] - dt:output[Rows - counter - 1]) / (counter + 1);
// Loop across the rows with missing values and fill in the data
For( i = Rows - counter, i <= Rows - 1, i++,
dt:output[i] = dt:output[i - 1] + incr
);
// reinitialize the counter
counter = 0;
);
);回复:用回归值替换缺失数据谢谢罗恩,但我没有看到任何可以进行插值的内容?
回复:用回归值替换缺失数据初学者试试这个
插补插件