<?xml version="1.0"?>
<rss version="2.0">
   <channel>
      <title>Exercise  2: Predicted counts by Silje Synnøve Lyder Hermansen</title>
      <link>https://padlet.com/siljesynnove/predicted_counts</link>
      <description></description>
      <language>en-us</language>
      <pubDate>2023-03-13 21:29:04 UTC</pubDate>
      <lastBuildDate>2023-03-16 09:43:46 UTC</lastBuildDate>
      <webMaster>hello@padlet.com</webMaster>
      <image>
         <url></url>
      </image>
      <item>
         <title>Exercise 2a and 2b</title>
         <author></author>
         <link>https://padlet.com/siljesynnove/predicted_counts/wish/2517794053</link>
         <description><![CDATA[<div><br><br>###### Exercise 2a<br><br># How many observations have a predicted probability above 0.5?<br><br>sum(df$preds1 &gt; 0.5)<br># 0 values<br><br>sum(df$preds2 &gt; 0.5, na.rm = T)<br># 172 values.<br><br><br># Decide on a τ, the cut point beyond which the predicted probability translates to a 1.&nbsp;<br># Why did you choose that one?<br><br># I choose the mean of df$PoolsLocal.&nbsp; I choose this cut-off because it represents the proportion of 0 and 1 in df$PoolsLocal.&nbsp;<br>I looked up other ways to decide on a cut-off value and found Youden J-value, but I was not able to calculate a J-value. Hence, i went with the mean of df$PoolsLocal.&nbsp;<br><br><br># Now, I find the mean of the values<br>summary(df$PoolsLocal)<br><br># Recode preds as 1 or 0 depending on whether predicted value is above of below the mean<br># i.e. Tao = mean of PoolsLocal.<br>df$pred1 = ifelse(df$preds1 &gt; mean(df$PoolsLocal),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;yes = 1,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;no = 0)<br>df$pred2 = ifelse(df$preds2 &gt; mean(df$PoolsLocal),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;yes = 1,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;no = 0)<br><br># Create a confusion matrix with the observed and predicted value.<br>df[, c("PoolsLocal", "pred1")] %&gt;%<br>&nbsp; table %&gt;%<br>&nbsp; prop.table<br># 59% of observation are correctly classified as 0 and 1. <br><br><br>df[, c("PoolsLocal", "pred2")] %&gt;%<br>&nbsp; table %&gt;%<br>&nbsp; prop.table<br># 67% of observations are correctly classified as 0 and 1. <br><br><br>###### Exercise 2b<br><br># Normal frequency table to calculate True Positive Rate and False Positive Rate:<br>table(df$PoolsLocal, df$pred1)<br>TRP1 = 172 / (172+78)<br>FRP1 = 208 / (208+249)<br><br>table(df$PoolsLocal, df$pred2)<br>TRP2 = 187 / (187+59)<br>FRP2 = 163 / (163+277)<br><br></div>]]></description>
         <enclosure url="" />
         <pubDate>2023-03-15 13:45:32 UTC</pubDate>
         <guid>https://padlet.com/siljesynnove/predicted_counts/wish/2517794053</guid>
      </item>
      <item>
         <title>ex2a+b</title>
         <author></author>
         <link>https://padlet.com/siljesynnove/predicted_counts/wish/2518349417</link>
         <description><![CDATA[<div>#count obs - model1=0, model2=172<br>tau&lt;-0.5<br>predmatrix %&gt;%&nbsp;<br>&nbsp; filter(predmod1&gt;tau) %&gt;%&nbsp;<br>&nbsp; count()<br><br># I have no idea how to choose a proper cutoff, so I just worked with&nbsp; .3.<br><br>#generate vars<br>tau &lt;- 0.3<br>predmatrix&lt;-predmatrix %&gt;%&nbsp;<br>&nbsp; mutate(preds1 = if_else(predmod1&gt;tau, 1, 0),&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;preds2 = if_else(predmod2&gt;tau, 1, 0))<br><br>#confusion matrix as prop table<br>prop.table(table(predmatrix$df.PoolsLocal, predmatrix$preds1))<br>prop.table(table(predmatrix$df.PoolsLocal, predmatrix$preds2))<br>#model 2 slightly better<br><br>#TPR = TP / TP+FN<br>#FPR = FP / FP+TN<br>tpr1 &lt;- 172/(172+78)&nbsp;<br>fpr1 &lt;- 208/(208+249)<br>tpr2 &lt;- 210/(210+36)<br>fpr2 &lt;- 207/(207+233)<br><br><br></div>]]></description>
         <enclosure url="" />
         <pubDate>2023-03-15 20:25:35 UTC</pubDate>
         <guid>https://padlet.com/siljesynnove/predicted_counts/wish/2518349417</guid>
      </item>
      <item>
         <title>for_loop (no clue if its right)</title>
         <author></author>
         <link>https://padlet.com/siljesynnove/predicted_counts/wish/2518353482</link>
         <description><![CDATA[<div>TPR1 = NULL<br>FPR1 = NULL<br>tau_loop &lt;- seq(from=0, to=1, by=0.05)<br><br>for(j in 1:length(tau_loop)) {<br>&nbsp;&nbsp;<br>&nbsp; preds0&lt;-predict(mod1,df,"response") &gt; tau_loop[j]<br>&nbsp; truePos&lt;-sum(df$PoolsLocal==1 &amp; preds0==1,na.rm = T)<br>&nbsp; Pos&lt;-sum(df$PoolsLocal,na.rm = T)<br>&nbsp;&nbsp;<br>&nbsp; falsePos&lt;-sum(df$PoolsLocal==0 &amp; preds0==1,na.rm = T)<br>&nbsp; Neg&lt;-sum(df$PoolsLocal==0,na.rm = T)<br>&nbsp;&nbsp;<br>&nbsp; #Indexation<br>&nbsp; TPR1[j] &lt;- truePos / Pos<br>&nbsp; FPR1[j] &lt;- falsePos / Neg<br>}<br><br></div>]]></description>
         <enclosure url="" />
         <pubDate>2023-03-15 20:29:45 UTC</pubDate>
         <guid>https://padlet.com/siljesynnove/predicted_counts/wish/2518353482</guid>
      </item>
      <item>
         <title>ROC</title>
         <author></author>
         <link>https://padlet.com/siljesynnove/predicted_counts/wish/2518374770</link>
         <description><![CDATA[<div>#ROC(K OUT) curves<br><br>#Empty objects<br>TPR=NULL;<br>FPR=NULL<br><br>#Cut values<br>tau&lt;-c(0,0.35,1)<br><br>#Indexation<br>for(i in 1:length(tau)) {&nbsp;<br>preds0&lt;-predict(mod1,df,"response") &gt; tau[i]<br>truePos&lt;-sum(df$PoolsLocal==1 &amp; preds0==1,na.rm = T)<br>Pos&lt;-sum(df$PoolsLocal,na.rm = T)<br><br>falsePos&lt;-sum(df$PoolsLocal==0 &amp; preds0==1,na.rm = T)<br>Neg&lt;-sum(df$PoolsLocal==0,na.rm = T)<br><br>#Indexation<br>TPR[i]&lt;-truePos/Pos<br>FPR[i]&lt;-falsePos/Neg<br><br>}<br>roc &lt;- data.frame(TPR, FPR)<br>roc %&gt;%&nbsp;<br>&nbsp; ggplot() +<br>&nbsp; geom_point(aes(x=FPR, y=TPR)) +&nbsp;<br>&nbsp; geom_segment(aes(x=FPR[1], xend=FPR[2], y=TPR[1], yend=TPR[2])) +&nbsp;<br>&nbsp; geom_segment(aes(x=FPR[2], xend=FPR[3], y=TPR[2], yend=TPR[3]))</div>]]></description>
         <enclosure url="" />
         <pubDate>2023-03-15 20:54:27 UTC</pubDate>
         <guid>https://padlet.com/siljesynnove/predicted_counts/wish/2518374770</guid>
      </item>
      <item>
         <title>2a</title>
         <author></author>
         <link>https://padlet.com/siljesynnove/predicted_counts/wish/2519010488</link>
         <description><![CDATA[<div># Exercise 2a: Predicted values<br><br># Calculate the predicted values from model 1 and 2.<br>pred_values1 &lt;- predict(mod1,&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;#In-sample prediction<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;newdata = df,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;#I want probabilities<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;type = "response")<br><br>pred_values2 &lt;- predict(mod2,&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #In-sample prediction<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newdata = df,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #I want probabilities<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type = "response")<br><br><br># How many observations have a predicted probability above 0.5?<br># count number of observations with predicted probability above 0.5,&nbsp;<br># excluding missing values<br>sum(pred_values1 &gt; 0.5 &amp; complete.cases(pred_values1)) # 0 observations<br>sum(pred_values2 &gt; 0.5 &amp; complete.cases(pred_values2)) # 172 observations<br><br><br># Decide on a τ, the cut point beyond which&nbsp;<br># the predicted probability translates to a 1. Why did you choose that one?<br><br># I use the base-line model (intercept only model / proportion of 1s).&nbsp;<br># It is the value at which I’d predict as much wrong&nbsp;<br># in one direction as the other without any other predictors in the model.<br><br># τ = 0.35<br># (i.e the proportion of 1s in the dataset is 35%)<br><br># However, the correct threshold depends on the cost of false negatives and false positives<br><br><br># Create a frequency table.&nbsp;<br># How many 0s and 1s did you predict?&nbsp;<br># How does it compare with the observed frequency of PoolsLocal?<br><br># create logical vector based on tau cut of 0.35<br>tau_cut1 &lt;- pred_values1 &gt;= 0.35<br>tau_cut2 &lt;- pred_values2 &gt;= 0.35<br><br># convert logical vector to numeric vector with 1s and 0s<br>tau_cut_numeric1 &lt;- as.numeric(tau_cut1)<br>tau_cut_numeric2 &lt;- as.numeric(tau_cut2)<br><br># create frequency table<br>table(tau_cut_numeric1)<br>table(tau_cut_numeric2)<br><br># Observed frequency of Poolslocal<br>table(df$PoolsLocal)<br><br># Both model 1 and model 2 predict a higher number of 1s (pools ressources)<br># than the observed outcome.&nbsp;</div>]]></description>
         <enclosure url="" />
         <pubDate>2023-03-16 08:01:10 UTC</pubDate>
         <guid>https://padlet.com/siljesynnove/predicted_counts/wish/2519010488</guid>
      </item>
      <item>
         <title>2b</title>
         <author></author>
         <link>https://padlet.com/siljesynnove/predicted_counts/wish/2519010905</link>
         <description><![CDATA[<div># Exercise 2b: True positives vs. false positives<br><br># Create a cross table where you compare&nbsp;<br># your predicted 0s and 1s with the actual observed values.&nbsp;<br># This is called a confusion matrix.&nbsp;<br><br># Rename variables<br>observed &lt;- df$PoolsLocal<br>predicted_mod1 &lt;- tau_cut_numeric1<br>predicted_mod2 &lt;- tau_cut_numeric2<br><br><br># create table<br>conf_mat1 &lt;- table(observed, predicted_mod1)<br>conf_mat1<br><br>conf_mat2 &lt;- table(observed, predicted_mod2)<br>conf_mat2<br><br><br><br># Calculate the true positive rate (sensitivity),<br># defined as the number of correctly predicted 1s divided by all observed 1s<br><br># convert confusion matrix to proportions, where each row sums to 1.&nbsp;<br>conf_mat1 %&gt;%<br>&nbsp; prop.table(., margin = 1)<br># sensitivity is seen the in lower right corner (68,8%)<br><br>conf_mat2 %&gt;%<br>&nbsp; prop.table(., margin = 1)<br># sensitivity is seen the in lower right corner (76%)<br><br><br># Calculate the false positive rate,<br># defined as the number of incorrectly predicted 0s divided by the number of observed 0s<br><br># Mod1: the false positive rate is seen in the upper right corner (45,5%)<br># Mod2: the false positive rate is seen in the upper right corner (37%)<br><br># This means that with the selected tau cut of 0.35, mod2 perfoms best<br><br><br>#Alternative<br>#Cut value;tau<br>tau &lt;- 0.35<br><br>#Predicted&nbsp; outcome<br>df$preds1&nbsp; &lt;- predict(mod1, df, "response")&gt;tau<br>df$preds2&nbsp; &lt;- predict(mod2, df, "response")&gt;tau<br><br><br>#True positives<br>truePos1 &lt;- sum(df$PoolsLocal == 1 &amp; df$preds1 == 1, na.rm = T)<br>truePos2 &lt;- sum(df$PoolsLocal == 1 &amp; df$preds2 == 1, na.rm = T)<br><br><br>#All positives<br>Pos&lt;-sum(df$PoolsLocal,na.rm = T)<br><br>#The rate<br>truePos1/Pos<br>truePos2/Pos</div>]]></description>
         <enclosure url="" />
         <pubDate>2023-03-16 08:01:37 UTC</pubDate>
         <guid>https://padlet.com/siljesynnove/predicted_counts/wish/2519010905</guid>
      </item>
      <item>
         <title>2c</title>
         <author></author>
         <link>https://padlet.com/siljesynnove/predicted_counts/wish/2519011816</link>
         <description><![CDATA[<div><br># Exercise 2c: Make a ROC curve (and learn how to write a loop).<br><br># As we change the cut value τ<br># we can observe a trade-off between the true positive rate (TPR)&nbsp;<br># and the false positive rate (FPR).&nbsp;<br># A higher cut value will result in fewer positive predictions (both true and false),&nbsp;<br># and therefore a lower TPR and FPR.&nbsp;<br># On the other hand, a lower cut value will result&nbsp;<br># in more positive predictions (both true and false),&nbsp;<br># and therefore a higher TPR and FPR.<br># This is what the ROC curve illustrates.<br><br><br># Calculate the TPR and the FPR for three cut values: 0, 0.35 and 1.<br># Here, I do it for the first cut value by relying on indexation (Hermansen 2023, ch 3).&nbsp;<br><br>#Empty objects<br>TPR=NULL;FPR=NULL<br><br>#Cut values<br>tau&lt;-c(0,0.35,1)<br><br>#Indexation<br>df$preds0&lt;-predict(mod1,df,"response") &gt; tau[1]<br><br>truePos&lt;-sum(df$PoolsLocal==1&amp;df$preds0==1,na.rm = T)<br>Pos&lt;-sum(df$PoolsLocal,na.rm = T)<br><br>falsePos&lt;-sum(df$PoolsLocal==0&amp;df$preds0==1,na.rm = T)<br>Neg&lt;-sum(df$PoolsLocal==0,na.rm = T)<br><br>#Indexation<br>TPR[1]&lt;-truePos/Pos<br>FPR[1]&lt;-falsePos/Neg<br><br># You can do it for the following two cut values by&nbsp;<br># replacing 1 by 2, and then 3 in ([1]).<br><br>#Indexation<br>df$preds0&lt;-predict(mod1,df,"response") &gt; tau[2]<br><br>truePos&lt;-sum(df$PoolsLocal==1&amp;df$preds0==1,na.rm = T)<br>Pos&lt;-sum(df$PoolsLocal,na.rm = T)<br><br>falsePos&lt;-sum(df$PoolsLocal==0&amp;df$preds0==1,na.rm = T)<br>Neg&lt;-sum(df$PoolsLocal==0,na.rm = T)<br><br>#Indexation<br>TPR[2]&lt;-truePos/Pos<br>FPR[2]&lt;-falsePos/Neg<br><br>#Indexation<br>df$preds0&lt;-predict(mod1,df,"response") &gt; tau[3]<br><br>truePos&lt;-sum(df$PoolsLocal==1&amp;df$preds0==1,na.rm = T)<br>Pos&lt;-sum(df$PoolsLocal,na.rm = T)<br><br>falsePos&lt;-sum(df$PoolsLocal==0&amp;df$preds0==1,na.rm = T)<br>Neg&lt;-sum(df$PoolsLocal==0,na.rm = T)<br><br>#Indexation<br>TPR[3]&lt;-truePos/Pos<br>FPR[3]&lt;-falsePos/Neg<br><br># Show values<br>TPR<br>FPR<br><br><br># Make a plot where you define FPR along the x-axis&nbsp;<br># and the TPR along the y-axis.&nbsp;<br># Draw a line between the three dots<br><br>#Create plot with labeled axes<br>plot(FPR, TPR,&nbsp;<br>&nbsp; &nbsp; &nbsp;type="l",&nbsp;<br>&nbsp; &nbsp; &nbsp;xlab="False Positive Rate (FPR)",&nbsp;<br>&nbsp; &nbsp; &nbsp;ylab="True Positive Rate (TPR)",<br>&nbsp; &nbsp; &nbsp;main = "ROC plot for mod1")<br><br><br><br># Use the R code from the first ROC curve,&nbsp;<br># wrap it in a for loop, and let the tau vary from 0 to 1 through increments of 0.05.<br># Use the code to illustrate the ROC curve of the second model in this problem sheet<br><br># This code uses a for loop to iterate over each tau value in the tau vector.&nbsp;<br># Inside the loop, the code calculates the true positive rate (TPR)&nbsp;<br># and false positive rate (FPR) for the current tau value&nbsp;<br># and saves them in the TPR and FPR vectors, respectively.&nbsp;<br># The loop continues until all tau values have been processed.<br><br><br>#Empty objects<br>TPR=NULL;FPR=NULL<br><br>#Cut values<br>tau_seq &lt;- seq(from = 0, to = 1, by = 0.05)<br><br><br>for (i in 1:length(tau_seq)) {<br>&nbsp;&nbsp;<br>&nbsp; # Indexation<br>&nbsp; df$preds0 &lt;- predict(mod2, df, "response") &gt; tau_seq[i]<br>&nbsp;&nbsp;<br>&nbsp; truePos &lt;- sum(df$PoolsLocal == 1 &amp; df$preds0 == 1, na.rm = T)<br>&nbsp; Pos &lt;- sum(df$PoolsLocal, na.rm = T)<br>&nbsp;&nbsp;<br>&nbsp; falsePos &lt;- sum(df$PoolsLocal == 0 &amp; df$preds0 == 1, na.rm = T)<br>&nbsp; Neg &lt;- sum(df$PoolsLocal == 0, na.rm = T)<br>&nbsp;&nbsp;<br>&nbsp; # Indexation<br>&nbsp; TPR[i] &lt;- truePos / Pos<br>&nbsp; FPR[i] &lt;- falsePos / Neg<br>}<br><br>TPR<br>FPR<br><br>#Create plot with labeled axes<br>plot(FPR, TPR,&nbsp;<br>&nbsp; &nbsp; &nbsp;type="l",&nbsp;<br>&nbsp; &nbsp; &nbsp;xlab="False Positive Rate (FPR)",&nbsp;<br>&nbsp; &nbsp; &nbsp;ylab="True Positive Rate (TPR)",<br>&nbsp; &nbsp; &nbsp;main = "ROC plot for mod2")<br><br></div>]]></description>
         <enclosure url="" />
         <pubDate>2023-03-16 08:02:35 UTC</pubDate>
         <guid>https://padlet.com/siljesynnove/predicted_counts/wish/2519011816</guid>
      </item>
      <item>
         <title>2</title>
         <author></author>
         <link>https://padlet.com/siljesynnove/predicted_counts/wish/2519120459</link>
         <description><![CDATA[<div>halfScore = sum(pred.prob &gt;= 0.5)<br><br>confM &lt;- confusionMatrix(data=as.factor(pred.val),reference=as.factor(model_data$PoolsLocal))<br><br>freqtab = data.frame(Predicted=table(pred.val),Observed = table(model_data$PoolsLocal))<br>freqtab = select(freqtab,-Observed.Var1)</div>]]></description>
         <enclosure url="" />
         <pubDate>2023-03-16 09:43:20 UTC</pubDate>
         <guid>https://padlet.com/siljesynnove/predicted_counts/wish/2519120459</guid>
      </item>
   </channel>
</rss>
