用迴歸值替換缺失數據<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;
);
);回覆:用迴歸值取代缺失數據謝謝羅恩,但我沒有看到任何可以進行插值的內容?
回覆:用迴歸值取代缺失數據初學者試試這個
插補插件