0

パッケージを見始めましたpls& で個別の係数を抽出する方法がわかりませんgroup/factor。グループごとに個別のモデルを実行したり、X ~ group相互作用の項を検討したりできますが、それは私が求めているものではありません。

次の構文を使用しています。

model1 <- plsr(outcome ~ pred * group, data =plsDATA,2)

私は以下を使用してみました:

model2 <- plsr(outcome ~ embed(pred:as.factor(group)), data=plsDATA,2)

しかし、これにより次のエラーが発生します。

model.frame.default(formula = results ~ embed(pred:as.factor(group)) のエラー: 変数の長さが異なります ('embed(pred:as.factor(group))' で見つかりました) さらに: 警告メッセージ: 1: pred:as.factor(group) 内: 数値式には 640 個の要素があります: 最初に使用されるもののみ 2: pred:as.factor(group) 内: 数値式には 32 個の要素があります: 最初に使用されるもののみ

次のコマンドを実行すると互換性のある寸法が得られるため、可変長エラーが発生する理由がわかりません。

dim(group)
[1] 32  1

dim(outcome)
[1] 32  1

dim(pred)
[1] 32 20

コードは以下のとおりです。

library(pls) #Dummy Data 
setwd("/Users/John/Documents") 
Data <- read.csv("SamplePLS.csv") #Define each of the inputs pred is X, group is the factor & outcome is Y 
pred <- as.matrix(Data[,3:22]) 
group <- as.matrix(Data[,1]) 
outcome <- as.matrix(Data[,2]) #now combine the matrices into a single dataframe 
plsDATA <- data.frame(SampN=c(1:nrow(Data))) 
plsDATA$pred <- pred 
plsDATA$group <- group 
plsDATA$outcome <-outcome #define the model - ask for two components 
model1 <- plsr(outcome ~ pred * group, data=plsDATA,2)#Get coefficients from this object
4

2 に答える 2

0

実際、私はこれを理解しました。グループ化変数をダミーコード化し、それを結果 (つまり、予測変数) にする必要があります。この場合、グループ メンバーシップを表す 2 つの列がありました。いずれの場合も、グループのメンバーシップは 1 で示され、非メンバーシップは 0 で示されました。次に、最初の 2 つの列をグループとして呼び出し (つまり、group <- as.matrix(Data[,1:2])) & 残りを実行しました。結果のグループを置き換える前のコードの。

于 2016-04-21T19:44:53.393 に答える
0

According to your question, you are wanting to extract the coefficients. There is a function, 'coef()' that will pull them out easily. See the results below.

Data <- read.csv("SamplePLS.csv") #Define each of the inputs pred is X, group

is the factor & outcome is Y 
> pred <- as.matrix(Data[,3:22]) 
> group <- as.matrix(Data[,1]) 
> outcome <- as.matrix(Data[,2]) #now combine the matrices into a single dataframe 
> plsDATA <- data.frame(SampN=c(1:nrow(Data))) 
> plsDATA$pred <- pred 
> plsDATA$group <- group 
> plsDATA$outcome <-outcome #define the model - ask for two components 
> model1 <- plsr(outcome ~ pred * group, data=plsDATA,2)
> coef(model1)
, , 2 comps

                       outcome
predpred1        -1.058426e-02
predpred2         2.634832e-03
predpred3         3.579453e-03
predpred4         1.135424e-02
predpred5         3.271867e-04
predpred6         4.438445e-03
predpred7         8.425997e-03
predpred8         3.001517e-03
predpred9         2.111697e-03
predpred10       -9.264594e-04
predpred11        1.885554e-03
predpred12       -2.798959e-04
predpred13       -1.390471e-03
predpred14       -1.023795e-03
predpred15       -3.233470e-03
predpred16        5.398053e-03
predpred17        9.796533e-03
predpred18       -8.237801e-04
predpred19        4.778983e-03
predpred20        1.235484e-03
group             9.463735e-05
predpred1:group  -8.814101e-03
predpred2:group   9.013430e-03
predpred3:group   7.597494e-03
predpred4:group   1.869234e-02
predpred5:group   1.462835e-03
predpred6:group   6.928687e-03
predpred7:group   1.925111e-02
predpred8:group   3.752095e-03
predpred9:group   2.404539e-03
predpred10:group -1.288023e-03
predpred11:group  4.271393e-03
predpred12:group  6.704938e-04
predpred13:group -3.943964e-04
predpred14:group -5.468510e-04
predpred15:group -5.595737e-03
predpred16:group  1.090501e-02
predpred17:group  1.977715e-02
predpred18:group -3.013597e-04
predpred19:group  1.169534e-02
predpred20:group  3.389127e-03

The same results could also be achieved with the call model1$coefficients or model1[[1]]. Based on the question, I think this is the result you are looking for.

于 2016-04-21T17:50:53.680 に答える